-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserve.py
231 lines (174 loc) · 6.51 KB
/
serve.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
import json
from db import Base, Photo, Face
from flask import Flask, request
import datetime
import sys
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import cast, DATE, extract
engine = create_engine('sqlite:///photos-saved.db', convert_unicode=True)
Base.metadata.bind = engine
DBSession = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
app = Flask(__name__)
def generate_timeline(sorted_photos):
last_year = None
last_month = None
last_region = None
cities_in_region = {}
current = []
timeline = []
for photo in sorted_photos:
do_break = False
photo_year, photo_month = photo.date.year, photo.date.month
if photo_year != 1969:
if photo_year and photo_year != last_year:
timeline.append({"year" : photo_year})
last_year = photo_year
if photo_month and photo_month != last_month:
timeline.append({"month" : photo_month})
last_month = photo_month
if photo.region2 and photo.region2 != last_region:
timeline.append({"region" : photo.region2})
last_region = photo.region2
timeline.append({ "filename" : "%s.jpg" % photo.hash_,
"aspectRatio" : 1.0,
"city" : photo.city,
"lat" : photo.latitude,
"lon" : photo.longitude})
timeline2 = []
tmp = []
def get_cities(l):
cities = {}
for e in l:
c = e["city"]
if c: cities[c] = True
return ", ".join( sorted(cities.keys()) )
while len(timeline) > 0:
e = timeline.pop(0)
if "filename" in e:
tmp.append(e)
if not "filename" in e or len(timeline) == 0:
c = get_cities(tmp)
timeline2.append({ "cities" : c })
if len(tmp) > 0: timeline2.append(tmp)
tmp = []
timeline2.append(e)
return timeline2
@app.route('/', methods = ['GET', 'POST'])
def index():
session = DBSession()
if request.method == 'POST':
query_year = int(request.form['query_year'])
query_city = request.form['query_city']
## Y no query by date year
query = session.query(Photo)
if query_year:
query = query.filter(Photo.date >= datetime.date(query_year, 1, 1) )\
.filter(Photo.date < datetime.date(query_year+1, 1, 1) )
if query_city:
query = query.filter(Photo.city.ilike(query_city))
sorted_photos = query.order_by(Photo.date.desc(),Photo.path).all()
else:
sorted_photos = session.query(Photo).order_by(Photo.date.desc(),Photo.path).all()
timeline = { "data" : generate_timeline(sorted_photos) }
response = app.response_class(
response=json.dumps(timeline),
status=200,
mimetype='application/json'
)
session.close()
return response
@app.route('/query_data', methods = ['GET',])
def qet_query_data():
session = DBSession()
years = sorted([a[0] for a in session.query(extract('year', Photo.date)).distinct().all()])
d = {}
for year in years:
query = session.query(Photo.city)
query = query.filter(Photo.date >= datetime.date(year, 1, 1) )\
.filter(Photo.date < datetime.date(year+1, 1, 1) )
cities = sorted([a[0] for a in query.filter(Photo.city.isnot(None)).distinct().all()])
d[year] = cities
response = app.response_class(
response=json.dumps(d),
status=200,
mimetype='application/json'
)
session.close()
return response
@app.route('/photo/<hash_>.jpg', methods = ['GET',])
def photo(hash_):
session = DBSession()
photo = session.query(Photo).filter(Photo.hash_ == hash_).all()[0]
if photo:
d = { "path" : photo.path,
"date" : photo.date.strftime('%d %b %Y') if photo.date else "",
"latitude" : photo.latitude,
"longitude" : photo.longitude }
else:
d = {}
response = app.response_class(
response=json.dumps(d),
status=200,
mimetype='application/json'
)
session.close()
return response
@app.route('/faces/<hash_>.jpg', methods = ['GET',])
def faces(hash_):
session = DBSession()
faces = session.query(Face).filter(Face.photo_id == hash_).all()
d = ["faces/%s_%d.jpg" % (f.photo_id, f.id) for f in faces]
response = app.response_class(
response=json.dumps(d),
status=200,
mimetype='application/json'
)
session.close()
return response
# TODO
@app.route('/person/<hash_>.jpg', methods = ['GET',])
def person(hash_):
session = DBSession()
photo = session.query(Photo).filter(Photo.hash_ == hash_).all()[0]
people = session.query(Face) .filter(Face.photo_id == photo.hash_).query(Face.person_id).distinct().all()
d = {}
for p in people:
photos = session.query(Face).filter(Face.person_id == p.id).query(Face.photo_id).distinct().all()
d[p.id] = { p.name, photos }
response = app.response_class(
response=json.dumps(d),
status=200,
mimetype='application/json'
)
session.close()
return response
@app.route('/nearby/<hash_>.jpg', methods = ['GET',])
def nearby(hash_):
session = DBSession()
photo = session.query(Photo).filter(Photo.hash_ == hash_).all()[0]
d = []
if photo.latitude and photo.longitude:
photos_nearby = session.query(Photo).filter(Photo.intersects(photo)).all()
d = [{ "filename" : "%s.jpg" % p.hash_,
"lat" : p.latitude,
"lon" : p.longitude } for p in photos_nearby if p.hash_ != photo.hash_][:10]
d.insert(0, { "filename" : "%s.jpg" % photo.hash_,
"lat" : photo.latitude,
"lon" : photo.longitude } )
response = app.response_class(
response=json.dumps(d),
status=200,
mimetype='application/json'
)
session.close()
return response
@app.after_request
def after_request(response):
response.headers.add('Access-Control-Allow-Origin', '*')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type,Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS')
return response