aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--on_this_day.py42
1 files changed, 35 insertions, 7 deletions
diff --git a/on_this_day.py b/on_this_day.py
index ff4d31d..4a2c39c 100644
--- a/on_this_day.py
+++ b/on_this_day.py
@@ -1,4 +1,6 @@
import requests
+import sys
+import subprocess
from datetime import date
from bs4 import BeautifulSoup
@@ -8,7 +10,9 @@ from config import config
def check_error(func, name):
try:
return func()
- except:
+ 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():
@@ -18,8 +22,10 @@ def get_on_this_day():
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 "<h1>On this day</h1><ul><li>%s</li><li>%s</li><li>%s</li><li>%s</li></ul><div>%s</div><div><pre>%s</pre></div>" % (old_news, peanuts, calvin_and_hobbes, wikipedia, wikiquote, thoreau)
+ return "<h1>On this day</h1><ul><li>%s</li><li>%s</li><li>%s</li><li>%s</li><li>%s</li><li>%s</li></ul><div>%s</div><div><pre>%s</pre></div>" % (old_news, peanuts, calvin_and_hobbes, wikipedia, crossword, holidays, wikiquote, thoreau)
def get_old_news():
print("getting old news")
@@ -33,13 +39,25 @@ def get_old_news():
name = names[i]
if requests.get(full_url).status_code != 404:
news_text += '<div><a href="%s">%s %s</a></div>\n' % (full_url, name, century_ago)
- return news_text
+ 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 '<div><a href="%s">Today\'s Wikipedia</a></div>' % (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 "</li><li>".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'<div><a href="{url}">Cryptic Crossword</a></div>'
+
def get_today_wikiquote():
print("getting today's wikiquote")
full_url = "https://en.wikiquote.org/wiki/%s" % date.today().strftime("%B_%d")
@@ -71,13 +89,23 @@ def get_peanuts():
comic_src = soup.select(".item-comic-image")[0].img["src"]
return '<div><a href="%s">Peanuts</a></div>' % (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_day1 = date.today().strftime("_%b %-d._")
- month_day2 = date.today().strftime("_%b. %-d._")
+ 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()
@@ -99,7 +127,7 @@ def get_thoreau():
# Find the lines inside the year that the date lies on
i = year_start_idx
while i < year_stop_idx:
- if lines[i].startswith(month_day1) or lines[i].startswith(month_day2):
+ if check_for_starts_with_line(month_days, lines[i]):
entry_start_idx = i - 2
i += 1
break
@@ -114,4 +142,4 @@ def get_thoreau():
# 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_day1 + year
+ return "No Thoreau entry on " + month_days[0] + year