From 8d79599a1f0e6715204c069f13077d2798e55c1c Mon Sep 17 00:00:00 2001 From: Alek Ratzloff Date: Wed, 21 Jun 2023 00:05:24 -0700 Subject: [PATCH] Add arg parsing and subcommands * pull - will download thumbnails and update the database * serve - (in the future) will run an HTTP frontend server to display the pulled data Signed-off-by: Alek Ratzloff --- chanbans/__main__.py | 42 +++++++++++++++++++++++++++++++++++++++++- chanbans/pull.py | 4 ++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/chanbans/__main__.py b/chanbans/__main__.py index f861ec2..acb22e5 100644 --- a/chanbans/__main__.py +++ b/chanbans/__main__.py @@ -1,6 +1,46 @@ import asyncio +import argparse from .pull import pull -asyncio.run(pull()) +def parse_args(): + parser = argparse.ArgumentParser(description="Run 4chan bans archiver") + subparsers = parser.add_subparsers(title="Commands", dest="command") + + subcommands = set() + + def add_subcommand(subcommand: str, *args, **kwargs): + nonlocal subparsers, subcommands + assert ( + subcommand not in subcommands + ), f"subcommand {subcommand} was already registered" + subcommands |= {subcommand} + return subparsers.add_parser(subcommand, *args, **kwargs) + + _pull_parser = add_subcommand( + "pull", + help="Pull bans from 4chan, save thumbnails, update the database, and exit", + ) + _serve_parser = add_subcommand("serve", help="Start HTTP server") + _help_parser = add_subcommand("help", help="Show this help message") + + args = parser.parse_args() + + if args.command not in subcommands or args.command == "help": + parser.print_help() + + return args + + +async def main(): + args = parse_args() + match args.command: + case "pull": + await pull() + case "serve": + print("TODO: HTTP server") + case command: + assert False, f"unknown command {command} that was not caught in add_subcommand" + +asyncio.run(main()) diff --git a/chanbans/pull.py b/chanbans/pull.py index bb58460..754f981 100644 --- a/chanbans/pull.py +++ b/chanbans/pull.py @@ -12,7 +12,11 @@ from .files import file_cache BANS_URL = "https://4chan.org/bans" PREVIEW_RE = re.compile(r"var postPreviews = (.+)") +# TODO(args) --thumbs-dir arg THUMBS_DIR = Path("thumbs") +# TODO(args) --cache-dir arg +# this is kind of hard to do because it's set in a decorator. I think the best option would be to +# make a global "config" object CACHE_DIR = Path("bans")