diff --git a/autograbber.py b/autograbber.py index 53b8235..05374ce 100755 --- a/autograbber.py +++ b/autograbber.py @@ -1,29 +1,27 @@ #!/usr/bin/env python3 +import argparse +import sys + from pathlib import Path -from services.iview import ABCService +from services.iview import iViewService # ------------------------- -# Metadata files +# Metadata paths # ------------------------- DOWNLOAD_META = Path("./.download") HISTORY_FILE = DOWNLOAD_META / ".history" SERIES_FILE = DOWNLOAD_META / ".series" -DOWNLOAD_META.mkdir(exist_ok=True) - -HISTORY_FILE.touch(exist_ok=True) -SERIES_FILE.touch(exist_ok=True) - # ------------------------- # Service registry # ------------------------- SERVICES = { - "ABC": ABCService(), + "IVIEW": iViewService(), } @@ -32,7 +30,10 @@ SERVICES = { # ------------------------- class AutoGrabber: - def __init__(self): + def __init__(self, download_dir, dry_run=False): + + self.download_dir = Path(download_dir) + self.dry_run = dry_run self.history = self.load_history() self.series_list = self.load_series() @@ -44,6 +45,9 @@ class AutoGrabber: history = {} + if not HISTORY_FILE.exists(): + HISTORY_FILE.touch() + with open(HISTORY_FILE, "r") as f: for line in f: @@ -60,7 +64,6 @@ class AutoGrabber: history[ep_id.strip()] = filename.strip() else: - # backwards compatibility history[line.strip()] = None return history @@ -83,6 +86,15 @@ class AutoGrabber: # ------------------------- def load_series(self): + if not SERIES_FILE.exists(): + + print( + "โŒ Missing required file: " + f"{SERIES_FILE}" + ) + + sys.exit(1) + series = [] with open(SERIES_FILE, "r") as f: @@ -162,11 +174,23 @@ class AutoGrabber: continue - print(f"โœ… {filename} โ†’ queued") + # dry-run mode + if self.dry_run: + + print( + f"๐Ÿงช " + f"{filename} " + f"(would download)" + ) + + continue + + print(f"โœ… {filename} โ†’ downloading") success = service.download_episode( episode, - entry + entry, + self.download_dir ) if success: @@ -194,9 +218,36 @@ class AutoGrabber: print("\nโœ… Autograbber run complete") +# ------------------------- +# CLI +# ------------------------- +def parse_args(): + + parser = argparse.ArgumentParser() + + parser.add_argument( + "--dry-run", + action="store_true", + help="Do not download anything" + ) + + parser.add_argument( + "--downloads", + default="./downloads", + help="Download directory" + ) + + return parser.parse_args() + + # ------------------------- # Entry point # ------------------------- if __name__ == "__main__": - AutoGrabber().run() + args = parse_args() + + AutoGrabber( + download_dir=args.downloads, + dry_run=args.dry_run + ).run()