-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
92 lines (81 loc) · 3.19 KB
/
main.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
import json
from functools import reduce
from flask import Flask, request, json, send_from_directory, redirect
from geopy import Nominatim
import psycopg2
app = Flask(__name__, static_folder='/home/responsibuyer/responsibuyer-gh-pages')
geolocator = Nominatim()
@app.route('/static/<path:path>')
def send_static(path):
return send_from_directory('/home/responsibuyer/responsibuyer-gh-pages', path)
@app.route('/')
def root():
return redirect("/static/index.html", code=302)
conn = psycopg2.connect(database='responsibuyer')
@app.route('/search')
def search():
global conn
resp = {}
lat = request.args.get('lat')
lon = request.args.get('long')
point = lat + ", " + lon
location = geolocator.reverse(point, language='en')
country = location.raw['address']['country']
country_code = location.raw['address']['country_code']
for _ in range(0,2):
# Try the query twice if the connection was closed (e.g. DB restarted).
cur = conn.cursor()
try:
cur.execute("""
select p.name,
p.description,
w.genus,
w.species,
w.common_name,
w.conservation_status,
count(*) AS total_trades
from wildlife_trade wt
inner join wildlife w on wt.wildlife_id = w.id
inner join product_category p on wt.product_category_id = p.id
where wt.country_code ilike '{}'
GROUP BY p.name, p.description, w.genus, w.species, w.common_name, w.conservation_status
ORDER BY total_trades DESC;
""".format(country_code))
except psycopg2.OperationalError:
conn = psycopg2.connect(database='responsibuyer')
rows = cur.fetchall()
products = {}
for row in rows:
product_name = row[0]
if product_name not in products:
products[product_name] = {"name": product_name, 'desc': row[1], 'animals': []}
animal = {}
animal['genus'] = row[2]
animal['species'] = row[3]
animal['common_name'] = row[4]
animal['conservation_status'] = row[5]
animal['trade_count'] = row[6]
products[product_name]['animals'].append(animal)
sorted_products = sort_products(list(products.values()))
resp['products'] = sorted_products
resp['country'] = country
resp['country_code'] = country_code
callback = request.args.get('callback')
if callback:
# Return JSONP
json_data = json.dumps(resp)
jsonp = "{}({});".format(callback, json_data)
response = app.make_response(jsonp)
response.mimetype = "application/javascript"
return response
else:
return json.jsonify(resp)
def sort_products(products):
for product in products:
product['animals'].sort(key=lambda x:x['trade_count'], reverse=True)
total_animal_count = reduce(lambda x,y:x+y, [x['trade_count'] for x in product['animals']])
product['ranking'] = total_animal_count
products.sort(key=lambda x:x['ranking'], reverse=True)
return products
if __name__ == '__main__':
app.run(host='0.0.0.0')