-
Notifications
You must be signed in to change notification settings - Fork 1
/
bus_status.py
301 lines (228 loc) · 9.93 KB
/
bus_status.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
import logging
import re
import json
from random import randint
from flask import Flask
from flask_dynamo import Dynamo
from flask_ask import Ask, statement, question, session, context
from geopy.geocoders import Nominatim
from Stop import Stop
import iso8601
import datetime
import time
import math
import requests
MTA_API_KEY = "a7274011-3c23-4fd2-9641-dcbe0d873f47"
app = Flask(__name__)
ask = Ask(app, "/")
logging.getLogger("flask_ask").setLevel(logging.DEBUG)
# DynamoDB config
app.config['DYNAMO_TABLES'] = [
{
'TableName': 'buses',
'KeySchema': [dict(AttributeName='user_id', KeyType='HASH')],
'AttributeDefinitions': [dict(AttributeName='user_id', AttributeType='S')],
'ProvisionedThroughput': dict(ReadCapacityUnits=5, WriteCapacityUnits=5)
}
]
dynamo = Dynamo(app)
# create tables if doesn't exist
with app.app_context():
dynamo.create_all()
@ask.launch
def welcome():
# check if user already has a saved bus
response = dynamo.tables['buses'].get_item(Key={'user_id': session.user.userId })
if 'Item' in response and 'bus_stop' in response['Item']:
item = response['Item']
session.attributes['bus_route'] = item['bus_route']
session.attributes['bus_stop'] = item['bus_stop']
session.attributes['stop_code'] = item['stop_code']
return question("Welcome to Find My Bus!")
else:
return question("Welcome to Find My Bus! You do not have a bus route and stop setup yet. Would you like to do so now?")
@ask.intent("YesIntent")
def yes_proceed():
yes_msg = "Great! What's your preferred bus route?"
return question(yes_msg)
@ask.intent("PickIntent", convert={'num':'int'})
def pick_number(num):
try:
number = int(num)
if session.attributes['nearbyStops']:
nearby_stops = json.loads(session.attributes['nearbyStops'])
selected_stop = nearby_stops[number-1]
msg = "Your default bus stop is {}. ".format(selected_stop['audioName'])
# Persist info into database
dynamo.tables['buses'].put_item(Item={
'user_id': session.user.userId,
'bus_route': session.attributes['bus_route'],
'bus_stop': selected_stop['audioName'],
'stop_code': selected_stop['code']
})
session.attributes['bus_stop'] = selected_stop['audioName']
session.attributes['stop_code'] = selected_stop['code']
msg += "You have completed the setup. You may now ask Where is the bus upon starting this skill. Try it now!"
return question(msg)
else:
msg = "Please allow Alexa to get your location in order to choose a bus stop"
return statement(msg)
except:
msg = "I didnt understand that, please try again"
return question(msg)
@ask.intent("NoIntent")
def no_proceed():
return statement('Okay. Good day.')
@ask.intent("BusTimeIntent")
def bus_time_intent():
try:
# make sure session objects are ready
if 'bus_route' not in session.attributes or 'bus_stop' not in session.attributes or 'stop_code' not in session.attributes:
return question('You do not have your preferred route and stop set yet. Would you like to set them now?')
# check if user already has a saved bus
response = dynamo.tables['buses'].get_item(Key={'user_id': session.user.userId })
if 'Item' in response and 'bus_stop' in response['Item']:
item = response['Item']
session.attributes['bus_route'] = item['bus_route']
session.attributes['bus_stop'] = item['bus_stop']
session.attributes['stop_code'] = item['stop_code']
# make sure session objects are ready
elif 'bus_route' not in session.attributes or 'bus_stop' not in session.attributes or 'stop_code' not in session.attributes:
return question('You do not have your preferred route and stop set yet. Would you like to set them now?')
bus_route = session.attributes['bus_route']
bus_stop = session.attributes['bus_stop']
stop_code = session.attributes['stop_code']
return statement(get_eta_message(bus_route, stop_code, bus_stop))
except:
return statement("I didn't understand that. Please try again")
@ask.intent("AnswerIntent", convert={'borough': 'string', 'num': 'int'})
def answer(borough, num):
try:
complete = borough+ str(num)
groups = re.search(r'([A-z])\.{0,1}([0-9]+)$', complete.replace(' ',''))
session.attributes['bus_route'] = groups.group(1) + ' ' + groups.group(2)
print(session.attributes['bus_route'])
if session.attributes['bus_route'].upper() == 'M 60':
session.attributes['bus_route'] = 'M60-SBS'
msg = "<speak>Your bus is {} {}. ".format(borough, num)
stops = find_stops()
if len(stops) == 0:
return question('There are no nearby stops for {}. Would you like to try another route?'.format(session.attributes['bus_route']))
session.attributes['nearbyStops'] = json.dumps(stops, default=obj_dict)
msg += display_stops(stops)+'</speak>'
print(msg)
return question(msg)
except:
return question("I didnt understand that. Please try again")
def obj_dict(obj):
return obj.__dict__
def mapStops(stops):
nearby_stops = {}
for idx,stop in enumerate(stops):
nearby_stops[idx+1] = stop
return nearby_stops
def filterStops(stops):
filteredStops = []
bus = session.attributes['bus_route'].replace(' ','')
for stop in stops:
if bus.upper() in map(str.upper, stop.buses):
filteredStops.append(stop)
return filteredStops
def getLocation():
deviceId = context.System.device.deviceId
URL = "https://api.amazonalexa.com/v1/devices/{}/settings" \
"/address".format(deviceId)
TOKEN = context.System.user.permissions.consentToken
HEADER = {'Accept': 'application/json',
'Authorization': 'Bearer {}'.format(TOKEN)}
r = requests.get(URL, headers=HEADER)
if r.status_code == 200:
resp = r.json()
return resp
def find_stops():
location = getLocation()
geopy_loc = location['addressLine1'] + " "+ location['stateOrRegion']
geopy_loc = geopy_loc.replace(' E ', ' ')
geopy_loc = geopy_loc.replace(' W ', ' ')
geolocator = Nominatim()
geo_location = geolocator.geocode(geopy_loc)
return get_nearby_stops(geo_location.latitude, geo_location.longitude)
def get_nearby_stops(latitude, longitude):
lat = str(latitude)
lon = str(longitude)
URL = "http://bustime.mta.info/api/where/stops-for-location.json?key="+MTA_API_KEY+"&lat="+lat+"&lon="+lon+"&radius=321.869"
r = requests.get(URL)
if r.status_code == 200:
return get_list_of_stops(r.json())
def display_stops(stops):
stopStr = 'The available stops are '
for idx,stop in enumerate(stops):
stopStr+= '<break time="0.5s" />Choose, '+ str(idx+1)+' for <break time="0.5s"/> '+stop.audioName+'. \n'
return stopStr
def get_list_of_stops(stops):
stop_list = []
for stop in stops['data']['stops']:
name = stop['name']
direction = stop['direction']
code = stop['code']
buses = []
for routes in stop['routes']:
buses.append(routes['shortName'])
print(routes['shortName'])
stop = Stop(name,code,buses,direction)
stop_list.append(stop)
return filterStops(stop_list)[:5]
def get_eta_message(bus_route, stop_id, bus_stop):
"""Get number of minutes until next bus arrival
Returns -1 if no bus available (or if bus doesn't even stop here)
Args:
stop_id -- GTFS bus ID code (6 digits)
bus_route -- bus route (like Q58 or M96)
bus_stop -- bus stop name
"""
# fix bus formatting
bus_route = bus_route.upper().replace(' ', '')
bus_route = bus_route.upper().replace('-SBS', '+')
# MTA buses are either MTA NYCT_Q50 or MTABC_Q50
# Let's try them both
r = requests.get('http://bustime.mta.info/api/siri/stop-monitoring.json',
params = {
'key': MTA_API_KEY,
'OperatorRef': 'MTA',
'MonitoringRef': stop_id,
'LineRef': 'MTA NYCT_{}'.format(bus_route)
})
r = r.json()
# check for errors
if 'ErrorCondition' in r['Siri']['ServiceDelivery']['StopMonitoringDelivery'][0]:
r = requests.get('http://bustime.mta.info/api/siri/stop-monitoring.json',
params = {
'key': MTA_API_KEY,
'OperatorRef': 'MTA',
'MonitoringRef': stop_id,
'LineRef': 'MTABC_{}'.format(bus_route)
})
r = r.json()
# still bad route?
if 'ErrorCondition' in r['Siri']['ServiceDelivery']['StopMonitoringDelivery'][0]:
return "No such route"
arrivals = r['Siri']['ServiceDelivery']['StopMonitoringDelivery'][0]['MonitoredStopVisit']
if len(arrivals) == 0:
return "No known {} arrivals expected.".format(bus_route)
arrival = arrivals[0]['MonitoredVehicleJourney']['MonitoredCall']
final_message = ''
# distance if time is unavailable
distance = arrival['Extensions']['Distances']['PresentableDistance']
if 'ExpectedArrivalTime' in arrival:
iso_time = arrival['ExpectedArrivalTime']
# calculated ETA
arrival_time = time.mktime(iso8601.parse_date(iso_time).timetuple())
current_time = time.mktime(datetime.datetime.now().timetuple())
eta = math.floor((arrival_time - current_time) / 60 * 100) / 100
final_message += "The {} will arrive in {} minutes. ".format(bus_route.replace('+', 'SBS'), eta)
final_message += "It is {} from {}.".format(distance, bus_stop)
else:
final_message += "The {} is {} from {}".format(bus_route.replace('+', 'SBS'), distance, bus_stop)
return final_message
if __name__ == '__main__':
app.run(debug=True)