forked from neonbadger/DestinationUnknown
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
309 lines (217 loc) · 9.55 KB
/
server.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
"""Flask server for Destination Unknown"""
from flask import Flask
from flask import session as sesh
from flask import redirect
from flask import request, url_for
from flask import render_template
from flask import jsonify
from uber_rides.session import Session
from uber_rides.client import UberRidesClient
from uber_rides.auth import AuthorizationCodeGrant
from uber_rides.session import OAuth2Credential
from utils import import_app_credentials
from utils import import_oauth2_credentials
from yelp_api import yelp_random_pick
from twilio_api import send_uber_text
from geopy.geocoders import Nominatim
from geopy.distance import vincenty
from model import connect_to_db, db, User, Search
from sqlalchemy import func
import requests
import os
app = Flask(__name__)
app.requests_session = requests.Session()
app.secret_key = os.urandom(24)
geolocator = Nominatim()
# Step 1 in 3-legged OAuth handshake:
# Prepare OAuth 2.0 service that is used to make requests
credentials = import_app_credentials()
auth_flow = AuthorizationCodeGrant(
credentials.get('client_id'),
credentials.get('scopes'),
credentials.get('client_secret'),
credentials.get('redirect_url'),
)
# Generate authorization url
auth_url = auth_flow.get_authorization_url()
@app.route('/')
def index():
"""Homepage."""
return render_template('home.html')
@app.route('/authenticate')
def login():
"""Redirect to https://login.uber.com/login"""
return redirect(auth_url)
@app.route('/redirect-uri', methods=['GET'])
def redirect_uri():
"""Step 2 & 3 in 3-legged Oauth handshake. Implement callback
at redirect_uri to exchange a code to obtain access token."""
parameters = {
'redirect_uri': 'http://localhost:5000/redirect-uri',
'code': request.args.get('code'),
'grant_type': 'authorization_code',
}
client_id = credentials.get('client_id')
client_secret = credentials.get('client_secret')
response = requests.post(
'https://login.uber.com/oauth/token',
auth = (
client_id,
client_secret
),
data = parameters
)
# Extract access token and store in session
sesh['access_token'] = response.json().get('access_token')
return redirect('/search')
@app.route('/search')
def search():
"""Show search form and create user object if not in database."""
if 'access_token' not in sesh:
return redirect(url_for('index'))
else:
credentials2 = import_oauth2_credentials()
oauth2credential = OAuth2Credential(
client_id=credentials2.get('client_id'),
access_token=sesh.get('access_token'),
expires_in_seconds=credentials2.get('expires_in_seconds'),
scopes=credentials2.get('scopes'),
grant_type=credentials2.get('grant_type'),
redirect_url=credentials2.get('redirect_url'),
client_secret=credentials2.get('client_secret'),
refresh_token=credentials2.get('refresh_token'),
)
uber_session = Session(oauth2credential=oauth2credential)
uber_client = UberRidesClient(uber_session, sandbox_mode=True)
user_profile = uber_client.get_user_profile().json
sesh['user'] = {
'first_name': user_profile.get('first_name'),
'last_name': user_profile.get('last_name'),
'email': user_profile.get('email'),
'phone': user_profile.get('phone'),
'img_url': user_profile.get('picture')
}
if db.session.query(User).filter(User.email == user_profile['email']).count() == 0:
user = User(first_name=user_profile.get('first_name'),
last_name= user_profile.get('last_name'),
img_url=user_profile.get('picture'),
email=user_profile.get('email'))
db.session.add(user)
db.session.commit()
return render_template('search.html',
first_name=sesh['user'].get('first_name'),
img_url=sesh['user'].get('img_url'),
)
@app.route('/yelp_result', methods = ['POST'])
def generate_yelp():
"""Return top-rated Yelp search result and create search object in database."""
mood = request.form.get('mood')
adjective = request.form.get('adjective')
alter_ego = request.form.get('alter_ego')
location = request.form.get('location')
start = geolocator.geocode(request.form.get('location'))
start_lat = start.latitude
start_lng = start.longitude
event = request.form.get('event')
city = request.form.get('city')
phone = request.form.get('phone')
biz = yelp_random_pick(event, city)
end_lat = biz.location.coordinate.latitude
end_lng = biz.location.coordinate.longitude
distance = vincenty((start_lat, start_lng), (end_lat, end_lng)).miles
sesh['user']['phone'] = phone
user = User.query.filter(User.email == sesh['user'].get('email')).first()
user_id = user.id
if user.phone == None:
user.phone = phone
db.session.commit()
search = Search(user_id=user_id,
mood=mood,
adjective=adjective,
alter_ego=alter_ego,
event=event,
location=location,
start_lat=start_lat,
start_lng=start_lng,
destination=biz.name,
end_lat=end_lat,
end_lng=end_lng,
mileage=distance,
)
db.session.add(search)
db.session.commit()
return render_template('yelp_result.html', biz=biz,
review=biz.snippet_text.replace('\n', ' '),
pic = sesh['user'].get('img_url'),
first_name=sesh['user'].get('first_name'),
location=location,
start_lat=start_lat,
start_lng=start_lng,
end_lat=end_lat,
end_lng=end_lng,
)
@app.route('/request_uber', methods=['POST', 'GET'])
def request_uber():
"""Make sandbox Uber ride request"""
search = Search.query.order_by(Search.date.desc()).first()
search.uber_request = True
db.session.commit()
credentials2 = import_oauth2_credentials()
oauth2credential = OAuth2Credential(
client_id=credentials2.get('client_id'),
access_token=sesh.get('access_token'),
expires_in_seconds=credentials2.get('expires_in_seconds'),
scopes=credentials2.get('scopes'),
grant_type=credentials2.get('grant_type'),
redirect_url=credentials2.get('redirect_url'),
client_secret=credentials2.get('client_secret'),
refresh_token=credentials2.get('refresh_token'),
)
uber_session = Session(oauth2credential=oauth2credential)
uber_client = UberRidesClient(uber_session, sandbox_mode=True)
# receive data from Ajax call
start_lat = request.form.get('start_lat')
start_lng = request.form.get('start_lng')
end_lat = request.form.get('end_lat')
end_lng = request.form.get('end_lng')
response = uber_client.get_products(37.3688301, -122.0363495)
products = response.json.get('products')
product_id = products[0].get('product_id')
# make sandbox calls
ride_request = uber_client.request_ride(product_id=product_id,
start_latitude=37.3688301,
start_longitude=-122.0363495,
end_latitude=37.8003415,
end_longitude=-122.4331332)
ride_details = ride_request.json
ride_id = ride_details.get('request_id')
get_ride = uber_client.update_sandbox_ride(ride_id, 'accepted')
send_uber_text();
return jsonify(ride_details)
@app.route('/show_stats')
def show_stats():
"""Show user's curiosity stats and D3 graphs."""
if 'access_token' not in sesh:
return redirect(url_for('index'))
else:
bold_stat = Search.query.filter_by(uber_request = 'T').count()
curious_stat = Search.query.filter_by(uber_request = 'F').count()
uber_miles = db.session.query(func.sum(Search.mileage)).filter(Search.uber_request == 'T').scalar()
moods = db.session.query(func.count(Search.mood), Search.mood).group_by(Search.mood).all()
alter_ego = db.session.query(func.count(Search.alter_ego), Search.alter_ego).group_by(Search.alter_ego).all()
return render_template("stats.html",
bold_stat=bold_stat,
curious_stat=curious_stat,
uber_miles=uber_miles)
@app.route('/check_token', methods=['GET'])
def check_access_token():
"""A test route that checks whether access token is fresh."""
token = sesh['access_token']
return render_template('check_token.html', token=token)
if __name__ == "__main__":
app.debug = False
connect_to_db(app)
db.create_all()
db.session.commit()
app.run()
url_for('static', filename='event.csv')