From fed3fddb2182d27a3bd832f354e0ec1b9ac61667 Mon Sep 17 00:00:00 2001 From: raphael Date: Wed, 15 Apr 2020 17:15:13 +0000 Subject: [PATCH] adds more individualized calls from frontend --- caltojson.py | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/caltojson.py b/caltojson.py index 8799267..ed60051 100644 --- a/caltojson.py +++ b/caltojson.py @@ -9,8 +9,9 @@ from google_auth_oauthlib.flow import InstalledAppFlow from google.auth.transport.requests import Request # If modifying these scopes, delete the file token.pickle. -SCOPES = ['https://www.googleapis.com/auth/calendar.readonly'] - +SCOPES = ["https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/calendar.readonly", "openid"] +API_SERVICE_NAME = 'calendar' +API_VERSION = 'v3' visibleList = ['Hightower', 'Home', 'Office', 'Life', 'Social', 'Grey'] class Event: @@ -173,7 +174,8 @@ def toNaturalColor(colormap, orgColor): def colorizeEvents(allEvents, colors): - with open("colors.json") as f: + COLORS_FILE = os.path.join(os.path.dirname(__file__), 'colors.json') + with open(COLORS_FILE) as f: colormap = json.load(f) for event in allEvents: @@ -202,8 +204,7 @@ def toJson(events): 'stopDateTime': event.stopDateTime() }) - with open('./calendarevents.json', 'w') as outfile: - json.dump(data, outfile) + return data def printColors(colors): for i in range(1, 25): @@ -213,9 +214,29 @@ def printColors(colors): def main(): generateJsonFromCalendarEntries() -def generateJsonFromCalendarEntries(token = None): - service = calendarCredentials(token) +def getCalendarList(credentials = None): + # create service + if credentials == None: + credentials = calendarCredentials() + service = build(API_SERVICE_NAME, API_VERSION, credentials=credentials) + + calendars = getCalendars(service) + COLORS_FILE = os.path.join(os.path.dirname(__file__), 'colors.json') + with open(COLORS_FILE) as f: + colormap = json.load(f) + + for calendar in calendars: + calendar.color = toNaturalColor(colormap, calendar.color) + return calendars + +def generateJsonFromCalendarEntries(credentials = None): + + # create service + if credentials == None: + credentials = calendarCredentials() + service = build(API_SERVICE_NAME, API_VERSION, credentials=credentials) + # define today and tomorrow now = datetime.datetime.now(datetime.timezone.utc).astimezone() today = now.replace(hour=0, minute=0, second = 0).isoformat() @@ -228,7 +249,7 @@ def generateJsonFromCalendarEntries(token = None): # if not events: # print('No upcoming events found.') - toJson(allEvents) + return toJson(allEvents) if __name__ == '__main__': main()