-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
220 lines (185 loc) · 7.25 KB
/
main.py
File metadata and controls
220 lines (185 loc) · 7.25 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
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
import requests, json, time, pandas as pd
'''
## Todo:
1. Pull the API key in a defined frequency
2. Generate stops master in a defined frequency
'''
retry_count = 1
# Functions
def refresh_api_key(output_file):
try:
r = requests.get('http://nextbus.com/').text
except Exception:
return 'Unable to contact the server'
try:
pos = r.find('?key=')
api_key = r[pos+5:pos+37]
write_api_key(output_file, api_key)
return api_key
except Exception:
return 'Unable to find the API key'
def read_api_key(filename):
return open(filename, 'r').read()
def write_api_key(filename, api_key):
try:
f = open(filename, 'w')
f.write(api_key)
f.close()
return 'success'
except Exception as e:
return 'Error: ' + str(e)
def generate_stops_master():
try:
# Get all Routes
all_routes = nextbus_api(endpoints['routes'])
# Generate master dataframes
stops_master = {}
stops_master_df = pd.DataFrame()
for i, routes in all_routes.iterrows():
route = str(routes['id'])
params = {'route': route}
route_df = nextbus_api(get_api_url(endpoints['route'], params))
# Generate Stops Master Dataframe
stop_df = pd.DataFrame(route_df[0]['stops'])
stop_df = stop_df[stop_df['showDestinationSelector']]
stop_df['route'] = route
stops_master[route] = stop_df
stops_master_df = stops_master_df.append(stop_df, ignore_index=True)
stops_master_df.to_csv('app_data/stops_master.csv', index=False)
f = open('app_data/stops_master.json', 'w')
f.write(json.dumps(stops_master_df.to_dict(orient='records')))
f.close()
return "I'm all refreshed!"
except Exception as e:
return "Oh-oh! I had the following error while trying to refresh: " + str(e)
def get_api_url(base_url, params):
for param in params:
base_url = base_url.replace('<'+param+'>', params[param])
return base_url
def voice_response(tag, minutes):
next_bus = minutes[0]
due_msg = 'now. ' if next_bus == '0' else 'in ' + next_bus + ' minutes. '
msg = 'Your next bus {} is arriving {}'.format(tag, due_msg)
if len(minutes) == 2:
msg += 'Following it is arriving in {}.'.format(minutes[1])
if len(minutes) > 2:
msg += 'Following it are arriving in '
msg += ', '.join(minutes[1:]) + ' minutes.'
pos = msg.rfind(',')
msg = msg[:pos] + ' and' + msg[pos+1:]
return msg
def process_input(input_msg):
if 'update routes' in input_msg:
return generate_stops_master()
else:
stops_master_df = pd.read_csv('app_data/stops_master.csv')
# Check for tag in input message
response = -1
for tag in tags:
if tag['tag'] in input_msg:
response = tag
# Tag found!
if response != -1:
template = response['template']
tag = response['tag']
# Select Stop and Destination from stops_master
user_route = trips[template]['route']
stop_name = trips[template]['stop']
destination_name = trips[template]['destination']
stop = stops_master_df[stops_master_df['route'] == user_route]
#direction_id = stop[stop['name'] == stop_name].reset_index()['directions'].iloc[0][0]
stop_id = stop[stop['name'] == stop_name].reset_index()['id'].iloc[0]
destination_id = stop[stop['name'] == destination_name].reset_index()['id'].iloc[0]
direction_id = user_route + '_0_var0'
# Get Stop prediction
params = { 'route':user_route, 'stop':stop_id, 'destination':destination_id, 'direction':direction_id }
try:
minutes = nextbus_api(get_api_url(endpoints['stops'], params))
minutes = minutes['minutes'].astype(str).tolist()
return voice_response(tag, minutes)
except Exception:
return "Sorry, I had trouble fetching at this time. Please try again."
# Tag not found :(
else:
return "Sorry, I couldn't find a template from your message. Could you repeat that?"
def read_user_config(userId):
try:
userData = json.loads(open('user_data/' + str(userId)+'.json', 'r').read())
except Exception as e:
return 'Error: ' + str(e)
return userData
def write_user_config(userId, userData):
try:
print(userData)
f = open('user_data/'+userId+'.json', 'w')
f.write(json.dumps(userData))
f.close()
return 'success'
except Exception as e:
return 'Error: ' + str(e)
def get_endpoints():
try:
return json.loads(open('app_data/endpoints.json', 'r').read())
except Exception as e:
return 'Error: ' + str(e)
def nextbus_api(endpoint, as_dataframe=True):
api_key = read_api_key('app_data/api_key.txt')
#api_key = '123123123123123123123123123123' # TODO - Handle incorrect API key error with this test case
#api_key = '123' # TODO - Handle incorrect API key length error
if len(api_key) is not 32:
# API error
return 'error'
else:
# Payload
headers = {"Referer": "http://www.nextbus.com/"}
params = {"key": api_key, "timestamp": int(time.time())}
# Request
try:
response = requests.get(endpoint, headers=headers, params=params)
response = json.loads(response.text)
if as_dataframe:
try:
df = pd.DataFrame(response)
except ValueError:
df = pd.DataFrame.from_dict(response, orient='index')
if len(df) == 1:
response = response[0]
if 'values' in response:
return pd.DataFrame.from_dict(response['values'])
else:
return pd.DataFrame.from_dict(response)
else:
return pd.DataFrame({"minutes":[row[0]['minutes'] for row in df['values'].tolist()]})
return response
except Exception as e:
return 'Error: ' + str(e)
# TODO:
# max_retries = 2
# global retry_count
# while retry_count <= max_retries:
# ## API error
# # Generate new API key
# refresh_api_key('api_key.txt')
# # Re-run user's request
# nextbus_api(endpoint, as_dataframe)
# retry_count += 1
def update_user_data(userId, userInputData):
# Get current user data from storage
userData = read_user_config(userId)
# Update with new user provided data
key = list(userInputData.keys())[0]
userData[key] = userInputData[key]
# Write to storage
write_user_config(userId, userData)
return 'success'
# User Input:
userId = '93828'
input_msg = "When's the Next bus to home?"
#input_msg = "update routes"
# App Config:
endpoints = get_endpoints()
user_config = read_user_config(userId) # TODO: Handle error
trips = user_config['trips']
tags = user_config['tags']
# App Execution:
print(process_input(input_msg.lower()))