#!/usr/bin/env python3 """python3 run.py web|worker|daily (config via env, see README)""" import os import sys from newsdigest import digest, store, web, worker STORE = os.environ.get("NEWS_STORE", os.path.join(os.path.dirname(os.path.abspath(__file__)), "data", "news.json")) if __name__ == "__main__": cmd = sys.argv[1] if len(sys.argv) > 1 else "" if cmd == "web": web.serve(STORE, int(os.environ.get("NEWS_PORT", "8797"))) elif cmd == "worker": with open(os.environ["NEWS_PASSCODE_FILE"]) as f: worker.run(STORE, f.read().strip()) elif cmd == "scheduler": # No cron on the box: build the day's digest once a day after NEWS_DAILY_HOUR (Perth time). import datetime as dt import time hour = int(os.environ.get("NEWS_DAILY_HOUR", "6")) st = store.Store(STORE) while True: now = dt.datetime.now(digest.TZ) st.reload() day = st.data["digests"].get(now.date().isoformat()) or {} if now.hour >= hour and len(day.get("entries", [])) < 10 and day.get("attempts_hour") != now.strftime("%Y-%m-%dT%H"): with st.tx() as data: data["digests"].setdefault(now.date().isoformat(), {"date": now.date().isoformat(), "entries": [], "community": []})["attempts_hour"] = now.strftime("%Y-%m-%dT%H") try: d = digest.build(st) print(f"digest {d['date']}: {len(d['entries'])} articles", flush=True) except Exception as exc: # noqa: BLE001 print(f"daily failed: {type(exc).__name__}: {exc}", flush=True) time.sleep(300) elif cmd == "daily": d = digest.build(store.Store(STORE)) print(f"digest {d['date']}: {len(d['entries'])} articles") else: sys.exit(__doc__)