Initial implementation of crossword fetcher
This commit is contained in:
parent
e1c2f0c4f2
commit
022308a9dc
|
@ -0,0 +1 @@
|
|||
.direnv
|
|
@ -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()
|
Loading…
Reference in New Issue