-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservices.py
82 lines (66 loc) · 2.45 KB
/
services.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
# Register MOL backend services here, then put their
# scripts and sql in the appropriate services sub-folder
# speciesinfo service runs speciesinfo.sql
# against the cartodb sql api in the /services/cartodb folder
import logging
import cache
import json
import hashlib
import config_creds
from collections import OrderedDict
from google.appengine.api import taskqueue
from google.appengine.api import memcache
#EE_TILE_URL = '/api/tile/%s/{Z}/{X}/{Y}/%s' # (key, token)
EE_TILE_URL = 'https://earthengine.googleapis.com/map/%s/{Z}/{X}/{Y}?token=%s'
def getKey(endpoint, querystring):
return '%s_%s_%s' % (
cache_key,
endpoint,
hashlib.md5(
querystring
).hexdigest()
)
def checkCache(key, type='json'):
logging.info('Checking cache for %s' % (key))
if type == 'json':
result = memcache.get(key)
if result:
result = json.loads(result)
else:
logging.info('No json in memcache, checking cache')
result = cache.get(key, value_type='string', loads=True)
else:
result = memcache.get(key)
if result:
logging.info('Got something from memcache')
else:
logging.info('No blob in memcache, checking cache')
result = cache.get(key, value_type='blob')
if result is not None:
logging.info("Got something from cache for %s." % key)
else:
logging.info("No love in cache for %s." % key)
return result
def cacheResult(result, key, type='json'):
logging.info('Caching %s' % key)
if type == 'json':
cache.add(key, json.dumps(result), value_type='string')
memcache.add(key, json.dumps(result))
else:
cache.add(key, result, value_type='blob')
memcache.add(key, result)
def writeResult(result, response, format="application/json", filename="mol.csv"):
response.headers["Content-Type"] = format
if format == "application/json":
response.headers["Content-Type"] = "application/json"
logging.info(type(result))
if type(result) not in ( type(str()), type(unicode())):
response.out.write(json.dumps(result))
else:
response.out.write(result)
elif format == "csv":
response.headers["Content-Type"] = "text/csv"
response.headers["Content-Disposition"] = str("attachment; filename=%s" % str(filename))
response.out.write(result)
else:
response.out.write(result)