def get_event(self, calendar_id, event_id): event = self.service.events().get(calendarId = self.configured_calendar_ids[calendar_id], eventId=event_id).execute() nb = DeepNonStrictNestedBunch(event) dt = None if nb.start.dateTime: dt = dateutil.parser.parse(nb.start.dateTime) elif nb.start.date: dt = dateutil.parser.parse(nb.start.date) dt = datetime(year = dt.year, month = dt.month, day = dt.day, hour=0, minute=0, second=0, tzinfo=self.timezone) if dt: nb.datetime_o = dt nb.calendar_id = calendar_id return nb
def get_events(self, start_time, end_time, ignore_cancelled = True, get_recurring_events_as_instances = True, restrict_to_calendars = []): '''A wrapper for events().list. Returns the events from the calendar within the specified times. Some of the interesting fields are: description, end, htmlLink, location, organizer, start, summary Note: "Cancelled instances of recurring events (but not the underlying recurring event) will still be included if showDeleted and singleEvents are both False." ''' es = [] calendar_ids = restrict_to_calendars or self.calendar_ids for calendar_id in calendar_ids: now = datetime.now(tz = self.timezone) events = [] page_token = None while True: events = self.service.events().list(pageToken=page_token, maxResults = 250, calendarId = self.configured_calendar_ids[calendar_id], timeMin = start_time, timeMax = end_time, showDeleted = False).execute() for event in events['items']: dt = None nb = DeepNonStrictNestedBunch(event) assert(not(nb._event)) nb._event = event # keep the original event as returned in case we want to reuse it e.g. insert it into another calendar if (not ignore_cancelled) or (nb.status != 'cancelled'): # Ignore cancelled events if nb.recurrence: if get_recurring_events_as_instances: # Retrieve all occurrences of the recurring event within the timeframe es += self.get_recurring_events(calendar_id, nb.id, start_time, end_time) else: es.append(nb) elif nb.start.dateTime: dt = dateutil.parser.parse(nb.start.dateTime) elif nb.start.date: dt = dateutil.parser.parse(nb.start.date) dt = datetime(year = dt.year, month = dt.month, day = dt.day, hour=0, minute=0, second=0, tzinfo=self.timezone) if dt: nb.datetime_o = dt nb.calendar_id = calendar_id es.append(nb) page_token = events.get('nextPageToken') if not page_token: break es.sort(key=lambda x: x.datetime_o) return es
def get_recurring_events(self, calendar_id, event_id, start_time, end_time, maxResults = None): '''A wrapper for events().instances. Returns the list of recurring events for the given calendar alias within the specified timeframe.''' es = [] page_token = None while True: events = self.service.events().instances(calendarId = self.configured_calendar_ids[calendar_id], eventId = event_id, pageToken=page_token, timeMin = start_time, timeMax = end_time, maxResults = maxResults, showDeleted = False).execute() for event in events['items']: dt = None nb = DeepNonStrictNestedBunch(event) assert(not(nb._event)) nb._event = event # keep the original event as returned in case we want to reuse it e.g. insert it into another calendar if nb.start.date: dt = dateutil.parser.parse(nb.start.date + 'T00:00:00-08:00') elif nb.start.dateTime: dt = dateutil.parser.parse(nb.start.dateTime) nb.datetime_o = dt nb.calendar_id = calendar_id es.append(nb) page_token = events.get('nextPageToken') if not page_token: break return es
def getEventsTable(self, calendar_id, year = None, month = None): eventstbl = {} events = self.getAllEvents(calendar_id, year, month) for event in events: event = DeepNonStrictNestedBunch (event) if event.start and event.location and event.status != 'cancelled': EventTitle = event.summary if event.start.get('dateTime'): startdate = event.start['dateTime'] startdate = time.strptime(startdate[0:19], '%Y-%m-%dT%H:%M:%S') startdate = datetime.fromtimestamp(time.mktime(startdate)) elif event.start.get('date'): startdate = event.start['date'] startdate = time.strptime(startdate, '%Y-%m-%d') startdate = datetime.fromtimestamp(time.mktime(startdate)) else: raise Exception('Cannot determine start date.') if event.end.get('dateTime'): enddate = event.end['dateTime'] enddate = time.strptime(enddate[0:19], '%Y-%m-%dT%H:%M:%S') enddate = datetime.fromtimestamp(time.mktime(enddate)) elif event.end.get('date'): enddate = event.end['date'] enddate = time.strptime(enddate, '%Y-%m-%d') enddate = datetime.fromtimestamp(time.mktime(enddate)) else: raise Exception('Cannot determine end date.') isBirthday = EventTitle.find("birthday") != -1 location = event.get('location') eventstbl[(startdate, EventTitle)] = {"event": event, "enddate" : enddate, "location" : location, "title" : EventTitle} #for k in sorted(eventstbl.keys()): # print(k, eventstbl[k]["title"]) return eventstbl