From 0e58fb890c9b9afe5e438f97c29b6334c0164eda Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Wed, 21 Oct 2020 21:54:23 -0500 Subject: Add Thoreau to on this day --- on_this_day.py | 46 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) (limited to 'on_this_day.py') diff --git a/on_this_day.py b/on_this_day.py index ea9e1e5..ca0f8e5 100644 --- a/on_this_day.py +++ b/on_this_day.py @@ -17,8 +17,9 @@ def get_on_this_day(): 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") - return "

On this day

%s" % (old_news, peanuts, calvin_and_hobbes, wikipedia, wikiquote) + return "

On this day

%s
%s
" % (old_news, peanuts, calvin_and_hobbes, wikipedia, wikiquote, thoreau) def get_old_news(): print("getting old news") @@ -70,3 +71,46 @@ def get_peanuts(): comic_src = soup.select(".item-comic-image")[0].img["src"] return '
Peanuts
' % (comic_src) +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._") + with open("journal1.txt") 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 lines[i].startswith(month_day1) or lines[i].startswith(month_day2): + 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_day1 + year -- cgit v1.2.3