kobo-wall-calendar/kobo_wall_calendar/google_calendar.py

33 lines
840 B
Python
Raw Permalink Normal View History

2023-09-17 20:35:27 +00:00
import functools
import requests_cache
from ical.calendar_stream import IcsCalendarStream
import datetime
import logging
logger = logging.getLogger(__name__)
2024-07-17 14:59:18 +00:00
SESSION = requests_cache.CachedSession('output/calendar')
2023-09-17 20:35:27 +00:00
@functools.cache
2024-07-17 13:41:01 +00:00
def get_events_for_month(ical_link: str, today: datetime.date):
2023-09-17 20:35:27 +00:00
logger.warning('Downloading ical from Google')
response = SESSION.get(ical_link)
assert response.status_code == 200
logger.warning('Loading ical')
calendar = IcsCalendarStream.calendar_from_ics(response.text)
today = datetime.date.today()
logger.warning('Iterating ical')
date = today.replace(day = 1)
stuff = []
while date.month == today.month:
for e in calendar.timeline.on_date(date):
stuff.append((date, e))
date += datetime.timedelta(days = 1)
return stuff