Google Calendar bot: Populate events array directly.
This commit is contained in:
parent
478785b415
commit
a74490bf1c
|
@ -107,8 +107,10 @@ def get_credentials():
|
||||||
logging.error("Run the get-google-credentials script from this directory first.")
|
logging.error("Run the get-google-credentials script from this directory first.")
|
||||||
|
|
||||||
|
|
||||||
def get_events():
|
def populate_events():
|
||||||
# type: () -> Iterable[Tuple[int, datetime.datetime, str]]
|
# type: () -> Optional[None]
|
||||||
|
global events
|
||||||
|
|
||||||
credentials = get_credentials()
|
credentials = get_credentials()
|
||||||
creds = credentials.authorize(httplib2.Http())
|
creds = credentials.authorize(httplib2.Http())
|
||||||
service = discovery.build('calendar', 'v3', http=creds)
|
service = discovery.build('calendar', 'v3', http=creds)
|
||||||
|
@ -117,6 +119,7 @@ def get_events():
|
||||||
feed = service.events().list(calendarId=options.calendarID, timeMin=now, maxResults=5,
|
feed = service.events().list(calendarId=options.calendarID, timeMin=now, maxResults=5,
|
||||||
singleEvents=True, orderBy='startTime').execute()
|
singleEvents=True, orderBy='startTime').execute()
|
||||||
|
|
||||||
|
events = []
|
||||||
for event in feed["items"]:
|
for event in feed["items"]:
|
||||||
try:
|
try:
|
||||||
start = dateutil.parser.parse(event["start"]["dateTime"])
|
start = dateutil.parser.parse(event["start"]["dateTime"])
|
||||||
|
@ -140,9 +143,9 @@ def get_events():
|
||||||
start = calendar_timezone.localize(start_naive) # type: ignore
|
start = calendar_timezone.localize(start_naive) # type: ignore
|
||||||
|
|
||||||
try:
|
try:
|
||||||
yield (event["id"], start, event["summary"])
|
events.append((event["id"], start, event["summary"]))
|
||||||
except KeyError:
|
except KeyError:
|
||||||
yield (event["id"], start, "(No Title)")
|
events.append((event["id"], start, "(No Title)"))
|
||||||
|
|
||||||
|
|
||||||
def send_reminders():
|
def send_reminders():
|
||||||
|
@ -190,7 +193,7 @@ for i in itertools.count():
|
||||||
# We check reminders every minute, but only
|
# We check reminders every minute, but only
|
||||||
# download the calendar every 10 minutes.
|
# download the calendar every 10 minutes.
|
||||||
if not i % 10:
|
if not i % 10:
|
||||||
events = list(get_events())
|
populate_events()
|
||||||
send_reminders()
|
send_reminders()
|
||||||
except:
|
except:
|
||||||
logging.exception("Couldn't download Google calendar and/or couldn't post to Zulip.")
|
logging.exception("Couldn't download Google calendar and/or couldn't post to Zulip.")
|
||||||
|
|
Loading…
Reference in a new issue