From 10593b1e128e37c3899eae78ac4dcce776f3ab81 Mon Sep 17 00:00:00 2001 From: Mark Powers Date: Thu, 24 Feb 2022 21:36:50 -0600 Subject: Fix old news --- on_this_day.py | 23 +++++++++++++---------- test.py | 2 +- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/on_this_day.py b/on_this_day.py index da70cd8..970cec9 100644 --- a/on_this_day.py +++ b/on_this_day.py @@ -10,16 +10,19 @@ from config import config 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" + century_ago = date.today().strftime("%d%%2F%m%%2F") + str(year) + + url = f"https://chroniclingamerica.loc.gov/search/pages/results/?state=Illinois&lccn=sn84031489&lccn=sn84031490&lccn=sn84031492&lccn=sn84025828&lccn=sn90053126&lccn=sn91055768&lccn=sn87062321&lccn=sn82014064&lccn=sn86056950&lccn=sn84025819&lccn=sn91055455&dateFilterType=range&date1={century_ago}&date2={century_ago}&language=&ortext=&andtext=&phrasetext=&proxtext=&proxdistance=5&rows=1&searchType=advanced&sequence=1" + + r = requests.get(url) + soup = BeautifulSoup(r.text, features="lxml") + try: + href = soup.select(".highlite a")[0]["href"] + link = "https://chroniclingamerica.loc.gov" + href + return f'
Old Chicago News from {year}
' + except: + pass + return "No old news" def get_today_wikipedia(): print("getting today's wikipedia") diff --git a/test.py b/test.py index e66be5d..5763e67 100644 --- a/test.py +++ b/test.py @@ -1,3 +1,3 @@ import on_this_day -print(on_this_day.get_week_holidays()) +print(on_this_day.get_old_news()) -- cgit v1.2.3