From e57ffee0388b3fc020cbe480b79e33fcec82de22 Mon Sep 17 00:00:00 2001 From: "Waylon S. Walker" Date: Sun, 12 Oct 2025 12:48:25 -0500 Subject: [PATCH] update scripts --- latest_yt_poster.py | 117 ++++++++++++++++++++++++++------------------ 1 file changed, 70 insertions(+), 47 deletions(-) diff --git a/latest_yt_poster.py b/latest_yt_poster.py index ab20e98..e181c61 100755 --- a/latest_yt_poster.py +++ b/latest_yt_poster.py @@ -6,65 +6,88 @@ # "Pillow", # ] # /// -import os, glob, shutil +""" +Mostly done by gpt-5 +""" + +import os +import glob +import shutil from rich import print +from PIL import Image +import random -base_dir = "/mnt/media/youtube" +def set_posters(base_dir: str) -> None: + """ + Set the latest thumbnail as the poster for each channel. + """ + for channel_dir in os.listdir(base_dir): + full_path = os.path.join(base_dir, channel_dir) + if not os.path.isdir(full_path): + continue -for channel_dir in os.listdir(base_dir): - full_path = os.path.join(base_dir, channel_dir) - if not os.path.isdir(full_path): - continue + # find all video thumbnails (jpg/png) + thumbs = sorted( + glob.glob(os.path.join(full_path, "**/*.jpg")) + + glob.glob(os.path.join(full_path, "**/*.png")), + key=os.path.getmtime, + reverse=True, + ) - # find all video thumbnails (jpg/png) + if not thumbs: + print(f"No thumbnails found for {channel_dir}") + continue + + latest_thumb = thumbs[0] + poster_path = os.path.join(full_path, "poster.jpg") + + if not os.path.exists(poster_path) or os.path.getmtime( + latest_thumb + ) > os.path.getmtime(poster_path): + shutil.copy2(latest_thumb, poster_path) + print(f"Set poster for {channel_dir} → {os.path.basename(latest_thumb)}") + + +def grid_collage(base_dir: str) -> None: + ## Grid collage + + # base_dir = "/mnt/main/media/YouTube" + poster_path = os.path.join(base_dir, "poster.jpg") + + # Collect recent thumbnails from subfolders + thumbs = [] thumbs = sorted( - glob.glob(os.path.join(full_path, "**/*.jpg")) + - glob.glob(os.path.join(full_path, "**/*.png")), + glob.glob(os.path.join(base_dir, "**/*.jpg")) + + glob.glob(os.path.join(base_dir, "**/*.png")), key=os.path.getmtime, - reverse=True - ) + reverse=True, + )[:50] + random.shuffle(thumbs) if not thumbs: - print(f"No thumbnails found for {channel_dir}") - continue + raise SystemExit("No thumbnails found") - latest_thumb = thumbs[0] - poster_path = os.path.join(full_path, "poster.jpg") + # Make a 5x5 grid collage (adjust as desired) + rows, cols = 5, 5 + tile_size = 256 + grid = Image.new("RGB", (cols * tile_size, rows * tile_size)) - if not os.path.exists(poster_path) or os.path.getmtime(latest_thumb) > os.path.getmtime(poster_path): - shutil.copy2(latest_thumb, poster_path) - print(f"Set poster for {channel_dir} → {os.path.basename(latest_thumb)}") + for i, thumb in enumerate(thumbs[: rows * cols]): + img = Image.open(thumb).convert("RGB").resize((tile_size, tile_size)) + r, c = divmod(i, cols) + grid.paste(img, (c * tile_size, r * tile_size)) -## Grid collage -from PIL import Image -import glob, os, random + grid.save(poster_path, quality=85) + print(f"Created {poster_path}") -# base_dir = "/mnt/main/media/YouTube" -poster_path = os.path.join(base_dir, "poster.jpg") -# Collect recent thumbnails from subfolders -thumbs = [] -# for root, dirs, files in os.walk(base_dir): -# for f in files: -# if f.lower().endswith((".jpg", ".png")) and "poster" not in f: -# thumbs.append(os.path.join(root, f)) -# thumbs = sorted(thumbs, key=os.path.getmtime, reverse=True)[:50] -thumbs = sorted(glob.glob(os.path.join(base_dir, "**/*.jpg")) + glob.glob(os.path.join(base_dir, "**/*.png")), key=os.path.getmtime, reverse=True)[:50] -random.shuffle(thumbs) +if __name__ == "__main__": + base_dirs = [ + "/mnt/media/youtube/shared", + "/mnt/media/youtube/waylon", + ] -if not thumbs: - raise SystemExit("No thumbnails found") - -# Make a 5x5 grid collage (adjust as desired) -rows, cols = 2, 2 -tile_size = 256 -grid = Image.new("RGB", (cols*tile_size, rows*tile_size)) - -for i, thumb in enumerate(thumbs[:rows*cols]): - img = Image.open(thumb).convert("RGB").resize((tile_size, tile_size)) - r, c = divmod(i, cols) - grid.paste(img, (c*tile_size, r*tile_size)) - -grid.save(poster_path, quality=85) -print(f"Created {poster_path}") + for base_dir in base_dirs: + set_posters(base_dir) + grid_collage(base_dir)