-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGoogleCalendar.py
127 lines (93 loc) · 3.61 KB
/
GoogleCalendar.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
120
121
122
123
124
125
126
127
import asyncio
from os import system
import random
import _thread
import sys
from threading import Timer
import httplib2
import os
from apiclient import discovery
from oauth2client import client
from oauth2client import tools
from oauth2client.file import Storage
from datetime import datetime
from dateutil import parser
import pytz
'''
GoogleCalendar Module
@class GoogleCalendar
@author - Team Wizards of Coz
'''
class GoogleCalendar():
pollDuration = 120
timeToEvent = 100
flags = None
scopes = None
secret_file = None
app_name = None
tz = None
def __init__(self,scopes,secret_file,app_name,tz):
self.scopes = scopes
self.secret_file = secret_file
self.app_name = app_name
self.tz = pytz.timezone(tz)
try:
import argparse
self.flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
self.flags = None
def pollCalendar(self):
# GOOGLE CALENDAR
credentials = self.getCredentials()
http = credentials.authorize(httplib2.Http())
self.service = discovery.build('calendar', 'v3', http=http)
Timer(self.pollDuration, self.pollCalendar).start()
def listNUpcomingEvents(self,num):
now = datetime.utcnow().isoformat() + 'Z' # 'Z' indicates UTC time
print('Getting the upcoming event')
eventsResult = self.service.events().list(
calendarId='primary', timeMin=now, maxResults=num, singleEvents=True,
orderBy='startTime').execute()
events = eventsResult.get('items', [])
if not events:
print('No upcoming events found.')
return []
else:
return events
def todaysEventAndTimeToEvent(self):
events = self.listNUpcomingEvents(5)
if not events:
print('No upcoming event found today')
return None,-1
else:
for event in events:
start = event['start'].get('dateTime', event['start'].get('date'))
startDate = parser.parse(start)
todayTZ = self.tz.localize(datetime.today())
if startDate.date()==todayTZ.date():
if startDate>todayTZ:
hours,minutes = self.hours_minutes(startDate-todayTZ)
totalMinutes = (hours*60) + minutes
return event,totalMinutes
return None,-1
def getCredentials(self):
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,'calendar-python-quickstart.json')
store = Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(self.secret_file, self.scopes)
flow.user_agent = self.app_name
if self.flags:
credentials = tools.run_flow(flow, store, self.flags)
else: # Needed only for compatibility with Python 2.6
credentials = tools.run(flow, store)
print('Storing credentials to ' + credential_path)
return credentials
def hours_minutes(self,td):
return td.seconds//3600, (td.seconds//60)%60
if __name__ == '__main__':
GoogleCalendar()