From 0f9519ade58173de0b564058ff7588b471cd7c65 Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Sat, 26 Sep 2020 19:00:25 -0500 Subject: Add wikipedia, wikiquote, and calvin and hobbes links --- on_this_day.py | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 on_this_day.py (limited to 'on_this_day.py') diff --git a/on_this_day.py b/on_this_day.py new file mode 100644 index 0000000..f8abb8e --- /dev/null +++ b/on_this_day.py @@ -0,0 +1,51 @@ +import requests + +from datetime import date +from bs4 import BeautifulSoup + +from config import config + +def get_on_this_day(): + return "

On this day

%s%s%s%s" % (get_old_news(), get_calvin_and_hobbes(), get_today_wikipedia(), get_today_wikiquote()) + +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 + +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_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 "" + -- cgit v1.2.3