refactor(download_imageset): Migrate CLI from argparse to click

This commit is contained in:
2026-07-28 08:41:40 +02:00
parent c9228ae4a7
commit 0ddf2c4db5
+44 -62
View File
@@ -1,9 +1,8 @@
#!/usr/bin/env -S uv run --script #!/usr/bin/env -S uv run --script
# /// script # /// script
# requires-python = ">=3.11" # requires-python = ">=3.11"
# dependencies = ["httpx", "pillow"] # dependencies = ["httpx", "pillow", "click"]
# /// # ///
import argparse
import dataclasses import dataclasses
import logging import logging
import os import os
@@ -16,6 +15,7 @@ from functools import partial
from pathlib import Path from pathlib import Path
from typing import Callable from typing import Callable
import click
import httpx import httpx
from PIL import Image from PIL import Image
@@ -326,11 +326,24 @@ def is_image(f: Path) -> bool:
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
# Subcommands # CLI
# --------------------------------------------------------------------------- # ---------------------------------------------------------------------------
def cmd_gallery(urls: list[str], name: str | None, cwd: Path | None = None) -> None: _DEFAULT_CWD = Path("~/Downloads/_temp/_imagesets").expanduser()
@click.group()
def cli() -> None:
pass
@cli.command("gallery")
@click.argument("urls", nargs=-1, required=True)
@click.option("--name", default=None)
@click.option("--cwd", type=Path, default=None)
def gallery_cmd(urls: tuple[str, ...], name: str | None, cwd: Path | None) -> None:
urls = list(urls)
resolved_name = name or _name_from_url(urls[0]) resolved_name = name or _name_from_url(urls[0])
output_dir = (cwd or Path(".")) / resolved_name output_dir = (cwd or Path(".")) / resolved_name
@@ -346,9 +359,12 @@ def cmd_gallery(urls: list[str], name: str | None, cwd: Path | None = None) -> N
_extract_and_process(output_dir) _extract_and_process(output_dir)
def cmd_filehost( @cli.command("filehost")
urls: list[str], name: str | None, token: str, cwd: Path | None = None @click.argument("urls", nargs=-1, required=True)
) -> None: @click.option("--name", default=None)
@click.option("--cwd", type=Path, default=_DEFAULT_CWD)
@click.option("--token", default=os.getenv("ALLDEBRID_TOKEN"))
def filehost_cmd(urls: tuple[str, ...], name: str | None, cwd: Path, token: str) -> None:
resolved_name = name or _name_from_url(urls[0]) resolved_name = name or _name_from_url(urls[0])
output_dir = (cwd or Path(".")) / resolved_name output_dir = (cwd or Path(".")) / resolved_name
output_dir.mkdir(parents=True, exist_ok=True) output_dir.mkdir(parents=True, exist_ok=True)
@@ -376,7 +392,26 @@ def cmd_filehost(
_extract_and_process(output_dir) _extract_and_process(output_dir)
def cmd_process(dirs: list[str]) -> None: @cli.command("download")
@click.argument("urls", nargs=-1, required=True)
@click.option("--name", default=None)
@click.option("--cwd", type=Path, default=_DEFAULT_CWD)
@click.option("--token", default=os.getenv("ALLDEBRID_TOKEN"))
@click.pass_context
def download_cmd(
ctx: click.Context, urls: tuple[str, ...], name: str | None, cwd: Path, token: str
) -> None:
filehost_urls = [u for u in urls if "rg.to" in u or "rapidgator.net" in u]
gallery_urls = [u for u in urls if u not in set(filehost_urls)]
if filehost_urls:
ctx.invoke(filehost_cmd, urls=tuple(filehost_urls), name=name, cwd=cwd, token=token)
if gallery_urls:
ctx.invoke(gallery_cmd, urls=tuple(gallery_urls), name=name, cwd=cwd)
@cli.command("process")
@click.argument("dirs", nargs=-1, required=True)
def process_cmd(dirs: tuple[str, ...]) -> None:
for d in dirs: for d in dirs:
p = Path(d) p = Path(d)
if not p.exists(): if not p.exists():
@@ -405,58 +440,5 @@ def cmd_process(dirs: list[str]) -> None:
print(f"skipping {p}: already within size limits") print(f"skipping {p}: already within size limits")
def cmd_download(
urls: list[str], name: str | None, token: str, cwd: Path | None = None
) -> None:
filehost_urls = [u for u in urls if "rg.to" in u or "rapidgator.net" in u]
gallery_urls = [u for u in urls if u not in set(filehost_urls)]
if filehost_urls:
cmd_filehost(filehost_urls, name, token, cwd)
if gallery_urls:
cmd_gallery(gallery_urls, name, cwd)
# ---------------------------------------------------------------------------
# CLI
# ---------------------------------------------------------------------------
def main() -> None:
print(os.getenv("PATH"))
parser = argparse.ArgumentParser(prog="download_imageset.py")
sub = parser.add_subparsers(dest="command", required=True)
gallery_p = sub.add_parser("gallery")
gallery_p.add_argument("urls", nargs="+")
gallery_p.add_argument("--name", default=None)
gallery_p.add_argument("--cwd", type=Path, default=None)
for cmd in ("filehost", "download"):
p = sub.add_parser(cmd)
p.add_argument("urls", nargs="+")
p.add_argument("--name", default=None)
p.add_argument(
"--cwd",
default=Path("~/Downloads/_temp/_imagesets").expanduser(),
type=Path,
)
p.add_argument("--token", default=os.getenv("ALLDEBRID_TOKEN"))
proc = sub.add_parser("process")
proc.add_argument("dirs", nargs="+")
args = parser.parse_args()
match args.command:
case "gallery":
cmd_gallery(args.urls, args.name, args.cwd)
case "filehost":
cmd_filehost(args.urls, args.name, args.token, cwd=args.cwd)
case "process":
cmd_process(args.dirs)
case "download":
cmd_download(args.urls, args.name, args.token, cwd=args.cwd)
if __name__ == "__main__": if __name__ == "__main__":
main() cli()