From 022308a9dc8008282e36488df046716c36e010ff Mon Sep 17 00:00:00 2001 From: Jez Cope Date: Mon, 28 Jun 2021 19:11:36 +0100 Subject: [PATCH] Initial implementation of crossword fetcher --- .gitignore | 1 + crossword.py | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 2 +- 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 crossword.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..92b2793 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.direnv diff --git a/crossword.py b/crossword.py new file mode 100644 index 0000000..ebe6303 --- /dev/null +++ b/crossword.py @@ -0,0 +1,73 @@ +#!/usr/bin/env python3 + +import requests as rq +import sh +import click +import datetime as dt +import maya +import tempfile +from pathlib import Path +from urllib.parse import urlparse + + +def fetch_and_upload(url, target_dir): + filename = Path(urlparse(url).path).name + with tempfile.TemporaryDirectory() as tmp: + out_path = Path(tmp) / filename + + print(f"Fetching {url} to {out_path}...") + result = rq.get(url) + result.raise_for_status() + out_path.write_bytes(result.content) + + print("Uploading...") + sh.rmapi.put(out_path, target_dir) + + print("Done!") + + +def do_guardian_crossword(publication, kind, date, target): + fetch_and_upload(f'https://crosswords-static.guim.co.uk/{publication}.{kind}.{date:%Y%m%d}.pdf', target) + + +@click.group() +def app(): + pass + + +@app.command() +@click.argument('date', default='today') +@click.option('--target', default='/', help="tablet folder to upload to") +def cryptic(date: str, target: str): + real_date = maya.when(date).datetime() + if real_date.weekday() == 6: + print(f'Error: {real_date:%d/%m/%Y} is a Sunday, and the Guardian is published Mon-Sat!') + return + + do_guardian_crossword('gdn', 'cryptic', real_date, target) + + +@app.command() +@click.argument('date', default='today') +@click.option('--target', default='/', help="tablet folder to upload to") +def everyman(date: str, target: str): + real_date = maya.when(date).datetime() + if real_date.weekday() != 6: + print(f'Error: {real_date:%d/%m/%Y} is a {real_date:%A}, and the Observer is only published on Sunday!') + return + + do_guardian_crossword('obs', 'everyman', real_date, target) + + +@app.command() +@click.argument('date', default='today') +@click.option('--target', default='/', help="tablet folder to upload to") +def auto(date: str, target: str): + real_date = maya.when(date).datetime() + if real_date.weekday() == 6: + do_guardian_crossword('obs', 'everyman', real_date, target) + else: + do_guardian_crossword('gdn', 'cryptic', real_date, target) + +if __name__ == '__main__': + app() diff --git a/flake.nix b/flake.nix index 23c4095..1e6d261 100644 --- a/flake.nix +++ b/flake.nix @@ -10,7 +10,7 @@ in { devShell = pkgs.mkShell { buildInputs = - [ (pkgs.python38.withPackages (py: with py; [ sh requests click ])) ]; + [ (pkgs.python38.withPackages (py: with py; [ sh requests click maya ])) ]; }; }); }