yt-dlp_alexwlchan: use download_image from chives
- ID
e995c87- date
2026-04-01 06:29:14+00:00- author
Alex Chan <alex@alexwlchan.net>- parent
d63f9a4- message
yt-dlp_alexwlchan: use `download_image` from chives- changed files
3 files, 8 additions, 33 deletions
Changed files
dev_requirements.txt (2049) → dev_requirements.txt (2049)
diff --git a/dev_requirements.txt b/dev_requirements.txt
index 99f39be..0df5591 100644
--- a/dev_requirements.txt
+++ b/dev_requirements.txt
@@ -1,6 +1,6 @@
# This file was autogenerated by uv via the following command:
# uv pip compile dev_requirements.in --output-file=dev_requirements.txt --exclude-newer=P7D --exclude-newer-package alexwlchan-chives=false
-alexwlchan-chives==30
+alexwlchan-chives==32
# via -r requirements.txt
brotli==1.2.0
# via -r requirements.txt
requirements.txt (1028) → requirements.txt (1028)
diff --git a/requirements.txt b/requirements.txt
index c4a7e9e..3b350eb 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,6 +1,6 @@
# This file was autogenerated by uv via the following command:
# uv pip compile requirements.in --output-file=requirements.txt --exclude-newer=P7D --exclude-newer-package alexwlchan-chives=false
-alexwlchan-chives==30
+alexwlchan-chives==32
# via -r requirements.in
brotli==1.2.0
# via yt-dlp
yt-dlp_alexwlchan.py (8488) → yt-dlp_alexwlchan.py (7803)
diff --git a/yt-dlp_alexwlchan.py b/yt-dlp_alexwlchan.py
index cd6bed4..a057cfe 100755
--- a/yt-dlp_alexwlchan.py
+++ b/yt-dlp_alexwlchan.py
@@ -9,7 +9,7 @@ import sys
import tempfile
from typing import Any, TypedDict
-from chives.fetch import fetch_image
+from chives.fetch import download_image
from chives.media import create_video_entity, VideoEntity
import hyperlink
from yt_dlp import YoutubeDL
@@ -49,32 +49,6 @@ ydl_opts: Any = {
}
-def _choose_filename_suffix(content_type: str) -> str:
- """
- Given an HTTP Content-Type header, choose the correct suffix for
- the downloaded file.
- """
- if content_type == "image/png":
- return ".png"
- elif content_type == "image/jpeg":
- return ".jpg"
- else:
- raise ValueError(f"Unrecognised content-type: {content_type}")
-
-
-def download_file(out_dir: Path, url: str, basename: str) -> Path:
- """
- Download an image and return the download path.
- """
- img_data, img_format = fetch_image(url)
- out_path = out_dir / (basename + "." + img_format)
-
- with open(out_path, "xb") as out_file:
- out_file.write(img_data)
-
- return out_path
-
-
def get_youtube_avatar(tmp_dir: Path, channel_url: str) -> Path:
"""
Download the avatar of a YouTube channel.
@@ -104,7 +78,7 @@ def get_youtube_avatar(tmp_dir: Path, channel_url: str) -> Path:
u = hyperlink.parse(channel_url)
basename = u.path[0].replace("@", "")
- return download_file(tmp_dir, url=thumbnail_url, basename=basename)
+ return download_image(url=thumbnail_url, out_prefix=tmp_dir / basename)
def get_instagram_avatar(tmp_dir: Path, uploader_name: str) -> Path:
@@ -118,11 +92,12 @@ def get_instagram_avatar(tmp_dir: Path, uploader_name: str) -> Path:
f"https://www.instagram.com/{uploader_name}/avatar",
"--cookies-from-browser",
"firefox",
- ]
+ ],
+ text=True,
)
- avatar_url = output.strip().decode("utf8")
+ avatar_url = output.strip()
- return download_file(tmp_dir, url=avatar_url, basename=uploader_name)
+ return download_image(url=avatar_url, out_prefix=tmp_dir / uploader_name)
class UploaderInfo(TypedDict):