def list_generated_issues(ctx): import pprint conn = db.connect(ctx.cfg["hn2ebook"]["db_path"]) issues = db.all_issues(conn) pp = pprint.PrettyPrinter(indent=2) log.info(pp.pformat(issues))
def backfill_best(ctx, start_date, end_date, source): conn = db.connect(ctx.cfg["hn2ebook"]["db_path"]) log.info("Backfilling from %s to %s" % format_range(start_date, end_date)) if source == "/front": with conn: hn.backfill_frontpage(conn, start_date, end_date) elif source == "daemonology": with conn: hn.backfill_daemonology(conn, start_date, end_date)
def new_issue(ctx, period_or_range, as_of, user_output, limit, criteria, persist): cfg = ctx.cfg["hn2ebook"] now = datetime.utcnow() log.info( "⚠️ Please be warned! This can take awhile, as walking the HN comments tree with the API is time intensive." ) if isinstance(period_or_range, str) and period_or_range in period_to_delta: period = period_or_range log.info( "creating %s periodical as of %s, with limit=%s, sort_criteria=%s" % (period, as_of, limit, criteria)) date_range = range_for_period(as_of, period) end_str = str(as_of.date()) output = get_output_path(cfg, user_output, f"{period}-{end_str}") creation_params = { "period": period, "as_of": as_of, "limit": limit, "criteria": criteria, } else: date_range = period_or_range log.info( "creating periodical with range %s, with limit=%s, sort_criteria=%s" % (date_range, limit, criteria)) output = get_output_path(cfg, user_output, "%s-%s" % (date_range[0], date_range[1])) creation_params = { "start": date_range[0], "end": date_range[1], "limit": limit, "criteria": criteria, } period = "custom" stories = collect_stories(cfg, date_range, limit, criteria) if len(stories) == 0: log.info( "No stories were found in the given range. You should run the backfill command." ) sys.exit(2) log.info("collected %d stories for the issue" % len(stories)) meta = issue_meta(stories, creation_params, isoformat(now), str(uuid4())) epub_path = core.epub_from_stories(cfg, stories, meta, output) if persist: with db.connect(cfg["db_path"]) as conn: persist_epub_meta(conn, now, stories, meta, epub_path, period)
def generate_opds(ctx): cfg = ctx.cfg["hn2ebook"] conn = db.connect(cfg["db_path"]) instance = { "root_url": cfg["root_url"], "name": cfg["instance_name"], "url": "/index.xml", } feeds = [ { "period": "daily", "name": "Hacker News Daily", "url": f"/daily.xml", "up_url": f"/index.xml", "start_url": f"/index.xml", "content": "Daily periodicals of the best stories on Hacker News", }, { "period": "weekly", "name": "Hacker News Weekly", "url": f"/weekly.xml", "up_url": f"/index.xml", "start_url": f"/index.xml", "content": "Weekly periodicals of the best stories on Hacker News", }, { "period": "monthly", "name": "Hacker News Monthly", "url": f"/monthly.xml", "up_url": f"/index.xml", "start_url": f"/index.xml", "content": "Monthly periodicals of the best stories on Hacker News", }, ] for feed in feeds: core.generate_opds( ctx.cfg["hn2ebook"], instance, feed, db.issues_by_period(conn, feed["period"]), ) core.generate_opds_index(ctx.cfg["hn2ebook"], instance, feeds) log.info("OPDS feed available at %s/index.xml" % ctx.cfg["hn2ebook"]["root_url"])
def update_best(ctx): conn = db.connect(ctx.cfg["hn2ebook"]["db_path"]) day = datetime.utcnow().date() with conn: hn.update_best_stories(conn, day)
def collect_stories(cfg, date_range, limit, criteria): log.info("collecting stories for range %s - %s" % format_range(date_range)) conn = db.connect(cfg["db_path"]) stories = stories_for_range(cfg, conn, date_range, limit, criteria) conn.close() return stories