/
cards.py
392 lines (362 loc) · 16.4 KB
/
cards.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
import webapp2
from jinja_templates import jinja_environment
import customer_configuration
import logging
import datetime
import fusion_tables
from lib import get_localization, get_language, BaseHandler
import json
DATE_TIME_FORMAT = '%Y-%m-%d %H:%M:%S'
class LocationHandler(BaseHandler):
def get(self, location_slug=None, timeframe=None, tags=None, hashtags=None):
now = self.request.get("now")
if not now:
now = datetime.datetime.strftime(datetime.datetime.now(), DATE_TIME_FORMAT) # fallback to server time
configuration = customer_configuration.get_configuration(self.request)
localization = get_localization()
# detect language and use configuration as default
language = get_language(self.request, configuration)
# calculate midnight, midnight1 and midnight 7 based on now
now_p = datetime.datetime.strptime(now, DATE_TIME_FORMAT)
midnight_p = datetime.datetime.combine(now_p + datetime.timedelta(days=1), datetime.time.min)
midnight1_p = datetime.datetime.combine(now_p + datetime.timedelta(days=2), datetime.time.min)
midnight7_p = datetime.datetime.combine(now_p + datetime.timedelta(days=8), datetime.time.min)
midnight = datetime.datetime.strftime(midnight_p, DATE_TIME_FORMAT)
midnight1 = datetime.datetime.strftime(midnight1_p, DATE_TIME_FORMAT)
midnight7 = datetime.datetime.strftime(midnight7_p, DATE_TIME_FORMAT)
# query on timeframe
if timeframe == 'now':
# start < now and end > now
condition = "start <= '" + now + "' and end >= '" + now + "'"
elif timeframe == 'today':
# end > now and start < midnight
condition = "end >= '" + now + "' and start <= '" + midnight + "'"
elif timeframe == 'tomorrow':
# end > midnight and start < midnight + 1 day
condition = "end >= '" + midnight + "' and start <= '" + midnight1 + "'"
elif timeframe == 'week':
# end > now and start < midnight + 7 days
condition = "end >= '" + now + "' and start <= '" + midnight7 + "'"
else: # 'all' and other timeframes are interpreted as 'all'
# end > now
condition = "end >= '" + now + "'"
# apply commercial limit
limit = customer_configuration.get_limit(self.request)
if limit:
condition += " AND 'start' < '%s'" % limit
# query on tags
if tags:
tags_p = tags.split(',')
for tag in tags_p:
condition += " AND tags CONTAINS '#" + tag + "#'"
# tags in the fusion table are surrounded by hash characters to avoid
# confusion if one tag would be a substring of another tag
# query on hashtags
if hashtags:
hashtags_p = hashtags.split(',')
for hashtag in hashtags_p:
condition += " AND hashtags CONTAINS '#" + hashtag + "#'"
# query on location
condition += " AND 'location slug' = '" + location_slug + "'"
# sort by datetime slug
condition += " ORDER BY 'datetime slug'"
no_results_message = ''
data = fusion_tables.select(configuration['slave table'], condition=condition)
if not data:
no_results_message = localization[configuration['language']]['no-results']
condition = "'location slug' = '" + location_slug + "'" # search without timeframe or tags filter
data = fusion_tables.select_first(configuration['slave table'], condition=condition)
if not data:
# TODO what if the location's events have been deleted?
# is foreseen: fallback to query on event_slug only
logging.error("No events found for location (%s)" % condition)
raise webapp2.abort(404)
template = jinja_environment.get_template('location.html')
content = template.render(
configuration=configuration,
data=data,
date_time_reformat=date_time_reformat,
no_results_message=no_results_message,
localization=localization[language]
)
# return the web-page content
self.response.out.write(content)
return
class EventHandler(BaseHandler):
def get(self, event_slug=None, datetime_slug=None):
configuration = customer_configuration.get_configuration(self.request)
# detect language and use configuration as default
language = get_language(self.request, configuration)
localization = get_localization()
# query on event
condition = "'event slug' = '%s'" % event_slug
if datetime_slug:
condition += " AND "
condition += "'datetime slug' = '%s'" % datetime_slug
data = fusion_tables.select(configuration['slave table'], condition=condition)
no_results_message = ''
if not data:
no_results_message = localization[configuration['language']]['no-results']
data = data[0] if data else {}
# if data has no address, fetch it
if not data['address']:
data['address'] = address(data['latitude'], data['longitude'], language)
template = jinja_environment.get_template('event.html')
content = template.render(
configuration=configuration,
data=data,
date_time_reformat=date_time_reformat,
date_time_reformat_iso=date_time_reformat_iso,
no_results_message=no_results_message,
localization=localization[language]
)
# return the web-page content
self.response.out.write(content)
return
class LocationsHandler(BaseHandler):
def get(self):
configuration = customer_configuration.get_configuration(self.request)
# detect language and use configuration as default
language = get_language(self.request, configuration)
localization = get_localization()
offset = self.request.get("offset")
condition = "'state' = 'public'"
# apply commercial limit
limit = customer_configuration.get_limit(self.request)
if limit:
condition += " AND 'start' < '%s'" % limit
if offset:
condition += " OFFSET %s" % offset
# at least for debugging, limit to 100 results
condition += " LIMIT 100"
no_results_message = ''
data = fusion_tables.select(configuration['master table'], condition=condition)
if not data:
no_results_message = localization[configuration['language']]['no-results']
# remove duplicates
unique_data = []
location_slugs = []
for d in data:
location_slug = d['location slug']
if location_slug not in location_slugs:
unique_data.append(d)
location_slugs.append(location_slug)
next_url = self.request.path_url + "?offset=%s" % str(int(offset if offset else 0) + 100)
# for debugging, the id must be added to an url as parameter
id_appendix = ""
if self.request.get("id"):
id_appendix = "?id=%s" % self.request.get("id")
next_url += "&id=%s" % self.request.get("id")
template = jinja_environment.get_template('locations.html')
content = template.render(
configuration=configuration,
data=unique_data,
date_time_reformat=date_time_reformat,
no_results_message=no_results_message,
localization=localization[language],
id_appendix=id_appendix,
offset=offset,
next_url=next_url
)
# return the web-page content
self.response.out.write(content)
return
class GeoJSONHandler(BaseHandler):
def get(self):
now = self.request.get("now")
if not now:
now = datetime.datetime.strftime(datetime.datetime.now(), DATE_TIME_FORMAT) # fallback to server time
condition = "'previous start' <= '" + now + "' and 'end' >= '" + now + "'"
cols = ['datetime slug', 'event slug', 'sequence', 'latitude', 'longitude', 'location slug', 'tags', 'hashtags']
configuration = customer_configuration.get_configuration(self.request)
# query on event
data = fusion_tables.select(configuration['slave table'], condition=condition, cols=cols)
# convert the results to GeoJSON
features = []
for row in data:
features.append({
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [row['longitude'], row['latitude']]
},
"properties": {
"datetime slug": row['datetime slug'],
"event slug": row['event slug'],
"sequence": row['sequence'],
"location slug": row['location slug'],
"tags": row['tags'],
"hashtags": row['hashtags']
}
})
feature_collection = {
"type": "FeatureCollection",
"features": features
}
# return the web-page content
self.response.headers['Content-Type'] = 'application/json'
self.response.out.write(json.dumps(feature_collection))
return
class SitemapByLocationHandler(BaseHandler):
def get(self):
configuration = customer_configuration.get_configuration(self.request)
location = self.request.get("location")
condition = "'sequence' > 0"
# apply commercial limit
limit = customer_configuration.get_limit(self.request)
if limit:
condition += " AND 'start' < '%s'" % limit
if location:
condition += " AND 'location slug' = '%s'" % location
condition += " ORDER BY 'datetime slug'"
no_results_message = ''
data = fusion_tables.select(configuration['slave table'], condition=condition, cols=[
'event slug',
'datetime slug',
'sequence',
'start'
])
if not data:
no_results_message = '# No results'
template = jinja_environment.get_template('sitemap.txt')
content = template.render(
configuration=configuration,
data=data,
no_results_message=no_results_message
)
# return the web-page content
self.response.headers['Content-Type'] = "text/plain"
self.response.out.write(content)
return
class SitemapHandler(BaseHandler):
def get(self):
configuration = customer_configuration.get_configuration(self.request)
offset = self.request.get("offset")
batch = self.request.get("batch")
condition = "'sequence' > 0"
# apply commercial limit
limit = customer_configuration.get_limit(self.request)
if limit:
condition += " AND 'start' < '%s'" % limit
if offset:
condition += " OFFSET %s" % offset
# at least for debugging, limit to 100 results
if batch:
condition += " LIMIT %s" % batch
no_results_message = ''
data = fusion_tables.select(configuration['slave table'], condition=condition, cols=[
'event slug',
'datetime slug',
'sequence',
'start'
])
if not data:
no_results_message = '# No results'
template = jinja_environment.get_template('sitemap.txt')
content = template.render(
configuration=configuration,
data=data,
no_results_message=no_results_message
)
# return the web-page content
self.response.headers['Content-Type'] = "text/plain"
self.response.out.write(content)
return
class IndexByLocationHandler(BaseHandler):
def get(self):
configuration = customer_configuration.get_configuration(self.request)
count = fusion_tables.count(configuration['slave table'])
template = jinja_environment.get_template('sitemapindexbylocation.xml')
# get a list of all locations (location slug)
condition = "'sequence' > 0"
# apply commercial limit
limit = customer_configuration.get_limit(self.request)
if limit:
condition += " AND 'start' < '%s'" % limit
condition += " GROUP BY 'location slug'"
no_results_message = ''
locations = fusion_tables.select(configuration['slave table'], condition=condition, cols=[
'location slug'
])
if not locations:
no_results_message = '# No results'
content = template.render(
configuration=configuration,
locations=locations
)
# return the web-page content
self.response.headers['Content-Type'] = "application/xml"
self.response.out.write(content)
return
class IndexHandler(BaseHandler):
def get(self):
configuration = customer_configuration.get_configuration(self.request)
count = fusion_tables.count(configuration['slave table'])
template = jinja_environment.get_template('sitemapindex.xml')
# render the series of offsets
batch = 1000
offsets = [0]
while offsets[-1] < count - batch:
offsets.append(offsets[-1] + batch)
content = template.render(
configuration=configuration,
batch=batch,
offsets=offsets
)
# return the web-page content
self.response.headers['Content-Type'] = "application/xml"
self.response.out.write(content)
return
def date_time_reformat(date, format='full', lang='en'):
from babel.dates import format_date, format_datetime, format_time
date_p = datetime.datetime.strptime(date, DATE_TIME_FORMAT)
return format_datetime(date_p, format=format, locale=lang)
from google.appengine.ext import ndb
from google.appengine.api import urlfetch
def date_time_reformat_iso(date, latitude, longitude):
# caching timezone doesn't make much sense, because it will depend on the actual date because of daylight saving
date_p = datetime.datetime.strptime(date, DATE_TIME_FORMAT)
"""
key = "%.4f,%.4f" % (latitude, longitude)
api_key = "AIzaSyAObYcVpywvDwFBZqDxU6PIRvVji9vM9TQ"
timestamp = (date_p - datetime.datetime(1970, 1, 1)).total_seconds() # actually shouldn't do this, because date_p isn't UTC
url = "https://maps.googleapis.com/maps/api/timezone/json?location=%.6f,%.6f×tamp=%d&key=%s" % (latitude, longitude, timestamp, api_key)
try:
result = urlfetch.fetch(url)
if result.status_code == 200:
timezone_json = result.content
else:
logging.exception("HTTP error fetching url %s" % url)
timezone_json = ''
except urlfetch.Error:
logging.exception("Caught exception fetching url %s" % url)
"""
# 20190103 caching would make sense!!! I'm now paying > $50 per month for timezone API calls! For time being, local timezone only!
timezone_json = '{"dstOffset" : 0, "rawOffset" : 3600, "status" : "OK", "timeZoneId" : "Europe/Brussels", "timeZoneName" : "Central European Standard Time"}'
timezone = json.loads(timezone_json)
(offset_hours, offset_minutes) = divmod((timezone["dstOffset"] + timezone["rawOffset"]) / 60, 60)
iso = date_p.isoformat() + '+' + "%02d:%02d" % (offset_hours, offset_minutes)
return iso
class Address_cache(ndb.Model):
location = ndb.DateProperty() # key is string composed of latitude + longitude + language
content = ndb.TextProperty()
def address(latitude, longitude, language):
key = "%.6f,%.6f" % (latitude, longitude)
address_cache = Address_cache.get_or_insert(key)
if not address_cache.content:
api_key = "AIzaSyAObYcVpywvDwFBZqDxU6PIRvVji9vM9TQ"
url = "https://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&result_type=street_address&language=%s&key=%s" % (latitude, longitude, language, api_key)
try:
result = urlfetch.fetch(url)
if result.status_code == 200:
address_json = result.content
else:
logging.exception("HTTP error fetching url %s" % url)
address_json = ''
except urlfetch.Error:
logging.exception("Caught exception fetching url %s" % url)
address_cache.content = address_json
address_cache.put()
addresses = json.loads(address_cache.content)
address = addresses['results'][0]['formatted_address']
return address