This repository was archived by the owner on Oct 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
176 lines (138 loc) · 4.66 KB
/
app.py
File metadata and controls
176 lines (138 loc) · 4.66 KB
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
176
import json
import math
import pickle
import requests as req
from flask import Flask, request
from flask_cors import CORS
from flask_json import FlaskJSON, JsonError, json_response, as_json
from math import sin, cos, sqrt, atan2, radians
auth = "db37117c-679d-41ae-b7cc-2c010daea9ed"
app = Flask(__name__)
CORS(app)
def add_cors_headers(response):
response.headers['Access-Control-Allow-Origin'] = '*'
if request.method == 'OPTIONS':
response.headers['Access-Control-Allow-Methods'] = 'DELETE, GET, POST, PUT'
headers = request.headers.get('Access-Control-Request-Headers')
if headers:
response.headers['Access-Control-Allow-Headers'] = headers
return response
app.after_request(add_cors_headers)
@app.route("/")
def hello_world():
return "Hello, World!"
@app.route("/pairings/")
def get_pairings():
with open('cache.pickle', 'rb') as handle:
pairs = pickle.load(handle)
print("Sending: ")
if len(pairs) < 10:
for pair in pairs:
print(pair)
else:
for pair in pairs[0:10]:
print(pair)
print("...")
return {"pairings": pairs}
def get_restaurant(id):
response = req.get(
"https://roo-api-sandbox.deliveroo.net/restaurants/{}".format(id),
params={},
headers={"content-type": "application/json", "api-key": auth},
)
json_data = json.loads(response.text)
return json_data
def is_restuarant_closed(id):
restaurant = get_restaurant(id)
return restaurant["status"] == "CLOSED"
def get_restaurant_lat_long(id):
restaurant = get_restaurant(id)
loc = restaurant["location"]
return (float(loc["lat"]), float(loc["long"]))
def get_orders():
response = req.get(
"https://roo-api-sandbox.deliveroo.net/orders",
params={},
headers={"content-type": "application/json", "api-key": auth},
)
json_data = json.loads(response.text)
return json_data
def get_riders():
response = req.get(
"https://roo-api-sandbox.deliveroo.net/riders",
params={},
headers={"content-type": "application/json", "api-key": auth},
)
json_data = json.loads(response.text)
return json_data
def get_rider(id):
response = req.get(
"https://roo-api-sandbox.deliveroo.net/riders/{}".format(id),
params={},
headers={"content-type": "application/json", "api-key": auth},
)
json_data = json.loads(response.text)
return json_data
def get_rider_lat_long(id):
rider = get_rider(id)
loc = rider["location"]
return (float(loc["lat"]), float(loc["long"]))
def distance(restaurantlatlong, riderlatlong):
# approximate radius of earth in km
R = 6373.0
lat1 = radians(restaurantlatlong[0])
lon1 = radians(restaurantlatlong[1])
lat2 = radians(riderlatlong[0])
lon2 = radians(riderlatlong[1])
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat / 2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
distance = R * c
return float("{:0.2f}".format(distance))
def pairings(orders, riders):
usable_riders = riders
pairings = []
for order in orders:
smallest = {"d": 100}
i = 0
while i < len(usable_riders):
rider = usable_riders[i]
loc = rider["location"]
riderlatlong = (float(loc["lat"]), float(loc["long"]))
restaurantlatlong = get_restaurant_lat_long(order["restaurant_id"])
d = distance(restaurantlatlong, riderlatlong)
if d < smallest["d"]:
smallest = rider
usable_riders.remove(rider)
smallest["d"] = d
break
i += 1
pairings.append({"uid": int(str(order["id"]) + str(rider["id"])),
"order": {"id": order["id"],
"description": order["order_items"]},
"distance": d,
"restaurant": {
"id": order["restaurant_id"],
"location": {
"lat": restaurantlatlong[0],
"long": restaurantlatlong[1]
}},
"rider": {
"id": rider["id"],
"name": rider["name"],
"location": {
"lat": riderlatlong[0],
"long": riderlatlong[1]
}
}
})
return pairings
if __name__ == "__main__":
# orders = get_orders()
# riders = get_riders()
#pairs = pairings(orders, riders)
# with open("cache.pickle", "wb") as p:
# pickle.dump(pairs, p, protocol=pickle.HIGHEST_PROTOCOL)
# print("Done")
app.run(threaded=True, port=5000)