2020-03-08 22:28:45 +01:00
|
|
|
from __future__ import print_function
|
|
|
|
import datetime
|
2020-03-13 21:46:14 +01:00
|
|
|
import dateutil.parser
|
2020-03-08 22:28:45 +01:00
|
|
|
import pickle
|
2020-03-13 21:46:14 +01:00
|
|
|
import json
|
2020-03-08 22:28:45 +01:00
|
|
|
import os.path
|
|
|
|
from googleapiclient.discovery import build
|
|
|
|
from google_auth_oauthlib.flow import InstalledAppFlow
|
|
|
|
from google.auth.transport.requests import Request
|
|
|
|
|
|
|
|
# If modifying these scopes, delete the file token.pickle.
|
2020-04-15 19:15:13 +02:00
|
|
|
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'
|
2020-03-13 21:46:14 +01:00
|
|
|
|
2020-03-12 00:08:30 +01:00
|
|
|
class Event:
|
2020-03-15 14:49:31 +01:00
|
|
|
def __init__(self, name_, start_, end_):
|
2020-03-12 00:08:30 +01:00
|
|
|
self.name = name_
|
2020-03-15 14:49:31 +01:00
|
|
|
self.eventColorId = 0
|
|
|
|
self.calendarColorId = 0
|
|
|
|
self.naturalColorId = 0
|
2020-03-12 00:08:30 +01:00
|
|
|
self.start = start_
|
|
|
|
self.end = end_
|
2020-03-13 21:46:14 +01:00
|
|
|
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:
|
2020-04-02 14:24:13 +02:00
|
|
|
print(self.name + ": " + self.naturalColorId)
|
2020-03-13 21:46:14 +01:00
|
|
|
|
2020-03-12 00:08:30 +01:00
|
|
|
|
|
|
|
class Calendar:
|
2020-03-13 21:46:14 +01:00
|
|
|
def __init__(self, summary_, calendarId_, color_):
|
|
|
|
self.summary = summary_
|
2020-03-12 00:08:30 +01:00
|
|
|
self.calendarId = calendarId_
|
|
|
|
self.color = color_
|
2020-03-08 22:28:45 +01:00
|
|
|
|
2020-04-02 14:24:13 +02:00
|
|
|
def calendarCredentials(token = None):
|
2020-03-08 22:28:45 +01:00
|
|
|
creds = None
|
|
|
|
# The file token.pickle stores the user's access and refresh tokens, and is
|
|
|
|
# created automatically when the authorization flow completes for the first
|
|
|
|
# time.
|
2020-04-02 14:24:13 +02:00
|
|
|
|
|
|
|
if token == None:
|
|
|
|
if os.path.exists('token.pickle'):
|
|
|
|
with open('token.pickle', 'rb') as token:
|
|
|
|
token.seek(0)
|
|
|
|
creds = pickle.load(token)
|
|
|
|
|
|
|
|
else:
|
|
|
|
creds = pickle.load(token)
|
|
|
|
|
2020-03-08 22:28:45 +01:00
|
|
|
# If there are no (valid) credentials available, let the user log in.
|
|
|
|
if not creds or not creds.valid:
|
|
|
|
if creds and creds.expired and creds.refresh_token:
|
|
|
|
creds.refresh(Request())
|
|
|
|
else:
|
|
|
|
flow = InstalledAppFlow.from_client_secrets_file(
|
|
|
|
'credentials.json', SCOPES)
|
2020-04-02 14:24:13 +02:00
|
|
|
creds = flow.run_local_server(port=1234)
|
2020-03-08 22:28:45 +01:00
|
|
|
# Save the credentials for the next run
|
|
|
|
with open('token.pickle', 'wb') as token:
|
|
|
|
pickle.dump(creds, token)
|
|
|
|
|
|
|
|
service = build('calendar', 'v3', credentials=creds)
|
2020-03-09 23:54:46 +01:00
|
|
|
|
|
|
|
return service
|
|
|
|
|
2020-03-13 21:46:14 +01:00
|
|
|
def getCalendarColors(service):
|
|
|
|
colors = service.colors().get().execute()
|
|
|
|
return colors
|
|
|
|
|
|
|
|
def getCalendars(service):
|
2020-03-08 22:28:45 +01:00
|
|
|
page_token = None
|
2020-03-12 00:08:30 +01:00
|
|
|
calendars = []
|
2020-03-08 22:28:45 +01:00
|
|
|
while True:
|
|
|
|
calendar_list = service.calendarList().list(pageToken=page_token).execute()
|
2020-03-13 21:46:14 +01:00
|
|
|
for calendar in calendar_list['items']:
|
|
|
|
calendars.append(Calendar(calendar['summary'], calendar['id'], calendar['colorId']))
|
2020-03-08 22:28:45 +01:00
|
|
|
page_token = calendar_list.get('nextPageToken')
|
|
|
|
if not page_token:
|
|
|
|
break
|
|
|
|
|
2020-03-13 21:46:14 +01:00
|
|
|
return calendars
|
|
|
|
|
2020-05-15 16:00:11 +02:00
|
|
|
def purgeCalendars(calendars, visibleCalendars):
|
2020-03-13 21:46:14 +01:00
|
|
|
purged = []
|
|
|
|
for calendar in calendars:
|
2020-05-18 23:49:26 +02:00
|
|
|
if calendar.calendarId in visibleCalendars:
|
2020-03-13 21:46:14 +01:00
|
|
|
purged.append(calendar)
|
|
|
|
return purged
|
|
|
|
|
2020-05-15 16:00:11 +02:00
|
|
|
def getCalendarEvents(service, visibleCalendars, startDate, endDate):
|
2020-03-13 21:46:14 +01:00
|
|
|
|
|
|
|
calendars = getCalendars(service)
|
2020-05-15 16:00:11 +02:00
|
|
|
calendars = purgeCalendars(calendars, visibleCalendars)
|
2020-03-13 21:46:14 +01:00
|
|
|
|
2020-03-09 23:54:46 +01:00
|
|
|
all_events = []
|
2020-03-08 22:28:45 +01:00
|
|
|
|
2020-03-12 00:08:30 +01:00
|
|
|
for calendar in calendars:
|
|
|
|
events_result = service.events().list(calendarId=calendar.calendarId, timeMin=startDate,
|
2020-03-09 23:54:46 +01:00
|
|
|
timeMax=endDate,
|
2020-03-08 22:28:45 +01:00
|
|
|
maxResults=10, singleEvents=True,
|
2020-03-12 00:08:30 +01:00
|
|
|
orderBy='startTime').execute()
|
|
|
|
|
|
|
|
for event in events_result.get('items', []):
|
|
|
|
|
2020-03-15 14:49:31 +01:00
|
|
|
# create simple event
|
2020-05-17 22:58:13 +02:00
|
|
|
name = event.get('summary', '(no Title)')
|
2020-03-12 00:08:30 +01:00
|
|
|
start = event['start'].get('dateTime')
|
|
|
|
end = event['end'].get('dateTime')
|
2020-03-15 14:49:31 +01:00
|
|
|
newEvent = Event(name, start, end)
|
|
|
|
|
|
|
|
# handle weird colors from google
|
2020-03-12 00:08:30 +01:00
|
|
|
color = event.get('colorId')
|
|
|
|
if color == None:
|
2020-03-15 14:49:31 +01:00
|
|
|
newEvent.calendarColorId = calendar.color
|
|
|
|
else:
|
|
|
|
newEvent.eventColorId = color
|
2020-03-12 00:08:30 +01:00
|
|
|
|
2020-03-15 14:49:31 +01:00
|
|
|
|
|
|
|
all_events.append(newEvent)
|
2020-03-09 23:54:46 +01:00
|
|
|
|
|
|
|
return all_events
|
|
|
|
|
2020-03-15 14:49:31 +01:00
|
|
|
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
|
|
|
|
|
2020-04-15 19:43:52 +02:00
|
|
|
def colorizeEvents(allEvents, service):
|
2020-04-15 19:15:13 +02:00
|
|
|
COLORS_FILE = os.path.join(os.path.dirname(__file__), 'colors.json')
|
|
|
|
with open(COLORS_FILE) as f:
|
2020-03-15 14:49:31 +01:00
|
|
|
colormap = json.load(f)
|
2020-04-15 19:43:52 +02:00
|
|
|
|
|
|
|
colors = getCalendarColors(service)
|
2020-03-15 14:49:31 +01:00
|
|
|
|
2020-03-13 21:46:14 +01:00
|
|
|
for event in allEvents:
|
2020-05-17 22:58:13 +02:00
|
|
|
event.colorHex = forEventGetColor(event, colormap, colors)
|
2020-04-15 19:43:52 +02:00
|
|
|
|
2020-05-17 22:58:13 +02:00
|
|
|
# this function is only used once for calendar generation stuff
|
|
|
|
def fromColorIdGetColor(color, colormap, colors):
|
|
|
|
return toNaturalColor(colormap, colors['calendar'][color]['background'])
|
2020-04-15 19:43:52 +02:00
|
|
|
|
2020-05-17 22:58:13 +02:00
|
|
|
def forEventGetColor(event, colormap, colors):
|
|
|
|
if event.calendarColorId != 0:
|
|
|
|
bg = colors['calendar'][event.calendarColorId]['background']
|
2020-04-15 19:43:52 +02:00
|
|
|
else:
|
2020-05-17 22:58:13 +02:00
|
|
|
calColorId = toCalendarColorId(colormap, event.eventColorId)
|
|
|
|
bg = colors['calendar'][calColorId]['background']
|
|
|
|
return toNaturalColor(colormap, bg)
|
2020-04-15 19:43:52 +02:00
|
|
|
|
2020-03-13 21:46:14 +01:00
|
|
|
def toJson(events):
|
|
|
|
data = {}
|
|
|
|
data['kind'] = 'calendar#events'
|
|
|
|
data['events'] = []
|
|
|
|
|
|
|
|
for event in events:
|
|
|
|
if event.allDay:
|
|
|
|
continue
|
2020-03-15 14:49:31 +01:00
|
|
|
|
2020-03-13 21:46:14 +01:00
|
|
|
data['events'].append({
|
|
|
|
'name': event.name,
|
|
|
|
'isAllDay': event.allDay,
|
|
|
|
'color': event.colorHex,
|
|
|
|
'startDateTime': event.startDateTime(),
|
|
|
|
'stopDateTime': event.stopDateTime()
|
|
|
|
})
|
2020-03-09 23:54:46 +01:00
|
|
|
|
2020-04-15 19:15:13 +02:00
|
|
|
return data
|
2020-03-13 21:46:14 +01:00
|
|
|
|
2020-03-15 14:49:31 +01:00
|
|
|
def printColors(colors):
|
|
|
|
for i in range(1, 25):
|
|
|
|
col = colors['event'][str(i)]['background']
|
|
|
|
print(f"{i}: {col}")
|
2020-03-13 21:46:14 +01:00
|
|
|
|
|
|
|
def main():
|
2020-04-02 14:24:13 +02:00
|
|
|
generateJsonFromCalendarEntries()
|
|
|
|
|
2020-04-15 19:15:13 +02:00
|
|
|
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)
|
2020-04-15 19:43:52 +02:00
|
|
|
|
|
|
|
colors = getCalendarColors(service)
|
2020-04-15 19:15:13 +02:00
|
|
|
|
|
|
|
for calendar in calendars:
|
2020-04-15 19:43:52 +02:00
|
|
|
calendar.color = fromColorIdGetColor(calendar.color, colormap, colors)
|
2020-04-15 19:15:13 +02:00
|
|
|
return calendars
|
2020-03-13 21:46:14 +01:00
|
|
|
|
2020-05-15 16:00:11 +02:00
|
|
|
def generateJsonFromCalendarEntries(visibleCalendars, credentials = None):
|
2020-04-15 19:15:13 +02:00
|
|
|
|
|
|
|
# create service
|
|
|
|
if credentials == None:
|
|
|
|
credentials = calendarCredentials()
|
|
|
|
service = build(API_SERVICE_NAME, API_VERSION, credentials=credentials)
|
|
|
|
|
2020-03-13 21:46:14 +01:00
|
|
|
# define today and tomorrow
|
2020-03-12 00:08:30 +01:00
|
|
|
now = datetime.datetime.now(datetime.timezone.utc).astimezone()
|
2020-03-13 21:46:14 +01:00
|
|
|
today = now.replace(hour=0, minute=0, second = 0).isoformat()
|
|
|
|
tomorrow = now.replace(hour=23, minute=59, second=59).isoformat() # + '+01:00'
|
2020-05-21 10:19:37 +02:00
|
|
|
twodaysfromnow = datetime.datetime.today() + datetime.timedelta(days=2)
|
|
|
|
twodaysfromnow = twodaysfromnow.replace(hour=23, minute=59, second=59).astimezone().isoformat()
|
2020-03-13 21:46:14 +01:00
|
|
|
|
2020-05-21 10:19:37 +02:00
|
|
|
print(tomorrow, flush=True)
|
|
|
|
print('----')
|
|
|
|
print(twodaysfromnow, flush=True)
|
|
|
|
allEvents = getCalendarEvents(service, visibleCalendars, today, twodaysfromnow)
|
2020-03-08 22:28:45 +01:00
|
|
|
|
2020-04-15 19:43:52 +02:00
|
|
|
colorizeEvents(allEvents, service)
|
2020-03-09 23:54:46 +01:00
|
|
|
# if not events:
|
|
|
|
# print('No upcoming events found.')
|
2020-03-13 21:46:14 +01:00
|
|
|
|
2020-04-15 19:15:13 +02:00
|
|
|
return toJson(allEvents)
|
2020-03-08 22:28:45 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-03-09 23:54:46 +01:00
|
|
|
main()
|