-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
executable file
·114 lines (90 loc) · 3.52 KB
/
views.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
import os
from flask import render_template, request, flash, jsonify, Flask
import MySQLdb
import json
import re
import analytics as ant
import numpy as np
import mysqlconnect as msc
#from mapquest_utils import *
import urllib2
app = Flask(__name__)
#db = msc.mysqlcon()
@app.route("/")
@app.route("/index")
@app.route("/landing")
def hello():
return render_template('landing.html')
# submit input and load itin picker
@app.route("/choose", methods=["POST","GET"])
def testpost():
origin = request.form["origin"]
origin_name = origin
destination = request.form["destination"]
destination_name = destination
roundtrip = request.form.get('roundtrip')
if not roundtrip:
roundtrip = 0
# do geocode with mapquest
#mquest=mapquest()
#s1latlng = mquest.revgeo(origin)
#e1latlng = mquest.revgeo(destination)
#origin = str(s1latlng[0]) + ',' + str(s1latlng[1])
#destination = str(e1latlng[0]) + ',' + str(e1latlng[1])
# geocode with google
try:
origin = urllib2.quote(origin)
geocode_url = "http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false®ion=uk" % origin
req = urllib2.urlopen(geocode_url)
jsonResponse = json.loads(req.read())
origin = str(jsonResponse['results'][0]['geometry']['location']['lat']) + ',' + str(jsonResponse['results'][0]['geometry']['location']['lng'])
destination = urllib2.quote(destination)
geocode_url = "http://maps.googleapis.com/maps/api/geocode/json?address=%s&sensor=false®ion=uk" % destination
req = urllib2.urlopen(geocode_url)
jsonResponse = json.loads(req.read())
destination = str(jsonResponse['results'][0]['geometry']['location']['lat']) + ',' + str(jsonResponse['results'][0]['geometry']['location']['lng'])
return render_template('index_map.html',origin=origin, destination=destination, roundtrip=roundtrip, origin_name=origin_name, destination_name=destination_name)
except:
return render_template('landing_error.html')
# return the current agenda
@app.route("/db_path/<vc>", methods=["GET"])
def path_json(vc):
db = msc.mysqlcon()
cities = []
vc = vc.split(',')
for vci in vc:
que = "select nid, name, lat, lng, url, photo from foursquare join foursquare_id on foursquare.id=foursquare_id.id where nid=%s" % vci
db.query(que)
query_results = db.store_result().fetch_row(maxrows=1)
#print query_results
for result in query_results:
nname = result[1].encode('ascii','ignore')
cities.append( dict(City=unicode(str(result[0]), 'utf8'), CountryCode=nname, lat=result[2], lng=result[3], url=result[4], photo=result[5]) )
db.close()
return jsonify(dict(cities=cities))
# suggestions based on route and sites
@app.route("/db_suggest/<vin>", methods=["GET"])
def sugg_json(vin):
vin = vin.split('&')
origin = vin[0]
destination = vin[1]
if len(vin)==2:
vsites = []
else:
vsites = vin[2]
cities = ant.suggest(vsites,origin,destination)
return jsonify(dict(cities=cities))
@app.route('/slides')
def showslides():
return render_template('slides.html')
@app.route('/ken')
def ken():
return render_template('ken.html')
@app.route('/<pagename>')
def regularpage(pagename=None):
"""
Route not found by the other routes above. May point to a static template.
"""
return "You've arrived at " + pagename
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)