From 1cd0a7d6f9d5470b5afd8a030d7300513b727962 Mon Sep 17 00:00:00 2001 From: Eric Wang Date: Sun, 17 Mar 2019 13:35:35 -0400 Subject: [PATCH] fix 3 year calendar --- penn/calendar3year.py | 44 +++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/penn/calendar3year.py b/penn/calendar3year.py index c8d7fe2..ff4f81b 100644 --- a/penn/calendar3year.py +++ b/penn/calendar3year.py @@ -2,7 +2,7 @@ import datetime -BASE_URL = "https://www.google.com/calendar/ical/pennalmanac@gmail.com/public/basic.ics" +BASE_URL = "https://www.stanza.co/api/schedules/almanacacademiccalendar/" class Calendar(object): @@ -20,25 +20,29 @@ def pull_3year(self): - End date 'end' """ events = [] - r = requests.get(BASE_URL).text - lines = r.split("\r\n") - d = {} - for line in lines: - if line == "BEGIN:VEVENT": - d = {} - elif line[:7] == "DTSTART": - raw_date = line.split(":")[1] - start_date = datetime.datetime.strptime(raw_date, '%Y%m%d').date() - d['start'] = start_date.strftime('%Y-%m-%d') - elif line[:5] == "DTEND": - raw_date = line.split(":")[1] - end_date = datetime.datetime.strptime(raw_date, '%Y%m%d').date() - d['end'] = end_date.strftime('%Y-%m-%d') - elif line[:7] == "SUMMARY": - name = line.split(":")[1] - d['name'] = str(name).strip() - elif line == "END:VEVENT": - events.append(d) + for term in ["fall", "summer", "spring"]: + url = "{}{}{}term.ics".format(BASE_URL, datetime.datetime.now().year, term) + resp = requests.get(url) + resp.raise_for_status() + r = resp.text + lines = r.split("\n") + d = {} + for line in lines: + if line == "BEGIN:VEVENT": + d = {} + elif line.startswith("DTSTART"): + raw_date = line.split(":")[1] + start_date = datetime.datetime.strptime(raw_date, '%Y%m%d').date() + d['start'] = start_date.strftime('%Y-%m-%d') + elif line.startswith("DTEND"): + raw_date = line.split(":")[1] + end_date = datetime.datetime.strptime(raw_date, '%Y%m%d').date() + d['end'] = end_date.strftime('%Y-%m-%d') + elif line.startswith("SUMMARY"): + name = line.split(":")[1] + d['name'] = str(name).strip() + elif line == "END:VEVENT": + events.append(d) events.sort(key=lambda d: d['start']) return events