-
Notifications
You must be signed in to change notification settings - Fork 5
/
extract_calendars.py
300 lines (232 loc) · 8.09 KB
/
extract_calendars.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright 2015 @lmorillas. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Based on
https://github.com/google/google-api-python-client/blob/master/samples/service_account/tasks.py
by jcgregorio@google.com
"""
__author__ = 'morillas@google.com (Luis Miguel Morillas)'
import httplib2
import pprint
import sys
import datetime
from operator import itemgetter
from itertools import groupby
import dotenv
import os
from bs4 import BeautifulSoup
dotenv.load_dotenv()
apik = os.getenv('apik')
from googleapiclient.discovery import build
from oauth2client.client import SignedJwtAssertionCredentials, AccessTokenRefreshError
from geopy import geocoders
google = geocoders.GoogleV3(api_key=apik, timeout=5)
yandex = geocoders.Yandex(timeout=5)
nom = geocoders.Nominatim(timeout=5)
import shelve
# Credentials for Service Accout
EMAIL_CLIENT = '696801545616-44i6o78jdoa7me4lr416n1d5rniidmns@developer.gserviceaccount.com'
FILE_KEY = 'pycal.p12'
def connect_calendar():
# Load the key in PKCS 12 format that you downloaded from the Google API
# Console when you created your Service account.
f = open(FILE_KEY, 'rb')
key = f.read()
f.close()
credentials = SignedJwtAssertionCredentials(EMAIL_CLIENT,
key,
scope=['https://www.googleapis.com/auth/calendar',
'https://www.googleapis.com/auth/calendar.readonly'])
http = httplib2.Http()
http = credentials.authorize(http)
service = build(serviceName='calendar', version='v3', http=http)
return service
def get_month(date_str):
'''
returns start month str from event
'''
return datetime.datetime.strptime(date_str[:10], '%Y-%m-%d').strftime("%B")
def calendar_events(service, cal_id, singleEvents="False"):
# Today: only envents present and future
timeMin = datetime.datetime.now().strftime('%Y-%m-%dT00:00:00.000Z')
if singleEvents != "False":
timeMax = '{}-12-31T23:00:00.000Z'.format(datetime.datetime.now().year)
else:
timeMax = None
#timeMin = datetime.datetime.now().isoformat()
events = []
try:
page_token = None
while True:
event_list = service.events().list(singleEvents=singleEvents,orderBy='startTime', calendarId=cal_id,
pageToken=page_token, timeMin=timeMin, timeMax=timeMax).execute()
events.extend([event for event in event_list['items']])
page_token = event_list.get('nextPageToken')
if not page_token:
break
except AccessTokenRefreshError:
print ('The credentials have been revoked or expired, please re-run'
'the application to re-authorize.')
return events
def geolocate(address):
global geocache
#address = address.encode('utf-8') # for storing in shelve
loc = None
if address not in geocache.keys():
print ('Searching ', address)
try:
loc = google.geocode(address)
except:
pass
if not loc:
try:
loc = yandex.geocode(address)
except:
pass
if not loc:
try:
loc = google.geocode(','.join(address.split(',')[1:]))
except:
pass
if loc:
loc = loc.latitude, loc.longitude, loc.raw
geocache[address] = loc
else:
loc = geocache.get(address)[:2]
return loc
def loc_to_country(latlon):
global geocache
if latlon not in geocache.keys():
print ('Searching country of ', latlon)
try:
loc = nom.reverse(latlon)
if loc:
country = loc.raw.get('address').get('country')
geocache[latlon] = country
return country
except:
return ''
else:
return geocache.get(latlon)
def event_to_item(event, cal):
if event.get('summary'):
print (event.get('summary').encode('utf-8'), ' --> ' )
else:
print('No summary ? ', event)
item = {}
item['description'] = event.get('description')
item['id'] = event.get('id')
item['start'] = event.get('start').get('date')
if not item['start']:
item['start'] = event.get('start').get('dateTime')
item['end'] = event.get('end').get('date')
if not item['end']:
item['end'] = event.get('end').get('dateTime')
item['label'] = event.get('summary')
item['url'] = event.get('htmlLink')
item['cal'] = cal
item['month'] = get_month(item.get('start'))
address = event.get('location')
if not address:
address = event.get('description')
if address:
address = BeautifulSoup(address, 'lxml').text
else:
print(event)
if address:
location = geolocate(address)
if location:
lat = location[0]
lon = location[1]
item['latlon'] = "{},{}".format(lat, lon)
print (item['latlon'])
country = loc_to_country(item['latlon'])
item['country'] = country
else:
print('ERROR geolocating ', address)
else:
print('No address in event', event)
return item
def create_index(data="", schema = ""):
import pytz
#data = json.dumps(data)
data = json.JSONEncoderForHTML().encode(data)
schema = json.dumps(schema)
now = datetime.datetime.now(pytz.utc)
format = "%Y-%m-%d" # "%Y-%m-%d %H:%M %Z"
template = open('index.templ').read()
open('docs/index.html', 'w').write(template.format(datetime=now.strftime(format),
data=data, schema=schema ))
def select_first_event(eventlist):
'''select only the first enven when repeated events'''
def sort_by_eventID(element):
return element.get('recurringEventId', element.get('summary'))
#recurring = itemgetter('recurringEventId') # keyerror ?
recurring = sort_by_eventID
def _date(x):
return x.get('start').get('dateTime')
eventlist.sort(key=recurring)
_non_repeated = []
for ev, recur in groupby(eventlist, key=recurring):
try:
recur = sorted(recur, key=_date)
_non_repeated.append(recur[0]) # only add the first
except:
print ('recur error -> ', [x for x in recur])
return _non_repeated
if __name__ == '__main__':
import datetime
import simplejson as json
geocache = shelve.open('geocache.dat')
# Cals IDs from https://wiki.python.org/moin/PythonEventsCalendar
cal_id_python_events = 'j7gov1cmnqr9tvg14k621j7t5c@group.calendar.google.com'
cal_id_user_group = '3haig2m9msslkpf2tn1h56nn9g@group.calendar.google.com'
items = []
service = connect_calendar()
events = calendar_events(service, cal_id_python_events)
for event in events:
items.append(event_to_item(event, 'Larger'))
events = calendar_events(service, cal_id_user_group, singleEvents="True")
events = select_first_event(events)
for event in events:
items.append(event_to_item(event, 'Smaller'))
geocache.sync()
geocache.close()
schema = {"properties": {
"url": {
"valueType": "url"
},
"start": {
"valueType": "date"
},
"end": {
"valueType": "date"
},
"month": {
"valueType": "date"
},
},
"types": {
"Item": {
"pluralLabel": "events",
"label": "event"
}
}}
data = {'items': items}
#data.update(metadata)
#json.dump(data, open('docs/events_python.json', 'w'))
create_index(data, schema)