-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
69 lines (59 loc) · 1.56 KB
/
api.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
import flask
import redis
import json
from flask import request
import dateutil.parser
import re
from random import choice
app = flask.Flask(__name__)
red = redis.StrictRedis(host='localhost', port=6379, db=0)
# args for orientation=all
sexes = [
"t", "tt", "m", "w", "mm", "ww", "wm", "mw"
]
orientations = ["%s4%s" %(s1, s2) for s1 in sexes for s2 in sexes] + ['None']
# slugify city arg:
def parse_city_to_slug(city):
city = re.sub('/', ' ', city)
city = re.sub('\\.', ' ', city).strip()
return re.sub('\s+', '-', city).lower().strip()
@app.route('/random')
def random():
key = red.randomkey()
results = red.zrangebyscore(
key,
min = 0,
max = 1e11
)
return choice(results)
@app.route("/")
def query():
# parse args
city = parse_city_to_slug(request.args.get('city', 'new york city'))
orientation = request.args.get('orientation', 'all')
start = request.args.get('start', 0)
end = request.args.get('end', 1e11)
order = request.args.get('order', 'desc')
if orientation=="all":
results = []
for o in orientations:
key = "%s:%s" % (city, o)
result = red.zrangebyscore(
key,
min = start,
max = end
)
results += result
else:
key = "%s:%s" % (city, orientation)
results = red.zrangebyscore(
key,
min = start,
max = end
)
if order=='desc':
results = [r for r in reversed(results)]
# turn it into a json list
return "[%s]" % ",".join(results)
if __name__ == "__main__":
app.run(host='0.0.0.0', port=5000)