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.models import User
from server.models import User, Calendar
from backend import caltojson
import os
import google.oauth2.credentials
import json
@ -19,14 +20,29 @@ class Routine:
if credentials is None:
continue
credentials = google.oauth2.credentials.Credentials(**credentials)
calendarjson = caltojson.generateJsonFromCalendarEntries(credentials)
visualCals = []
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)
with open('userinfo/'+ user.id +'/calendarevents.json', 'w', encoding='utf-8') as f:
print(calendarjson)
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)
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__":
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"]
API_SERVICE_NAME = 'calendar'
API_VERSION = 'v3'
visibleList = ['Hightower', 'Home', 'Office', 'Life', 'Social', 'Grey']
class Event:
def __init__(self, name_, start_, end_):
@ -116,17 +115,17 @@ def getCalendars(service):
return calendars
def purgeCalendars(calendars):
def purgeCalendars(calendars, visibleCalendars):
purged = []
for calendar in calendars:
if calendar.summary in visibleList:
if calendar.summary in visibleCalendars:
purged.append(calendar)
return purged
def getCalendarEvents(service, startDate, endDate):
def getCalendarEvents(service, visibleCalendars, startDate, endDate):
calendars = getCalendars(service)
calendars = purgeCalendars(calendars)
calendars = purgeCalendars(calendars, visibleCalendars)
all_events = []
@ -237,7 +236,7 @@ def getCalendarList(credentials = None):
calendar.color = fromColorIdGetColor(calendar.color, colormap, colors)
return calendars
def generateJsonFromCalendarEntries(credentials = None):
def generateJsonFromCalendarEntries(visibleCalendars, credentials = None):
# create service
if credentials == None:
@ -249,7 +248,7 @@ def generateJsonFromCalendarEntries(credentials = None):
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)
allEvents = getCalendarEvents(service, visibleCalendars, today, tomorrow)
colorizeEvents(allEvents, service)
# if not events: