-
Notifications
You must be signed in to change notification settings - Fork 3
/
worldmap.py
472 lines (383 loc) · 14 KB
/
worldmap.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
"""
Recurse World Map back-end
"""
# Copyright (C) 2019 Jaryn Colbert <jaryn.colbert@gmail.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from itertools import groupby
import logging
from functools import wraps
import requests
from flask import Flask, jsonify, redirect, request, send_from_directory, session, url_for
from flask_cors import CORS
from authlib.integrations.flask_client import OAuth
from werkzeug.exceptions import HTTPException
import psycopg2
from update_data import get_env_var, lookup_geodata, insert_geo_data, insert_alias
# pylint: disable=invalid-name
app = Flask(__name__, static_url_path='/build')
CORS(app)
app.secret_key = get_env_var('FLASK_SECRET_KEY', 'development')
logging.basicConfig(level=logging.INFO)
rc = OAuth(app).register(
'Recurse Center',
api_base_url='https://www.recurse.com/api/v1/',
authorize_url='https://www.recurse.com/oauth/authorize',
access_token_url='https://www.recurse.com/oauth/token',
client_id=get_env_var('CLIENT_ID'),
client_secret=get_env_var('CLIENT_SECRET'),
)
connection = psycopg2.connect(get_env_var('DATABASE_URL'))
token = get_env_var('RC_API_ACCESS_TOKEN')
@app.route('/')
def index():
"Get the single-page app HTML"
return send_from_directory('build', 'index.html')
@app.route('/favicon.ico')
def favicon():
"Get the app favicon"
return send_from_directory('build', 'favicon.ico')
@app.route('/manifest.json')
def manifest():
"Get the app manifest"
return send_from_directory('build', 'manifest.json')
@app.route('/static/<path:path>')
def static_file(path):
"Get the single-page app assets"
return send_from_directory('build/static', path)
@app.route('/auth/recurse')
def auth_recurse_redirect():
"Redirect to the Recurse Center OAuth2 endpoint"
callback = get_env_var('CLIENT_CALLBACK')
return rc.authorize_redirect(callback)
@app.route('/auth/recurse/callback', methods=['GET', 'POST'])
def auth_recurse_callback():
"Process the results of a successful OAuth2 authentication"
try:
token = rc.authorize_access_token()
except HTTPException:
logging.error(
'Error %s parsing OAuth2 response: %s',
request.args.get('error', '(no error code)'),
request.args.get('error_description', '(no error description'),
)
return (jsonify({
'message': 'Access Denied',
'error': request.args.get('error', '(no error code)'),
'error_description': request.args.get('error_description', '(no error description'),
}), 403)
me = get_rc_profile()
logging.info("Logged in: %s", me.get('name', ''))
return redirect(url_for('index'))
def get_rc_profile():
"Return the RC API information for the currently logged in user"
headers = {'Authorization': f'Bearer {token}'}
url = 'https://www.recurse.com/api/v1/profiles/me'
r = requests.get(url, headers=headers)
if r.status_code != requests.codes['ok']:
r.raise_for_status()
me = r.json()
session['recurse_user_id'] = me.get('id', '')
session['recurse_user_name'] = me.get('name', '')
session['recurse_user_image'] = me.get('image_path', '')
session['recurse_user_location'] = me.get('current_location', {}).get('name', '')
return me
def needs_authorization(route):
""" Use the @needs_authorization annotation to check that a valid session
exists for the current user."""
@wraps(route)
def wrapped_route(*args, **kwargs):
"""Check the session, or return access denied."""
if app.debug:
return route(*args, **kwargs)
elif 'recurse_user_id' in session:
return route(*args, **kwargs)
else:
return (jsonify({
'message': 'Access Denied',
}), 403)
return wrapped_route
# @app.route('/api/locations/all')
@needs_authorization
def get_all_rc_locations():
cursor = connection.cursor()
"""Returns all locations in the database
with their geolocation data."""
cursor.execute("""SELECT
location_id,
name,
lat,
lng
FROM geolocations
WHERE EXISTS(
SELECT location_id
FROM location_affiliations
WHERE location_id = geolocations.location_id
)
ORDER BY location_id""")
locations = [{
'location_id': x[0],
'location_name': x[1],
'lat': x[2],
'lng': x[3],
'has_rc_people': True
} for x in cursor.fetchall()]
cursor.close()
return jsonify(locations)
@app.route('/api/locations/all')
@needs_authorization
def get_all_rc_locations_with_people():
cursor = connection.cursor()
# Query returns list of locations grouped in the format:
# {
# location_id:
# location_name:
# type:
# lat:
# lng:
# city_count:
# population:
# person_list: [
# {
# person_id:
# person_name:
# image_url:
# stints: [
# {
# stint_type:
# rc_title:
# batch_name:
# start_date:
# }
# ]
# }
# ]
# }
"""Returns all locations in the database
with their geolocation data and all affiliated RC users."""
cursor.execute("""SELECT
location_id,
location_name,
type,
lat,
lng,
city_count,
total_population,
person_list
FROM geolocations_people_and_stints_agg""")
locations = [{
'location_id': x[0],
'location_name': x[1],
'type': x[2],
'lat': x[3],
'lng': x[4],
'city_count': x[5],
'total_population': x[6],
'has_rc_people': True,
'person_list': x[7]
} for x in cursor.fetchall()]
cursor.close()
return jsonify(locations)
@app.route('/api/locations/search')
def locations_search():
suggestions = []
q = request.args.get('query')
if (q == ""):
return jsonify(suggestions)
headers = {'Authorization': f'Bearer {token}'}
url = 'https://www.recurse.com/api/v1/locations?limit={limit}&query={query}'
limit = 10
r = requests.get(url.format(
limit=limit, query=q), headers=headers)
if r.status_code != requests.codes['ok']:
r.raise_for_status()
suggestions = r.json()
return jsonify(suggestions)
@app.route('/api/locations/<int:id>')
@needs_authorization
def get_location(id):
cursor = connection.cursor()
# If location is aliased to another location, find preferred location
preferred_id = get_alias(cursor, id)
if (preferred_id):
id = preferred_id
# If geolocation data exists with people affiliated, return it
location = get_geolocation_with_people(cursor, id)
if (location):
return jsonify(location)
# Else lookup existing geolocation info, and then return it
location = get_geolocation(cursor, id)
if (location):
add_population_if_country(cursor, location)
return jsonify(location)
# Otherwise, create and insert new location
location_name = request.args.get('name')
if (location_name == ""):
return jsonify({})
location = {
"id": id,
"location_id": id,
"name": location_name,
"short_name": request.args.get('short_name'),
"type": request.args.get('type')
}
insert_location(cursor, location)
connection.commit()
# If there's an existing location in the db with the same lat/lng,
# create an alias and use the alias' id for lookup
geo = lookup_geodata(cursor, location)
preferred_location = find_location_with_coords(cursor, geo)
if (preferred_location):
insert_alias(cursor, location, preferred_location)
id = preferred_location["location_id"]
# Otherwise, insert new geolocation into database
insert_geo_data(cursor, geo)
connection.commit()
# Retrieve location by id now that db has been updated
return get_location(id)
@app.route('/api/me/')
@needs_authorization
def get_current_user():
"Return the name and image for the currently logged in user"
if not 'recurse_user_name' in session:
me = get_rc_profile()
return jsonify({
'person_id': session.get('recurse_user_id', ''),
'name': session.get('recurse_user_name', ''),
'image_url': session.get('recurse_user_image', ''),
'current_location': session.get('recurse_user_location', '')
})
# Returns the id of the preferred location if this location
# has an alias (e.g. "Manhattan, NY" -> "New York City, NY").
# If no alias exists, returns None.
def get_alias(cursor, location_id):
logging.info("Select Alias for Location ID #{}".format(
location_id
))
"""Returns the preferred location alias for the location
with the given id if an alias exists."""
cursor.execute("""SELECT
preferred_location_id
FROM location_aliases
WHERE location_id = %s""", [location_id])
alias_id = cursor.fetchone()
return alias_id[0] if alias_id else None
def find_location_with_coords(cursor, location):
"""Searches database for a location with lat/lng values matching those of the given location."""
cursor.execute("""SELECT
location_id,
name,
lat,
lng
FROM geolocations
WHERE lat = %s
AND lng = %s
AND NOT location_id = %s""",
[location["lat"], location["lng"], location["location_id"]])
results = [{
'location_id': x[0],
'location_name': x[1]
} for x in cursor.fetchall()]
return require_one(results, location["location_id"])
def insert_location(cursor, location):
logging.info("Insert Location #{}: {} ({})".format(
location.get('id'),
location.get('name'),
location.get('short_name')
))
cursor.execute("INSERT INTO locations" +
" (location_id, name, short_name)" +
" VALUES (%s, %s, %s)" +
" ON CONFLICT (location_id)" +
" DO NOTHING ",
[location.get('id'),
location.get('name'),
location.get('short_name')
]
)
def get_geolocation(cursor, location_id):
logging.info("Select Location By ID #{}".format(
location_id
))
"""Returns the requested geolocation data for a location with the given id."""
cursor.execute("""SELECT
location_id,
name,
type,
lat,
lng
FROM geolocations
WHERE location_id = %s""", [location_id])
x = cursor.fetchone()
return {
'location_id': x[0],
'location_name': x[1],
'type': x[2],
'lat': x[3],
'lng': x[4],
'has_rc_people': False
} if x else {}
def get_geolocation_with_people(cursor, location_id):
logging.info("Select Location By ID #{}".format(
location_id
))
"""Returns the requested geolocation data for a location with the given id."""
cursor.execute("""SELECT
location_id,
location_name,
type,
lat,
lng,
city_count,
total_population,
person_list
FROM geolocations_people_and_stints_agg
WHERE location_id = %s""", [location_id])
locations = [{
'location_id': x[0],
'location_name': x[1],
'type': x[2],
'lat': x[3],
'lng': x[4],
'city_count': x[5],
'total_population': x[6],
'has_rc_people': True,
'person_list': x[7]
} for x in cursor.fetchall()]
return require_one(locations, location_id)
def add_population_if_country(cursor, location):
if (location["type"] == 'city'):
return
location_id = location["location_id"]
logging.info("Select Country Population, ID #{}".format(
location_id
))
"""Returns the population and city counts for the country with the given id."""
cursor.execute("""SELECT
location_id,
city_count,
total_population
FROM geolocations_popl_by_country_agg
WHERE location_id = %s""", [location_id])
locations = [{
'location_id': x[0],
'city_count': x[1],
'total_population': x[2]
} for x in cursor.fetchall()]
population_data = require_one(locations, location_id)
location.update(population_data)
def require_one(location_arr, location_id):
if (len(location_arr) == 0):
return {}
if (len(location_arr) == 1):
return location_arr[0]
# Otherwise, throw error - Result should be <= 1
raise ValueError("Multiple locations returned for id " + location_id)