import requests import sys import subprocess from datetime import date from bs4 import BeautifulSoup from config import config def check_error(func, name): try: return func() except Exception as e: sys.stderr.write(f"Error getting {name}") sys.stderr.write(str(e)) return "Error getting %s" % name def get_on_this_day(): old_news = check_error(get_old_news, "old news") peanuts = check_error(get_peanuts, "peanuts") calvin_and_hobbes = check_error(get_calvin_and_hobbes, "calvin and hobbes") wikipedia = check_error(get_today_wikipedia, "wikipedia") wikiquote = check_error(get_today_wikiquote, "wikiquote") thoreau = check_error(get_thoreau, "thoreau") holidays = check_error(get_week_holidays, "holiday") crossword = check_error(get_crossword, "crossword") return "

On this day

%s
%s
" % (old_news, peanuts, calvin_and_hobbes, wikipedia, crossword, holidays, wikiquote, thoreau) def get_old_news(): print("getting old news") year = int(date.today().strftime("%Y")) - 100 century_ago = str(year) + date.today().strftime("-%m-%d") news_text = "" urls = config["news"]["urls"].split(",") names = config["news"]["names"].split(",") for i in range(len(urls)): full_url = urls[i] % century_ago name = names[i] if requests.get(full_url).status_code != 404: news_text += '
%s %s
\n' % (full_url, name, century_ago) return news_text or "No old news" def get_today_wikipedia(): print("getting today's wikipedia") full_url = "https://en.wikipedia.org/wiki/%s" % date.today().strftime("%B_%d") return '
Today\'s Wikipedia
' % (full_url) def get_week_holidays(): print("getting holidays") command = "calendar -f /usr/share/calendar/calendar.mark -A 14".split(" ") output = subprocess.check_output(command) output = output.decode("utf-8").strip().split("\n") return "
  • ".join(output) or "No holidays this fortnight." def get_crossword(): date_str = date.today().strftime("%Y-%m-%d") url = f"https://simplydailypuzzles.com/daily-cryptic/index.html?puzz=dc1-{date_str}" return f'
    Cryptic Crossword
    ' def get_today_wikiquote(): print("getting today's wikiquote") full_url = "https://en.wikiquote.org/wiki/%s" % date.today().strftime("%B_%d") r = requests.get(full_url) soup = BeautifulSoup(r.text, features="lxml") table = str(soup.find(text="2020").parent.parent.next_sibling.next_sibling) table = table.replace('href="/', 'href="https://en.wikiquote.org/') return '
    %s
    ' % table def get_calvin_and_hobbes(): print("getting calvin and hobbes") year = int(date.today().strftime("%Y")) % 9 + 1986 comic_date = str(year) + date.today().strftime("/%m/%d") full_url = "https://www.gocomics.com/calvinandhobbes/%s" % comic_date r = requests.get(full_url) soup = BeautifulSoup(r.text, features="lxml") if not "Today on" in str(soup.title): # gocomics gives you today if 404 comic_src = soup.select(".item-comic-image")[0].img["src"] return '
    Calvin and Hobbes
    ' % (comic_src) else: return "" def get_peanuts(): print("getting peanuts") comic_date = date.today().strftime("%Y/%m/%d") full_url = "https://www.gocomics.com/peanuts/%s" % comic_date r = requests.get(full_url) soup = BeautifulSoup(r.text, features="lxml") comic_src = soup.select(".item-comic-image")[0].img["src"] return '
    Peanuts
    ' % (comic_src) def check_for_starts_with_line(lst, line): for md in lst: if line.startswith(md): return True return False def get_thoreau(): print("getting thoreau") year_int = int(date.today().strftime("%Y")) - 183 year = str(year_int) year_stop = str(year_int+1) month_days = [ date.today().strftime("_%b %-d."), date.today().strftime("_%b. %-d."), date.today().strftime("_%B %-d."), date.today().strftime("_%B. %-d.") ] filename = config["thoreau"]["journal1"] with open(filename) as f: lines = f.readlines() # Find lines that the year lies on i= 0 year_start_idx = -1 for i in range(len(lines)): if lines[i].startswith(year): year_start_idx = i+1 break year_stop_idx = -1 for i in range(year_start_idx, len(lines)): if lines[i].startswith(year_stop): year_stop_idx = i - 2 break entry_start_idx = -1 # Find the lines inside the year that the date lies on i = year_start_idx while i < year_stop_idx: if check_for_starts_with_line(month_days, lines[i]): entry_start_idx = i - 2 i += 1 break i += 1 entry_end_idx = -1 while i < year_stop_idx: if lines[i].startswith("_"): entry_end_idx = i - 2 break i += 1 # If found date, join the strings if entry_start_idx != -1 and entry_end_idx != -1: return "".join(lines[entry_start_idx:entry_end_idx]) return "No Thoreau entry on " + month_days[0] + year