diff --git a/fimfarchive/commands/update.py b/fimfarchive/commands/update.py index aceb7d7..508b184 100644 --- a/fimfarchive/commands/update.py +++ b/fimfarchive/commands/update.py @@ -22,15 +22,20 @@ Update command. # +import os import traceback from argparse import ArgumentParser, Namespace, FileType +from os.path import basename from typing import Any, Iterable, Iterator, Optional +import arrow from jmespath import compile as jmes -from fimfarchive.fetchers import Fetcher, FimfarchiveFetcher, FimfictionFetcher +from fimfarchive.fetchers import ( + Fetcher, FimfarchiveFetcher, Fimfiction2Fetcher, FimfictionFetcher, +) from fimfarchive.flavors import UpdateStatus -from fimfarchive.selectors import Selector, RefetchSelector +from fimfarchive.selectors import Selector, RefetchSelector, UpdateSelector from fimfarchive.signals import SignalReceiver from fimfarchive.stories import Story from fimfarchive.tasks import UpdateTask @@ -43,6 +48,9 @@ __all__ = ( ) +ACCESS_TOKEN_KEY = 'FIMFICTION_ACCESS_TOKEN' + + class StoryFormatter(Iterable[str]): """ Generates a text representation of story meta. @@ -199,6 +207,12 @@ class UpdateCommand(Command): description=self.__doc__, ) + parser.add_argument( + '--alpha', + help="fetch from Fimfiction APIv1", + action='store_true', + ) + parser.add_argument( '--archive', help="previous version of the archive", @@ -222,12 +236,30 @@ class UpdateCommand(Command): Args: opts: Parsed command line arguments. """ - fimfarchive: Fetcher = FimfarchiveFetcher(opts.archive) - fimfiction: Fetcher = FimfictionFetcher() - selector: Optional[Selector] = None + fimfarchive: Fetcher + fimfiction: Fetcher + selector: Selector + + token = os.environ.get(ACCESS_TOKEN_KEY) + + if opts.alpha: + fimfiction = FimfictionFetcher() + elif token: + fimfiction = Fimfiction2Fetcher(token, True, opts.refetch) + else: + exit(f"Environment variable required: {ACCESS_TOKEN_KEY}") if opts.refetch: selector = RefetchSelector() + else: + selector = UpdateSelector() + + print(f"\nStarted: {arrow.now()}") + print(f"Archive: {basename(opts.archive.name)}") + print(f"Fetcher: {type(fimfiction).__name__}") + print(f"Selector: {type(selector).__name__}") + + fimfarchive = FimfarchiveFetcher(opts.archive) return UpdateTask( fimfarchive=fimfarchive, @@ -242,4 +274,6 @@ class UpdateCommand(Command): with UpdatePrinter(task): task.run() + print(f"\nDone: {arrow.now()}") + return 0