forked from diegomtzg/MirrorX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendarManager.py
119 lines (95 loc) · 4.82 KB
/
calendarManager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from __future__ import print_function
import dateutil.parser
import httplib2
import os
import time
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
import datetime
import smartMirrorManager
from PyQt5.QtWidgets import QWidget, QLabel, QHBoxLayout, QVBoxLayout, QFormLayout
from PyQt5.QtGui import *
from PyQt5.QtCore import *
try:
import argparse
flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
flags = None
SCOPES = 'https://www.googleapis.com/auth/calendar.readonly'
global CLIENT_SECRET_FILE
APPLICATION_NAME = 'MirrorX'
class Calendar(QWidget):
def __init__(self):
super(Calendar, self).__init__()
self.initUI()
def initUI(self):
self.titleFont = QFont('Helvetica', smartMirrorManager.title_fontsize)
self.contentFont = QFont('Helvetica', 20)
self.calendarRows = QFormLayout()
self.calendarRows.setVerticalSpacing(15)
self.calendarRows.setHorizontalSpacing(40)
self.calendarTitle = QLabel("<font color='white'>Today's Events</font>")
self.calendarTitle.setFont(self.titleFont)
self.calendarRows.addRow(self.calendarTitle)
self.setLayout(self.calendarRows)
self.getDailyEvents()
self.updateDailyEvents()
def updateDailyEvents(self):
self.timer = QTimer(self)
self.timer.timeout.connect(self.getDailyEvents)
self.timer.start(1000 * 60 * 60) # Update calendar every hour
def getDailyEvents(self):
# Creates a Google Calendar API service object gets the daily meetings for someone (max is 10)
credentials = self.get_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('calendar', 'v3', http=http)
now = datetime.datetime.now().isoformat() + 'Z' # 'Z' indicates UTC
eventsResult = service.events().list(calendarId='primary', timeMin=now, maxResults=10, singleEvents=True, orderBy='startTime').execute()
events = eventsResult.get('items', [])
curr_date = time.strftime("%d")
num_events_today = 0
for event in events:
event_date = dateutil.parser.parse(event['start'].get('dateTime', event['start'].get('date'))).strftime("%d")
if (curr_date == event_date):
num_events_today = num_events_today + 1
if not events or num_events_today == 0:
relaxLabel = QLabel("<font color='white'>Time to Relax!</font>")
noEventsLabel = QLabel("<font color='white'>No scheduled events today.</font>")
relaxLabel.setFont(self.contentFont)
noEventsLabel.setFont(self.contentFont)
self.calendarRows.addRow(relaxLabel)
self.calendarRows.addRow(noEventsLabel)
else:
for event in events:
event_date = dateutil.parser.parse(event['start'].get('dateTime', event['start'].get('date'))).strftime("%d")
# Retrieved event may be for next day, ensure that it is today
if(curr_date == event_date):
eventStart = dateutil.parser.parse(event['start'].get('dateTime', event['start'].get('date'))).strftime("%I:%M %p").lstrip('0')
eventEnd = dateutil.parser.parse(event['end'].get('dateTime', event['end'].get('date'))).strftime("%I:%M %p").lstrip('0')
eventName = event['summary']
newEventName = QLabel("<font color='white'>" + eventName + "</font>")
newEventName.setWordWrap(True)
newEventName.setAlignment(Qt.AlignRight)
newEventName.setFixedWidth(150)
newEventTime = QLabel("<font color='white'>" + eventStart + "</font>")
newEventName.setFont(self.contentFont)
newEventTime.setFont(self.contentFont)
newEventTime.setAlignment(Qt.AlignRight)
self.calendarRows.addRow(newEventTime, newEventName)
def get_credentials(self):
# Gets valid user credentials from storage.
# If nothing has been stored, or if the stored credentials are invalid, the OAuth2 flow is completed to obtain the new credentials.
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir, CLIENT_SECRET_FILE)
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
credentials = tools.run_flow(flow, store, flags)
return credentials