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 <alekratz@gmail.com>
This commit is contained in:
2023-06-21 00:05:24 -07:00
parent 54a0488e3c
commit 8d79599a1f
2 changed files with 45 additions and 1 deletions

View File

@@ -1,6 +1,46 @@
import asyncio import asyncio
import argparse
from .pull import pull 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())

View File

@@ -12,7 +12,11 @@ from .files import file_cache
BANS_URL = "https://4chan.org/bans" BANS_URL = "https://4chan.org/bans"
PREVIEW_RE = re.compile(r"var postPreviews = (.+)") PREVIEW_RE = re.compile(r"var postPreviews = (.+)")
# TODO(args) --thumbs-dir arg
THUMBS_DIR = Path("thumbs") 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") CACHE_DIR = Path("bans")