-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoublemap.py
More file actions
147 lines (126 loc) · 5.18 KB
/
doublemap.py
File metadata and controls
147 lines (126 loc) · 5.18 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
from flask import Flask, render_template
from flask_ask import Ask, statement, question, session
import logging
from stops import bus_stops_class
from routes import routes_class
from buses import bus_class
from eta import eta_class
from datetime import datetime
app = Flask(__name__)
ask = Ask(app, "/")
log = logging.getLogger()
log.addHandler(logging.StreamHandler())
log.setLevel(logging.DEBUG)
logging.getLogger("flask_ask").setLevel(logging.DEBUG)
class DoubleMap(object):
def __init__(self):
self.url = "https://bloomington.doublemap.com/map/v2/"
self.routes = self.returnRouteDetails(self.url)[0]
self.routesDetailsByID = self.returnRouteDetailsById(self.url)
self.stops = self.returnStopsDetails(self.url)
self.shortnames = self.returnRouteDetails(self.url)[1]
self.stopNames = self.returnStopNames(self.url)
self.busDetails = self.returnBusDetails(self.url)
def returnURL(self):
return self.url
def returnStopsDetails(self, url):
# get the stops details
stopsDetailsObj = bus_stops_class(url)
stopsDetails = stopsDetailsObj.fetch_stops_details()
return stopsDetails
def returnRouteDetailsById(self, url):
routeDetailByIdObj = routes_class(url)
routeDetialsByID = routeDetailByIdObj.fetch_routes_details_by_id()
return routeDetialsByID
def returnRouteDetails(self, url):
# get the route details
routeDetailsObj = routes_class(url)
routeDetails = routeDetailsObj.fetch_routes_details()
shortNames = routeDetailsObj.shortname
return routeDetails, shortNames
def returnBusDetails(self, url):
# get the bus details
busDetailsObj = bus_class(url)
busDetails = busDetailsObj.fetch_bus_details()
return busDetails
def returnStopNames(self, url):
stopNameObj = bus_stops_class(url)
stopNameDetails = stopNameObj.fetch_stops_details_names()
return stopNameDetails
def returnEtaByStop(self, url, stopID):
etaObj = eta_class(url)
etabByStopDetail = etaObj.fetch_eta_by_stop_details(stopID)
return etabByStopDetail
def getCurrentLocationOfBus(self, shortName):
shortName = shortName.upper()
count = 0
route_list = list()
lastStopID = []
if len(self.shortnames) == 0 or len(self.busDetails) == 0:
return "The buses have ended or not started yet!"
if shortName not in self.shortnames:
return "Please enter a valid bus name"
else:
route_list = self.routes[shortName]
print(route_list)
routeid_list = []
count = 0
if len(route_list) > 1:
while count < len(route_list):
routeid_list.append(route_list[count]['id'])
count += 1
else:
routeid_list.append(route_list[count]['id'])
#print(routeid_list)
busInfo = self.returnBusDetails(self.returnURL())
lastStopID = []
for route_id in routeid_list:
temp_list = busInfo[route_id]
for stopID in temp_list:
lastStopID.append(stopID['lastStop'])
list_ = []
for lastStop in lastStopID:
list_.append("Bus {} is at {}".format(shortName, self.stops[int("10"+str(lastStop))]['name']))
return list_
def etaByStop(self, stopName):
list_ = []
stopName = stopName.lower()
for name, values in self.stopNames.items():
if stopName in name.lower():
etaDetails = self.returnEtaByStop(self.returnURL(), values['id'])
for eta in etaDetails:
list_.append("{} will arrive at {} in {} mins".format(self.routesDetailsByID[eta['route']]['short_name'], name, eta['avg']))
return list_
doubleMapObj = DoubleMap()
#return the url and while passing add the revelent addition to the URL
getURL = doubleMapObj.returnURL()
@ask.launch
def launch():
return statement("Welcome to double map alexa app.")
@ask.intent('getCurrentLocationOfBus')
def getBusLocation(busname):
busname = busname.upper()
return statement("...".join(doubleMapObj.getCurrentLocationOfBus(busname)))
@ask.intent('getETAbyStop')
def getETAbyStop(stopName):
return statement("...".join(doubleMapObj.etaByStop(stopName)))
if __name__ == "__main__":
app.run(debug=True)
# doubleMapObj = DoubleMap()
# #return the url and while passing add the revelent addition to the URL
# getURL = doubleMapObj.returnURL()
# # t1 = datetime.now()
# # doubleMapObj.getCurrentLocationOfBus("3")
# # t2 = datetime.now()
# # delta = t2 - t1
# # print("Time elapsed: ",delta.seconds + delta.microseconds/1E6)
# # t1 = datetime.now()
# doubleMapObj.getCurrentLocationOfBus("a")
# # t2 = datetime.now()
# # delta = t2 - t1
# # print("Time elapsed: ",delta.seconds + delta.microseconds/1E6)
# t1 = datetime.now()
# print(doubleMapObj.etaByStop("Wells Library"))
# t2 = datetime.now()
# delta = t2 - t1
# print("Time elapsed: ",delta.seconds + delta.microseconds/1E6)