-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouteme.py
175 lines (133 loc) · 5.16 KB
/
routeme.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
import re
from time import time
from random import choice
from collections import defaultdict
from flask import Flask, redirect, request, render_template
from flask import json
import numpy as np
import networkx as nx
#from dbhandle import DBHandle
app = Flask(__name__)
#dbh = DBHandle()
app.debug = True
execfile('router.py')
# HTML
@app.route("/")
def index():
return render_template('index.html', fails=False,
days=0, origin='', destin='', oneway=0)
ONEWAY = re.compile(ur"(?P<days>\d+)-days-from-(?P<origin>\w+)-(?P<ocountry>\w+)-to-(?P<destin>\w+)-(?P<dcountry>\w+)-by-(?P<mode>\w+)", re.U)
ROUND = re.compile(ur"(?P<days>\d+)-days-around-(?P<origin>\w+)-(?P<ocountry>\w+)-by-(?P<mode>\w+)", re.U)
def failure(args):
days = int(args['days'])
origin = (' '.join(args['origin'].split('_')).strip() + ', ' +
' '.join(args['ocountry'].split('_')).strip())
if 'destin' in args:
destin = (' '.join(args['destin'].split('_')).strip() + ', ' +
' '.join(args['dcountry'].split('_')).strip())
oneway = True
return render_template('index.html', fails=True,
origin=origin, destin=destin, days=days, oneway=origin != destin)
# HTML
@app.route("/<trip>", methods=['GET', 'POST'])
def figure(trip):
args = ROUND.match(trip)
oneway = False
if args is None:
args = ONEWAY.match(trip)
oneway = True
if args is None:
redirect("/")
args = args.groupdict()
city = dest = ' '.join(args['origin'].split('_')).strip()
try:
if 'ocountry' in args:
country = ' '.join(args['ocountry'].split('_')).strip()
origin = destin = CITYMAP[(city, country)]
else:
origin = destin = CITYMAP[city]
except KeyError:
return failure(args)
if oneway:
dest = ' '.join(args['destin'].split('_')).strip()
try:
if 'dcountry' in args:
country = ' '.join(args['dcountry'].split('_')).strip()
destin = CITYMAP[(dest, country)]
dfails = dest + ', ' + country
destin = CITYMAP[dest]
except KeyError:
return failure(args)
days = int(args['days'])
if not (3 <= days <= 10):
return failure(args)
mode = args['mode'][0].upper()
if mode not in "DT":
return failure(args)
routes, hours = get_scored_routes(origin, destin, mode, days, 4)
if routes:
return render_template('route.html',
cities=CITYJSON, routes=json.dumps(routes[:15]),
links=json.dumps(LINKS[mode, 10]), mode=mode,
city=city, dest=dest, days=days, hours=hours,
origin=origin, destin=destin,
scores=json.dumps(get_scaled_scores()),
factoids=json.dumps(FACTOIDS),
around=["false", "true"][origin == destin],
gmap=bool(int(request.args.get("gmap", 1))))
else:
return failure(args)
# JSON
def cities():
"""Return list of cities, e.g ["Berlin, Germany", "Bordeaux, France"]."""
q = request.args.get('q').split(',')[0]
sql = ("SELECT city.name, country.name "
"FROM city JOIN country ON city.countryCode=country.code "
"WHERE city.oglinks > 0 AND city.name LIKE '{0}%' " #OR Country.name LIKE '{0}%'"
#"LIMIT 10"
).format(q)
return json.dumps([u'{}, {}'.format(city, country)
for city, country in dbh(sql)])
def places():
city = int(request.args.get('city'))
cats = [(float(request.args.get('arts')), 'art'),
(float(request.args.get('history')), 'historic'),
(float(request.args.get('technical')), 'technical'),
(float(request.args.get('amusement')), 'amusement'),
(float(request.args.get('nature')), 'nature'),]
cats.sort(reverse=True)
places = {}
for want, cat in cats:
if not want:
break
places[cat] = dbh("SELECT subcategory, name, url FROM place "
"WHERE category='{}' AND cityId={}"
.format(cat, city))
return json.dumps(places)
def reroute():
origin = int(request.args.get('origin'))
destin = int(request.args.get('destin'))
days = int(request.args.get('days'))
mode = request.args.get('mode').upper()[0]
hours = int(request.args.get('hours'))
weights = np.array([float(request.args.get('arts')),
float(request.args.get('history')),
float(request.args.get('technical')),
float(request.args.get('amusement')),
float(request.args.get('nature'))])
routes, hours = get_scored_routes(origin, destin, mode, days, hours, weights)[:15]
return json.dumps({
'scores': get_scaled_scores(weights),
'routes': routes,
})
JSON = {
'cities': cities,
'reroute': reroute,
'places': places
}
@app.route("/json/<what>")
def rjson(what):
return JSON[what]()
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)
pass