kobo-wall-calendar/kobo_wall_calendar/google_calendar.py

33 lines
840 B
Python

import functools
import requests_cache
from ical.calendar_stream import IcsCalendarStream
import datetime
import logging
logger = logging.getLogger(__name__)
SESSION = requests_cache.CachedSession('output/calendar')
@functools.cache
def get_events_for_month(ical_link: str, today: datetime.date):
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