1 | import requests, logging |
||
2 | from tflAPI import TFLapi |
||
3 | |||
4 | |||
5 | class Tube(object): |
||
6 | '''Root Tube object. Explore the underground with the .map object''' |
||
7 | def __init__(self): |
||
8 | logging.getLogger("requests").setLevel(logging.WARNING) |
||
9 | self._api = TFLapi() |
||
10 | self.map = TubeMap(self._api) |
||
11 | |||
12 | @property |
||
13 | def lines(self): |
||
14 | return self.map.allLines() |
||
15 | |||
16 | @property |
||
17 | def stations(self): |
||
18 | return self.map.allStations() |
||
19 | |||
20 | def getAllTrainsForStation(self, station): |
||
21 | if type(station) is TubeStation: |
||
22 | station = station.code |
||
23 | trains = self.map.get(stationcode=station).getAllTrains().values() |
||
24 | return trains |
||
25 | |||
26 | def getAllTrainsForLine(self, line): |
||
27 | if type(line) is TubeLine: |
||
28 | line = line.code |
||
29 | trains = self.map.get(linecode=line).getAllTrains().values() |
||
30 | return trains |
||
31 | |||
32 | class TubeMap(object): |
||
33 | '''Object representation of the underground |
||
34 | access single stations or line with .get |
||
35 | .get(stationcode='OXC') |
||
36 | .get(linecode='V') |
||
37 | get all lines and stations with .all* |
||
38 | .allLines() |
||
39 | .allStations() |
||
40 | |||
41 | get associated lines with stations via property |
||
42 | .get(stationcode='OXC').getLines() |
||
43 | |||
44 | get associated stations with lines via property |
||
45 | .get(linecode='V').getStations() |
||
46 | ''' |
||
47 | def __init__(self, api): |
||
48 | from tflStationNames import stations, lineStations |
||
49 | self._api = api |
||
50 | linesList = [TubeLine("C", "Central"), TubeLine("B", "Bakerloo"), TubeLine("D", "District"), TubeLine("H", "Hammersmith & Circle"), TubeLine("J", "Jubilee"), TubeLine("M", "Metropolitan"), TubeLine("N", "Nothern"), TubeLine("P", "Piccadilly"), TubeLine("W", "Waterloo & City"), TubeLine("V", "Victoria")] |
||
51 | stationsList = [TubeStation(code, name) for code, name in stations.iteritems()] |
||
52 | |||
53 | lines = {} #TODO: rewrite by comprehension |
||
54 | for line in linesList: |
||
55 | lines[line.code] = line |
||
56 | |||
57 | stations = {} #TODO: rewrite by comprehension |
||
58 | for station in stationsList: |
||
59 | stations[station.code] = station |
||
60 | |||
61 | for lcode in lineStations.keys(): |
||
62 | for scode in lineStations[lcode]: |
||
63 | line = lines[lcode] |
||
64 | station = stations[scode] |
||
65 | #add station to line |
||
66 | stations[scode]._lines.addLine(line) |
||
67 | #add line to station |
||
68 | lines[lcode]._stations.addStation(station) |
||
69 | |||
70 | #stations are mapped to lines, create root managers |
||
71 | self._lines = TubeLineManager() |
||
72 | self._stations = TubeStationManager() |
||
73 | |||
74 | self._lines.update(lines) |
||
75 | self._stations.update(stations) |
||
76 | |||
77 | def _validstationcode(self, stationcode): |
||
78 | if stationcode is not None: |
||
79 | return stationcode in self._stations.keys() |
||
80 | else: |
||
81 | return None |
||
82 | |||
83 | def _validlinecode(self, linecode): |
||
84 | if linecode is not None: |
||
85 | return linecode in self._lines.keys() |
||
86 | else: |
||
87 | return None |
||
88 | |||
89 | def allStations(self): |
||
90 | return self._stations |
||
91 | |||
92 | def allLines(self): |
||
93 | return self._lines |
||
94 | |||
95 | def getStation(self, stationcode=None): return self.get(stationcode=stationcode) |
||
96 | def getLine(self, linecode=None): return self.get(linecode=linecode) |
||
97 | |||
98 | def get(self, stationcode=None, linecode=None): |
||
99 | #valid station but, not line |
||
100 | if (self._validstationcode(stationcode)) and not (self._validlinecode(linecode)): |
||
101 | return self._stations[stationcode] |
||
102 | #valid line but, not station |
||
103 | if not (self._validstationcode(stationcode)) and (self._validlinecode(linecode)): |
||
104 | return self._lines[linecode] |
||
105 | #valid line and station |
||
106 | if (self._validstationcode(stationcode)) and (self._validlinecode(linecode)): |
||
107 | #check if station is on line |
||
108 | if linecode not in self._stations[stationcode]._lines.keys(): |
||
109 | return None |
||
110 | return TubeStationLinePlatform( self._stations[stationcode], |
||
111 | self._lines[linecode], |
||
112 | self._api) |
||
113 | raise KeyError("stationcode or linecode does not exist, refer to allLines or allStations for valid codes") |
||
114 | |||
115 | class TubeTrain(object): |
||
116 | def __init__(self): |
||
117 | self.line = None |
||
118 | self.leadingcar_id = None |
||
119 | self.set_number = None |
||
120 | self.trip_number = None |
||
121 | self.arrival_seconds = None |
||
122 | self.arrival_time = None |
||
123 | self.current_location = None |
||
124 | self.destination = None |
||
125 | self.destination_code = None |
||
126 | self.platform_departure_time = None |
||
127 | self.interval_between_previous_train = None |
||
128 | self.departed_current_station = None |
||
129 | self.direction = None |
||
130 | self.track_code = None |
||
131 | |||
132 | def __repr__(self): |
||
133 | return "<Tube.Train LCID(%s) on %s at %s>" % (self.leadingcar_id, self.line.name + " Line", self.current_location) |
||
134 | |||
135 | class TubePlatform(object): |
||
136 | def __init__(self, api, detailPlatform, line): |
||
137 | self.api = api |
||
138 | self._detailPlatform = detailPlatform |
||
139 | self.name = None |
||
140 | self.platform_number = None |
||
141 | self.track_code = None |
||
142 | self.next_train = None |
||
143 | self.trains = {} |
||
144 | self.line = line |
||
145 | |||
146 | self._getTrains() |
||
147 | |||
148 | def __repr__(self): |
||
149 | return "<Tube.Platform: %s %s >" % (self.line.name, self.name) |
||
150 | |||
151 | def _getTrains(self): |
||
152 | detailTrains = self._detailPlatform.trains |
||
153 | self._loadTrainFromDetail(detailTrains, self.line) |
||
154 | |||
155 | def _loadTrainFromDetail(self, detailTrains, line): |
||
156 | for d in detailTrains: |
||
157 | newT = TubeTrain() |
||
158 | newT.line = line |
||
159 | newT.leadingcar_id = d.leadingcar_id |
||
160 | newT.set_number = d.set_number |
||
161 | newT.trip_number = d.trip_number |
||
162 | newT.arrival_seconds = d.arrival_seconds |
||
163 | newT.arrival_time = d.arrival_time |
||
164 | newT.current_location = d.current_location |
||
165 | newT.destination = d.destination |
||
166 | newT.destination_code = d.destination_code |
||
167 | newT.platform_departure_time = d.platform_departure_time |
||
168 | newT.interval_between_previous_train = d.interval_between_previous_train |
||
169 | newT.departed_current_station = d.departed_current_station |
||
170 | newT.direction = d.direction |
||
171 | newT.track_code = d.track_code |
||
172 | self.trains[newT.leadingcar_id] = newT |
||
173 | |||
174 | class TubeStationLinePlatform(object): |
||
175 | def __init__(self, station, line, api): |
||
176 | self.api = api |
||
177 | self.station = station |
||
178 | self.line = line |
||
179 | self.platforms = {} |
||
180 | |||
181 | self._getPlatforms() |
||
182 | |||
183 | def _getPlatforms(self): |
||
184 | details = self.api.getDetailed(self.station.code, self.line.code) |
||
185 | self._loadPlatformsFromDetail(details.platforms) |
||
186 | |||
187 | def _loadPlatformsFromDetail(self, detailPlatforms): |
||
188 | for p in detailPlatforms: |
||
189 | newP = TubePlatform(self.api, p, self.line) |
||
190 | newP.name = p.name |
||
191 | newP.platform_number = p.platform_number |
||
192 | newP.track_code = p.platform_number |
||
193 | newP.next_train = p.track_code |
||
194 | self.platforms[newP.name] = newP |
||
195 | View Code Duplication | ||
0 ignored issues
–
show
Duplication
introduced
by
![]() |
|||
196 | def getAllTrains(self): |
||
197 | ret = {} |
||
198 | for plat in self.platforms.keys(): |
||
199 | ret.update(self.platforms[plat].trains) |
||
200 | return ret |
||
201 | |||
202 | |||
203 | class TubeStation(object): |
||
204 | def __init__(self, code, name): |
||
205 | self.code = code |
||
206 | self.name = name |
||
207 | self._lines = TubeLineManager() |
||
208 | |||
209 | def __repr__(self): |
||
210 | return "<Tube.Station: %s>" % self.name |
||
211 | |||
212 | def getLines(self): |
||
213 | return self._lines |
||
214 | |||
215 | def getAllTrains(self): |
||
216 | ret = {} |
||
217 | for l in self._lines: |
||
218 | ret.update(Tube().map.get(linecode=l, stationcode=self.code).getAllTrains() ) |
||
219 | return ret |
||
220 | |||
221 | View Code Duplication | ||
0 ignored issues
–
show
|
|||
222 | class TubeStationManager(dict): |
||
223 | def __init__(self): |
||
224 | pass |
||
225 | def addStation(self, station): |
||
226 | if not self.has_key(station.code): |
||
227 | self[station.code] = station |
||
228 | |||
229 | class TubeLine(object): |
||
230 | def __init__(self, code, name): |
||
231 | self.code = code |
||
232 | self.name = name |
||
233 | self._stations = TubeStationManager() |
||
234 | |||
235 | def __repr__(self): |
||
236 | return "<Tube.Line: %s>" % self.name |
||
237 | |||
238 | def getStations(self): |
||
239 | return self._stations |
||
240 | |||
241 | def getAllTrains(self): |
||
242 | ret = {} |
||
243 | for stat in self._stations: |
||
244 | tfl = Tube() |
||
245 | ret.update( tfl.map.get(linecode=self.code, stationcode=stat).getAllTrains() ) |
||
246 | return ret |
||
247 | |||
248 | class TubeLineManager(dict): |
||
249 | def __init__(self): |
||
250 | pass |
||
251 | def addLine(self, line): |
||
252 | if not self.has_key(line.code): |
||
253 | self[line.code] = line |
||
254 |