-
Notifications
You must be signed in to change notification settings - Fork 1
/
gcal.py
153 lines (114 loc) · 5.11 KB
/
gcal.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#calendar functions
import os
import jinja2
import logging
from apiclient import errors
import time, threading
from datetime import datetime, timedelta
from google.appengine.ext import deferred
import util
BUNDLE_TEMPLATE_URL = 'templates/bundle.html'
EVENT_TEMPLATE_URL = 'templates/event.html'
DEFAULT_REFRESH_TIME = 3600.0 #auto-refresh every hour
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
def auto_refresh(creds, mirror_service, calendar_service, item_id, calendar_title, calendar_id, first_time=None):
#check if calendar and timeline item still exist
#then refresh, set a timer to refresh again on new thread
logging.info('auto refresh called')
calendar_service = util.create_service('calendar', 'v3', creds)
mirror_service = util.create_service('mirror', 'v1', creds)
try:
timeline_item = mirror_service.timeline().get(id = item_id).execute()
calendar_item = calendar_service.calendarList().get(calendarId = calendar_id).execute()
if timeline_item.get('isDeleted') or not calendar_item:
logging.info("stopped auto-refresh")
return "auto-refresh halted, timeline item or calendar does not exist"
except errors.HttpError, error:
logging.info("error in auto-refresh try")
return "auto-refresh error, breaking"
if not first_time:
refresh_me(mirror_service, calendar_service, item_id, calendar_title, calendar_id)
time.sleep(DEFAULT_REFRESH_TIME)
logging.info('about to start new thread')
deferred.defer(auto_refresh, creds, mirror_service, calendar_service, item_id, calendar_title, calendar_id)
logging.info('new thread started')
return "auto-refresh thread done"
def refresh_me(mirror_service, calendar_service, item_id, calendar_title, calendar_id):
logging.info('refresh me called')
bundle_html, event_htmls = get_html_from_calendar(calendar_service, calendar_id, calendar_title)
patched_timeline_item = {'html': bundle_html,
'htmlPages': event_htmls}
try:
result = mirror_service.timeline().patch(id=item_id, body=patched_timeline_item).execute()
except errors.HttpError, error:
logging.info ('an error has occured %s ', error)
return "Calendar named \'" + calendar_title + "\'' has been refreshed."
def get_html_from_calendar(calendar_service, calendar_id, calendar_title):
my_events = []
event_htmls = []
template_values = {}
MAX_EVENTS = 10
logging.info('beginning to grab events')
page_token = None
dt_utc = datetime.utcnow()
dt_local = dt_utc - timedelta(seconds=time.altzone)
timeMin = dt_local.isoformat("T") + "Z"
logging.info("min")
logging.info(timeMin)
events = calendar_service.events().list(calendarId=calendar_id,
pageToken=page_token,
singleEvents=True,
orderBy="startTime",
timeMin=timeMin,
maxResults=MAX_EVENTS).execute()
if events:
for event in events['items']:
#parse these
logging.info("start")
logging.info(event['start'])
e = {}
if 'start' in event:
if 'dateTime' in event['start']:
e['startDate'] = event['start']['dateTime'][0:10]
e['startTime'] = event['start']['dateTime'][11:16]
d = datetime.strptime(e['startTime'], '%H:%M')
e['startTime'] = d.strftime('%I:%M')
else:
e['startDate'] = event['start']['date']
e['startTime'] = ''
d = datetime.strptime(e['startDate'], '%Y-%m-%d')
e['startDate'] = d.strftime('%a, %b %d')
if 'end' in event:
if 'dateTime' in event['end']:
e['endDate'] = event['end']['dateTime'][0:10]
e['endTime'] = event['end']['dateTime'][11:16]
d = datetime.strptime(e['endTime'], '%H:%M')
e['endTime'] = d.strftime('%I:%M%p')
else:
e['endDate'] = event['end']['date']
e['endTime'] = ''
d = datetime.strptime(e['endDate'], '%Y-%m-%d')
e['endDate'] = d.strftime('%a, %b %d')
if 'summary' in event:
e['title'] = event['summary']
if 'description' in event:
e['description'] = event['description']
if 'location' in event:
e['location'] = event['location']
if 'reminder' in event:
e['reminder'] = event['reminders']
my_events.append(e)
#set html for individual events
template_values['event'] = e
template = jinja_environment.get_template(EVENT_TEMPLATE_URL)
html = template.render(template_values)
event_htmls.append(html)
logging.info('done grabbing events')
#############
#now build the bundle to push to timeline
template = jinja_environment.get_template(BUNDLE_TEMPLATE_URL)
template_values['events'] = my_events
template_values['calendar_title'] = calendar_title
bundle_html = template.render(template_values)
return bundle_html, event_htmls