From 1bd2dcd4600ec10d9bab4329e3b49fd82e73633d Mon Sep 17 00:00:00 2001 From: raphael Date: Fri, 13 Mar 2020 21:46:14 +0100 Subject: [PATCH] adds some color to the mix --- __pycache__/quickstart.cpython-36.pyc | Bin 0 -> 4240 bytes calendars.json | 1 + quickstart.py | 127 +++++++++++++++++++++----- token.pickle | Bin 752 -> 752 bytes 4 files changed, 107 insertions(+), 21 deletions(-) create mode 100644 __pycache__/quickstart.cpython-36.pyc create mode 100644 calendars.json diff --git a/__pycache__/quickstart.cpython-36.pyc b/__pycache__/quickstart.cpython-36.pyc new file mode 100644 index 0000000000000000000000000000000000000000..325e258e3ca7d6a03f7e113ee5a91a8e028be152 GIT binary patch literal 4240 zcmbVPTaOz_74E9OxZTtCcqWs}gs_A}TGs613EB$~g(V>u2!bIbk%EearsHbQwA1eP zRJSuTR{P2O8X;a*AVoayO8g+yuRP^1@B-hdwmp+U%LBIR)T#TaQ|El=^oOmM_wTijF@1Ic%E>;A#aP-tb zSeP4pWvDhJ`q%qA``IYk$yAu`MLRNjxw97+E0ygWOyd3vy{54!w2KO6M0a6&f{^NBRQ?oeue&&Stw^Q>|gV@`ibqK2X`{-c!{? z$Ja>F(ptC|7SYpq6nzsDa|eVJV6o}B`r>cR`Tu27o7h-+AYUVlpBLG9Da$p?J1xuR zQkIh`eXTXjBi)l(>WBCQOaY|~dCBH%#!EglQ7jZ&ve(2rC=C=>^4IW&*L-G{Ce&v^ zeU)__g|NU;mKOV!DPei&+s`Jdaz=4FDWb~BqkfjkE{6+2bHXa1yKb{`sWmC$L^$J6 z<&hG0U7v?Yik?kfByx$!HW7OJuBGh*yB5HTJz?gusA%KxjLB&IeFrP@4v4{g)50UU znzqepr&iF(yTA2PCRS+m{1A+`o(qMx8_6yjb6{%v&Z5mP(ztmM*r_?I9@oBV!M5)t z$yNB{l;6BMQ4myq5FAXxq#jYE2em=u91+A1qc$=+;FR{rrxkgfBIBl3PA=&(cg;0j8~=_T z>7#S?BjdV_lGC>T4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Wi0A8h*&4Ypd7YWr>^Q7H1s|Z`jo_z<`^mV6(yo#d-Y{n$F81?^L~ct zbgTo@ay+0f)*vIA#Ls&ao$9#vPQAWIBfSD&YdJ+?eA4QV;Jl8I-=o7Ranr!0b$p^F weXly(Iq`qZQGBZ0TXpLADZN-u$&F8zw)j(^^G$g8jkfJxa<92t?uN_$10sdOumAu6 literal 0 HcmV?d00001 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 9a6227501b67a9e424ec74b472a79000c4a82343..904d95e6b911d441acb7aad106e68d5b13b0df32 100644 GIT binary patch delta 181 zcmeys`hj&qP`$Z@Z@Fbrah64*k-1@BzFR<5j+>=#uveLPlyQ_>vVXX@lZBg`nO|78 zTR@~qT6%n#k(WofnSpO|NqUN7nXzZ3X+XM(u9I11NPvfTnx92(Ws;|#p<79MP`<87 zieq+)hgo1jmXBkacSKc%TUCL(qgP>hfm32`ykSn2mz+_cm!o5)d8vDucZG?6UTAPs hNLWEyQJGIcWO1nT#P^!)yrS}&3~9le|$rBjBXm#Jq)QF^?t zPiU!IhGCSeN12JMYgu`se?efnrLV6?WMxK0pj$