updates routine script

This commit is contained in:
Raphael Maenle 2020-05-15 16:00:11 +02:00
parent 8d546e37a8
commit b5dbfd87de
2 changed files with 30 additions and 15 deletions

View File

@ -1,6 +1,7 @@
from server import db from server import db
from server.models import User from server.models import User, Calendar
from backend import caltojson from backend import caltojson
import os
import google.oauth2.credentials import google.oauth2.credentials
import json import json
@ -19,14 +20,29 @@ class Routine:
if credentials is None: if credentials is None:
continue continue
credentials = google.oauth2.credentials.Credentials(**credentials) visualCals = []
calendarjson = caltojson.generateJsonFromCalendarEntries(credentials) calendars = db.session.query(Calendar).filter(Calendar.usr_id==user.id).all()
for calendar in calendars:
if calendar.toggle == 'True':
visualCals.append(calendar.name)
googleCreds = google.oauth2.credentials.Credentials(**credentials)
calendarjson = caltojson.generateJsonFromCalendarEntries(visualCals, googleCreds)
user.setJson(calendarjson) user.setJson(calendarjson)
print(calendarjson)
with open('userinfo/'+ user.id +'/calendarevents.json', 'w', encoding='utf-8') as f: directory = 'userinfo/' + user.id + '/'
if not os.path.exists(directory):
os.makedirs(directory)
with open(directory + 'calendarevents.json', 'w', encoding='utf-8') as f:
json.dump(calendarjson, f, ensure_ascii=False, indent=4) json.dump(calendarjson, f, ensure_ascii=False, indent=4)
def credentials_to_dict(credentials):
return{'token': credentials.token,
'refresh_token': credentials.refresh_token,
'token_uri': credentials.token_uri,
'client_id': credentials.client_id,
'client_secret': credentials.client_secret,
'scopes': credentials.scopes}
if __name__ == "__main__": if __name__ == "__main__":
routine = Routine() routine = Routine()

View File

@ -12,7 +12,6 @@ from google.auth.transport.requests import Request
SCOPES = ["https://www.googleapis.com/auth/userinfo.email", "https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/calendar.readonly", "openid"] 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_SERVICE_NAME = 'calendar'
API_VERSION = 'v3' API_VERSION = 'v3'
visibleList = ['Hightower', 'Home', 'Office', 'Life', 'Social', 'Grey']
class Event: class Event:
def __init__(self, name_, start_, end_): def __init__(self, name_, start_, end_):
@ -116,17 +115,17 @@ def getCalendars(service):
return calendars return calendars
def purgeCalendars(calendars): def purgeCalendars(calendars, visibleCalendars):
purged = [] purged = []
for calendar in calendars: for calendar in calendars:
if calendar.summary in visibleList: if calendar.summary in visibleCalendars:
purged.append(calendar) purged.append(calendar)
return purged return purged
def getCalendarEvents(service, startDate, endDate): def getCalendarEvents(service, visibleCalendars, startDate, endDate):
calendars = getCalendars(service) calendars = getCalendars(service)
calendars = purgeCalendars(calendars) calendars = purgeCalendars(calendars, visibleCalendars)
all_events = [] all_events = []
@ -237,7 +236,7 @@ def getCalendarList(credentials = None):
calendar.color = fromColorIdGetColor(calendar.color, colormap, colors) calendar.color = fromColorIdGetColor(calendar.color, colormap, colors)
return calendars return calendars
def generateJsonFromCalendarEntries(credentials = None): def generateJsonFromCalendarEntries(visibleCalendars, credentials = None):
# create service # create service
if credentials == None: if credentials == None:
@ -249,7 +248,7 @@ def generateJsonFromCalendarEntries(credentials = None):
today = now.replace(hour=0, minute=0, second = 0).isoformat() today = now.replace(hour=0, minute=0, second = 0).isoformat()
tomorrow = now.replace(hour=23, minute=59, second=59).isoformat() # + '+01:00' tomorrow = now.replace(hour=23, minute=59, second=59).isoformat() # + '+01:00'
allEvents = getCalendarEvents(service, today, tomorrow) allEvents = getCalendarEvents(service, visibleCalendars, today, tomorrow)
colorizeEvents(allEvents, service) colorizeEvents(allEvents, service)
# if not events: # if not events: