-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.py
100 lines (78 loc) · 3.4 KB
/
application.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
import os
import re
from flask import Flask, jsonify, render_template, request
from cs50 import SQL
from helpers import lookup
# Configure application
app = Flask(__name__)
# Configure CS50 Library to use SQLite database
db = SQL("sqlite:///mashup.db")
# Ensure responses aren't cached
@app.after_request
def after_request(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
@app.route("/")
def index():
"""Render map"""
return render_template("index.html")
@app.route("/articles")
def articles():
"""Look up articles for geo"""
location = request.args.get('geo')
articles = lookup(location);
# TODO
return jsonify([articles][0])
@app.route("/search")
def search():
"""Search for places that match query"""
q = request.query_string[2:].decode("utf-8").replace("%20", " ").replace("%2C", "") + "%"
qList = q.split("+")
#lets see if that location is a zip code
if len(qList) == 1:
dbQuery = "SELECT * FROM places WHERE admin_name1 LIKE :q OR admin_name2 LIKE :q OR admin_name3 LIKE :q OR place_name LIKE :q OR postal_code LIKE :q"
dbContent = db.execute(dbQuery,q=qList[0])
else:
dbQuery = "SELECT * FROM places WHERE (admin_name2 LIKE :city OR place_name LIKE :city) AND (admin_name1 LIKE :state OR admin_code1 LIKE :state)"
dbContent = db.execute(dbQuery, city=qList[0], state=qList[1])
return jsonify([dbContent][0])
@app.route("/update")
def update():
"""Find up to 10 places within view"""
# Ensure parameters are present
if not request.args.get("sw"):
raise RuntimeError("missing sw")
if not request.args.get("ne"):
raise RuntimeError("missing ne")
# Ensure parameters are in lat,lng format
if not re.search("^-?\d+(?:\.\d+)?,-?\d+(?:\.\d+)?$", request.args.get("sw")):
raise RuntimeError("invalid sw")
if not re.search("^-?\d+(?:\.\d+)?,-?\d+(?:\.\d+)?$", request.args.get("ne")):
raise RuntimeError("invalid ne")
# Explode southwest corner into two variables
sw_lat, sw_lng = map(float, request.args.get("sw").split(","))
print(sw_lat, sw_lng)
# Explode northeast corner into two variables
ne_lat, ne_lng = map(float, request.args.get("ne").split(","))
# Find 10 cities within view, pseudorandomly chosen if more within view
if sw_lng <= ne_lng:
# Doesn't cross the antimeridian
rows = db.execute("""SELECT * FROM places
WHERE :sw_lat <= latitude AND latitude <= :ne_lat AND (:sw_lng <= longitude AND longitude <= :ne_lng)
GROUP BY country_code, place_name, admin_code1
ORDER BY RANDOM()
LIMIT 10""",
sw_lat=sw_lat, ne_lat=ne_lat, sw_lng=sw_lng, ne_lng=ne_lng)
else:
# Crosses the antimeridian
rows = db.execute("""SELECT * FROM places
WHERE :sw_lat <= latitude AND latitude <= :ne_lat AND (:sw_lng <= longitude OR longitude <= :ne_lng)
GROUP BY country_code, place_name, admin_code1
ORDER BY RANDOM()
LIMIT 10""",
sw_lat=sw_lat, ne_lat=ne_lat, sw_lng=sw_lng, ne_lng=ne_lng)
# Output places as JSON
print(rows)
return jsonify(rows)