Add ability to specify day and year

master
Nick Krichevsky 2021-12-27 19:06:23 -05:00
parent 1a2c56aa3d
commit 0ab6b505d6
1 changed files with 28 additions and 19 deletions

View File

@ -7,7 +7,7 @@ import pathlib
import sys import sys
import time import time
import urllib.parse import urllib.parse
from typing import List from typing import List, Optional
import random import random
import bs4 import bs4
@ -142,21 +142,6 @@ def get_token_from_file(path: pathlib.Path) -> str:
return token return token
def download_latest_inputs(output_dir: pathlib.Path, token: str):
date = PuzzleDate.get_latest()
step = None
try:
step = "download sample inputs"
download_sample_inputs(date, output_dir)
step = "download input"
download_input(date, token, output_dir)
except requests.exceptions.HTTPError as err:
LOG.error(f"Failed to {step}: {err}")
LOG.debug(f"Response body: {err.response.text}")
sys.exit(1)
def wait_for_next_puzzle(): def wait_for_next_puzzle():
next_date = PuzzleDate.get_next() next_date = PuzzleDate.get_next()
next_datetime = datetime.datetime(year=next_date.year, month=12, day=next_date.day) next_datetime = datetime.datetime(year=next_date.year, month=12, day=next_date.day)
@ -181,7 +166,17 @@ def setup_logs(verbose: bool):
) )
@click.command() class FetchCommand(click.Command):
def invoke(self, ctx: click.Context):
year = ctx.params.get("year")
day = ctx.params.get("day")
if (year and not day) or (not day and year):
raise click.ClickException("day and year must be specified together")
return super().invoke(ctx)
@click.command(cls=FetchCommand)
@click.option("-v", "--verbose", is_flag=True) @click.option("-v", "--verbose", is_flag=True)
@click.option( @click.option(
"--token", "passed_token", type=str, help="The advent of code session token." "--token", "passed_token", type=str, help="The advent of code session token."
@ -201,14 +196,17 @@ def setup_logs(verbose: bool):
help="The location of the advent of code session token. Ignored if --token is provided.", help="The location of the advent of code session token. Ignored if --token is provided.",
) )
@click.option("--wait", is_flag=True, help="Wait until new puzzle inputs are ready") @click.option("--wait", is_flag=True, help="Wait until new puzzle inputs are ready")
@click.option("--year", type=int)
@click.option("--day", type=int)
def main( def main(
verbose: bool, verbose: bool,
passed_token: str, passed_token: str,
token_path: pathlib.Path, token_path: pathlib.Path,
output_dir: pathlib.Path, output_dir: pathlib.Path,
wait: bool, wait: bool,
day: Optional[int],
year: Optional[int],
): ):
PuzzleDate.get_next()
token = passed_token token = passed_token
if not token and not token_path: if not token and not token_path:
LOG.error("no token file given") LOG.error("no token file given")
@ -226,7 +224,18 @@ def main(
if wait: if wait:
wait_for_next_puzzle() wait_for_next_puzzle()
download_latest_inputs(output_dir, token) specified_date = PuzzleDate(year=year, day=day) if day and year else None
date_to_fetch = specified_date or PuzzleDate.get_latest()
step = None
try:
step = "download sample inputs"
download_sample_inputs(date_to_fetch, output_dir)
step = "download input"
download_input(date_to_fetch, token, output_dir)
except requests.exceptions.HTTPError as err:
LOG.error(f"Failed to {step}: {err}")
LOG.debug(f"Response body: {err.response.text}")
sys.exit(1)
if __name__ == "__main__": if __name__ == "__main__":