diff --git a/__pycache__/quickstart.cpython-36.pyc b/__pycache__/quickstart.cpython-36.pyc new file mode 100644 index 0000000..325e258 Binary files /dev/null and b/__pycache__/quickstart.cpython-36.pyc differ diff --git a/calendars.json b/calendars.json new file mode 100644 index 0000000..4306185 --- /dev/null +++ b/calendars.json @@ -0,0 +1 @@ +{"calendars": [{"name": "Todoist"}, {"name": "https://intranet.fhwn.ac.at/ics/Default.aspx?FHTimetable.ics&id=YUWSjNJUx5I%3d&datediffmonths=1&replaceSpecialChars=1"}, {"name": "Hightower"}, {"name": "Home \ud83c\udfe0"}, {"name": "D-ARIA"}, {"name": "Office"}, {"name": "http://followshows.com/ical/AS3Bbq0q"}, {"name": "Grey"}, {"name": "Privat"}, {"name": "Work"}, {"name": "Social"}, {"name": "Life"}, {"name": "https://poledancevienna.at/mypdv/ical/XJSY-56GQ-G2VC"}, {"name": "Contacts"}, {"name": "Holidays in Austria"}, {"name": "Week Numbers"}]} \ No newline at end of file diff --git a/quickstart.py b/quickstart.py index d4a28e8..9bdd8b5 100644 --- a/quickstart.py +++ b/quickstart.py @@ -1,6 +1,8 @@ from __future__ import print_function import datetime +import dateutil.parser import pickle +import json import os.path from googleapiclient.discovery import build from google_auth_oauthlib.flow import InstalledAppFlow @@ -9,15 +11,57 @@ from google.auth.transport.requests import Request # If modifying these scopes, delete the file token.pickle. SCOPES = ['https://www.googleapis.com/auth/calendar.readonly'] +visibleList = ['Hightower', 'Home \ud83c\udfe0', 'Office', 'Life', 'Social', 'Grey'] + class Event: - def __init__(self, name_, color_, start_, end_): + def __init__(self, name_, colorId_, start_, end_): self.name = name_ - self.color = color_ + self.colorId = colorId_ self.start = start_ self.end = end_ + self.colorHex = '#adfff5' + + if self.start == None or self.end == None : + self.allDay = True + else: + self.allDay = False + + def startDateTime(self): + if self.allDay: + return None + return self.jsonFromDT(self.start) + + def stopDateTime(self): + if self.allDay: + return None + return self.jsonFromDT(self.end) + + def jsonFromDT(self, string): + sdt = dateutil.parser.parse(string) + data = { + 'date': { + 'year': sdt.year, + 'month': sdt.month, + 'day': sdt.day + }, + 'time': { + 'hour': sdt.hour, + 'minute': sdt.minute, + 'second': sdt.second + } + } + return data + + def print(self): + if self.allDay: + print(self.name + "All Day") + else: + print(self.name + ": " + self.start + ", " + self.end) + class Calendar: - def __init__(self, calendarId_, color_): + def __init__(self, summary_, calendarId_, color_): + self.summary = summary_ self.calendarId = calendarId_ self.color = color_ @@ -45,17 +89,36 @@ def calendarCredentials(): return service -def getCalendarEvents(service, startDate, endDate): +def getCalendarColors(service): + colors = service.colors().get().execute() + return colors + +def getCalendars(service): page_token = None calendars = [] while True: calendar_list = service.calendarList().list(pageToken=page_token).execute() - for calendar_list_entry in calendar_list['items']: - calendars.append(Calendar(calendar_list_entry['id'], calendar_list_entry['colorId'])) + for calendar in calendar_list['items']: + calendars.append(Calendar(calendar['summary'], calendar['id'], calendar['colorId'])) page_token = calendar_list.get('nextPageToken') if not page_token: break + return calendars + +def purgeCalendars(calendars): + purged = [] + for calendar in calendars: + print(calendar.summary) + if calendar.summary in visibleList: + purged.append(calendar) + return purged + +def getCalendarEvents(service, startDate, endDate): + + calendars = getCalendars(service) + calendars = purgeCalendars(calendars) + all_events = [] for calendar in calendars: @@ -73,29 +136,51 @@ def getCalendarEvents(service, startDate, endDate): if color == None: color = calendar.color - all_events.append(Event(name, color, start, end)) + event = Event(name, color, start, end) + all_events.append(event) return all_events +def colorizeEvents(allEvents, colors): + for event in allEvents: + event.colorHex = colors['calendar'][event.colorId]['background'] + +def toJson(events): + data = {} + data['kind'] = 'calendar#events' + data['events'] = [] + + for event in events: + if event.allDay: + continue + data['events'].append({ + 'name': event.name, + 'isAllDay': event.allDay, + 'color': event.colorHex, + 'startDateTime': event.startDateTime(), + 'stopDateTime': event.stopDateTime() + }) + + with open('./calendarevents.json', 'w') as outfile: + json.dump(data, outfile) + + def main(): - service = calendarCredentials() - # Call the Calendar API - now = datetime.datetime.now(datetime.timezone.utc).astimezone() - now = now.replace(hour=0, minute=0) - today = now.isoformat() # + '+01:00' # 'Z' indicates UTC time - print("today: ") - print(today) - # one_day = datetime.timedelta(days=1) - tomorrow = (now.replace(hour=23, minute=0, second=1)).isoformat() # + '+01:00' - print("tomorrow: ") - print(tomorrow) - all_events = getCalendarEvents(service, today, tomorrow) + # define today and tomorrow + now = datetime.datetime.now(datetime.timezone.utc).astimezone() + today = now.replace(hour=0, minute=0, second = 0).isoformat() + tomorrow = now.replace(hour=23, minute=59, second=59).isoformat() # + '+01:00' + + allEvents = getCalendarEvents(service, today, tomorrow) + + colors = getCalendarColors(service) + colorizeEvents(allEvents, colors) # if not events: # print('No upcoming events found.') - for event in all_events: - print(event.name + ": " + event.start + ", " + event.color) + + toJson(allEvents) if __name__ == '__main__': diff --git a/token.pickle b/token.pickle index 9a62275..904d95e 100644 Binary files a/token.pickle and b/token.pickle differ