From 5a0d7159293c130a4081d362d4e9e1ca16c539a3 Mon Sep 17 00:00:00 2001 From: raphael Date: Sun, 15 Mar 2020 14:49:31 +0100 Subject: [PATCH] created color correction using a .json file and remapping the colors from the api to the colors actually shown in the google calendar online visualization --- colors.json | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++ quickstart.py | 56 +++++++++++++++---- token.pickle | Bin 752 -> 752 bytes 3 files changed, 193 insertions(+), 9 deletions(-) create mode 100644 colors.json diff --git a/colors.json b/colors.json new file mode 100644 index 0000000..36a36e6 --- /dev/null +++ b/colors.json @@ -0,0 +1,146 @@ +{ + "colors": [ + { + "api": "#ac725e", + "natural": "#795548" + }, + { + "api": "#d06b64", + "natural": "#9331C4" + }, + { + "api": "#f83a22", + "natural": "#8E24AA" + }, + { + "api": "#fa573c", + "natural": "#E67C73" + }, + { + "api": "#ff7537", + "natural": "#EF6C00" + }, + { + "api": "#ffad46", + "natural": "#F09300" + }, + { + "api": "#42d692", + "natural": "#009688" + }, + { + "api": "#16a765", + "natural": "#0B8043" + }, + { + "api": "#7bd148", + "natural": "#7CB342" + }, + { + "api": "#b3dc6c", + "natural": "#C0CA33" + }, + { + "api": "#fbe983", + "natural": "#E4C441" + }, + { + "api": "#fad165", + "natural": "#F6BF26" + }, + { + "api": "#92e1c0", + "natural": "#33B679" + }, + { + "api": "#9fe1e7", + "natural": "#039BE5" + }, + { + "api": "#9fc6e7", + "natural": "#4285F4" + }, + { + "api": "#4986e7", + "natural": "#3F51B5" + }, + { + "api": "#9a9cff", + "natural": "#7986CB" + }, + { + "api": "#b99aff", + "natural": "#B39DDB" + }, + { + "api": "#c2c2c2", + "natural": "#616161" + }, + { + "api": "#cabdbf", + "natural": "#A79B8E" + }, + { + "api": "#cca6ac", + "natural": "#AD1457" + }, + { + "api": "#f691b2", + "natural": "#D81B60" + }, + { + "api": "#cd74e6", + "natural": "#8E24AA" + }, + { + "api": "#a47ae2", + "natural": "#9E69AF" + } + ], + "eventRemap": [ + { + "from": "11", + "to": "3" + }, + { + "from": "4", + "to": "2" + }, + { + "from": "6", + "to": "4" + }, + { + "from": "5", + "to": "12" + }, + { + "from": "7", + "to": "14" + }, + { + "from": "9", + "to": "16" + }, + { + "from": "1", + "to": "17" + }, + { + "from": "3", + "to": "23" + }, + { + "from": "8", + "to": "19" + }, + { + "from": "2", + "to": "18" + }, + { + "from": "10", + "to": "8" + } + ] +} \ No newline at end of file diff --git a/quickstart.py b/quickstart.py index 9bdd8b5..997744c 100644 --- a/quickstart.py +++ b/quickstart.py @@ -11,12 +11,14 @@ 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'] +visibleList = ['Hightower', 'Home', 'Office', 'Life', 'Social', 'Grey'] class Event: - def __init__(self, name_, colorId_, start_, end_): + def __init__(self, name_, start_, end_): self.name = name_ - self.colorId = colorId_ + self.eventColorId = 0 + self.calendarColorId = 0 + self.naturalColorId = 0 self.start = start_ self.end = end_ self.colorHex = '#adfff5' @@ -56,7 +58,7 @@ class Event: if self.allDay: print(self.name + "All Day") else: - print(self.name + ": " + self.start + ", " + self.end) + print(self.name + ": " + self.NaturalColorId) class Calendar: @@ -109,7 +111,6 @@ def getCalendars(service): def purgeCalendars(calendars): purged = [] for calendar in calendars: - print(calendar.summary) if calendar.summary in visibleList: purged.append(calendar) return purged @@ -129,22 +130,54 @@ def getCalendarEvents(service, startDate, endDate): for event in events_result.get('items', []): + # create simple event name = event['summary'] start = event['start'].get('dateTime') end = event['end'].get('dateTime') + newEvent = Event(name, start, end) + + # handle weird colors from google color = event.get('colorId') if color == None: - color = calendar.color + newEvent.calendarColorId = calendar.color + else: + newEvent.eventColorId = color - event = Event(name, color, start, end) - all_events.append(event) + + all_events.append(newEvent) return all_events +def toCalendarColorId(colormap, colorId): + for remap in colormap['eventRemap']: + if remap['from'] == colorId: + return remap['to'] + + print(f"failed with {colorId}") + return colorId + +def toNaturalColor(colormap, orgColor): + for remap in colormap['colors']: + if remap['api'] == orgColor: + return remap['natural'] + + print(f"failed with {orgColor}") + return orgColor + def colorizeEvents(allEvents, colors): + + with open("colors.json") as f: + colormap = json.load(f) + for event in allEvents: - event.colorHex = colors['calendar'][event.colorId]['background'] + if event.calendarColorId != 0: + orgColor = colors['calendar'][event.calendarColorId]['background'] + else: + calColorId = toCalendarColorId(colormap, event.eventColorId) + orgColor = colors['calendar'][calColorId]['background'] + event.colorHex = toNaturalColor(colormap, orgColor) + def toJson(events): data = {} data['kind'] = 'calendar#events' @@ -153,6 +186,7 @@ def toJson(events): for event in events: if event.allDay: continue + data['events'].append({ 'name': event.name, 'isAllDay': event.allDay, @@ -164,6 +198,10 @@ def toJson(events): with open('./calendarevents.json', 'w') as outfile: json.dump(data, outfile) +def printColors(colors): + for i in range(1, 25): + col = colors['event'][str(i)]['background'] + print(f"{i}: {col}") def main(): service = calendarCredentials() diff --git a/token.pickle b/token.pickle index 904d95e6b911d441acb7aad106e68d5b13b0df32..58158b4824a8e8e9a1f4a79cd97671d374c0271b 100644 GIT binary patch delta 181 zcmeys`hj&qP<^OTYH_x1Seappe}1aJv0+46rhiFZiknkXmPJ)=j%P|me6f!~n2(`< zX=G54Wl>UAZl0;PbE$Kcab>u>NpX^^ZdI_qWuZl+ZeU<`S$cN5d7g_wSh{I>j!AMs zVQ54_zGGpOk4tHBfQL&;NK|5^lTW@=L0G(5Mp$@(hn#_{S7BjJL2+5RXG&nXdya=! ha9((bmvMl5qHks5#P^!){CpZ#oJ%=2%QIeN1OU6YK12Wj 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