/
city.py
59 lines (48 loc) · 1.68 KB
/
city.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
import json
import os
import logging
from pprint import pprint as pp
from google.cloud import storage
def get_cities():
file_name = "cities.json"
# fallback data in case can't get cloud json
here_data = [{'name':'Canberra'},
{'name':'Kathmandu'},
{'name':'Kyoto'},
{'name':'Montreal'},
{'name':'Quebec'},
{'name':'Reykjavik'},
{'name':'Timbuktu'},
{'name':'Toronto'},
{'name':'Vancouver'},
{'name':'Xanadu'}]
# jsonFile = os.path.join( "static", "data", filename)
# jsonFile = os.path.join(os.path.split(__file__)[0], 'data/cities.json')
# create Goo Cloud Storage client to get that json from
try:
# Configure this environment variable via app.yaml
CLOUD_STORAGE_BUCKET = os.environ['CLOUD_STORAGE_BUCKET']
CLOUD_STORAGE_FOLDER = "data"
gcs = storage.Client()
# Get the bucket that the file will be uploaded to.
bucket = gcs.get_bucket(CLOUD_STORAGE_BUCKET)
# the data file will be known as a blob to us henceforth
myblob = bucket.get_blob(CLOUD_STORAGE_FOLDER + "/"+ file_name)
print("we have blob!")
print(myblob.name)
b = myblob.download_as_string()
print("what's in b?")
print(b)
try:
file_data = json.loads(b)
except:
file_data = [{"name":"json file read failed"}]
#print("what's in file_data?")
#print(file_data)
if len(file_data) > 0:
data = file_data
else:
data = here_data
except KeyError:
data = here_data
return data