|
1
|
|
|
''' |
|
2
|
|
|
Warning, this is an "private" class to the TFL object |
|
3
|
|
|
You shouldn't reference this directly in your code as |
|
4
|
|
|
there is minimal type checking and exception handling |
|
5
|
|
|
''' |
|
6
|
|
|
import requests, xmltodict |
|
7
|
|
|
from collections import OrderedDict |
|
8
|
|
|
|
|
9
|
|
|
class TFLapi(object): |
|
10
|
|
|
|
|
11
|
|
|
def __init__(self): |
|
12
|
|
|
self.detail_url = "http://cloud.tfl.gov.uk/TrackerNet/PredictionDetailed/%(line)s/%(station)s" |
|
13
|
|
|
|
|
14
|
|
|
def _getDetailedXML(self, station, line): |
|
15
|
|
|
url = self.detail_url % { 'station' : station, 'line' : line} |
|
16
|
|
|
rxml = requests.get(url).text.replace(u"\xef\xbb\xbf", "") #strange unicode error |
|
17
|
|
|
return rxml |
|
18
|
|
|
|
|
19
|
|
|
def getDetailed(self, station=None, line=None): |
|
20
|
|
|
if (station is None) or (line is None): |
|
21
|
|
|
return None |
|
22
|
|
|
|
|
23
|
|
|
return APIDetail(xml=self._getDetailedXML(station, line) ) |
|
24
|
|
|
|
|
25
|
|
|
|
|
26
|
|
|
class APIDetail(object): |
|
27
|
|
|
def __init__(self, xml=None): |
|
28
|
|
|
if xml is None: |
|
29
|
|
|
Exception("Need XML") |
|
30
|
|
|
self.xml = xml |
|
31
|
|
|
self.xmlDict = xmltodict.parse(self.xml) |
|
32
|
|
|
|
|
33
|
|
|
self.station = None |
|
34
|
|
|
self.line = None |
|
35
|
|
|
self._processHeader() |
|
36
|
|
|
|
|
37
|
|
|
self.platforms = [] |
|
38
|
|
|
self._processPlatforms() |
|
39
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
def _processHeader(self): |
|
42
|
|
|
self.line = self.xmlDict['ROOT']['Line'] |
|
43
|
|
|
self.station = self.xmlDict['ROOT']['S']['@Code'] |
|
44
|
|
|
|
|
45
|
|
|
def _processPlatforms(self): |
|
46
|
|
|
platforms = self.xmlDict['ROOT']['S']['P'] |
|
47
|
|
|
for plat in platforms: |
|
48
|
|
|
self.platforms.append( DetailPlatform(plat) ) |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
class DetailPlatform(object): |
|
52
|
|
|
def __init__(self, xmlDict): |
|
53
|
|
|
self.trains = [] |
|
54
|
|
|
self.name = None |
|
55
|
|
|
self.platform_number = None |
|
56
|
|
|
self.track_code = None |
|
57
|
|
|
self.next_train = None |
|
58
|
|
|
|
|
59
|
|
|
self.xmlDict = xmlDict |
|
60
|
|
|
if type(xmlDict) is OrderedDict: |
|
61
|
|
|
self.name = xmlDict['@N'] |
|
62
|
|
|
self.platform_number = xmlDict['@Num'] |
|
63
|
|
|
self.track_code = xmlDict['@TrackCode'] |
|
64
|
|
|
self.next_train = xmlDict['@NextTrain'] |
|
65
|
|
|
|
|
66
|
|
|
self._processTrains() |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
def _processTrains(self): |
|
70
|
|
|
if self.xmlDict.has_key('T'): |
|
71
|
|
|
trains = self.xmlDict['T'] |
|
72
|
|
|
for t in trains: |
|
73
|
|
|
try: |
|
74
|
|
|
self.trains.append( DetailTrain(t) ) |
|
75
|
|
|
except: |
|
76
|
|
|
#TODO: May be dropping a train on the floor here |
|
77
|
|
|
pass |
|
78
|
|
|
|
|
79
|
|
|
|
|
80
|
|
|
class DetailTrain(object): |
|
81
|
|
|
def __init__(self, xmlDict): |
|
82
|
|
|
if (type(xmlDict) is not OrderedDict): |
|
83
|
|
|
raise Exception("xmlDict does not contain a train") |
|
84
|
|
|
self.xmlDict = xmlDict |
|
85
|
|
|
#'true' if True else 'false' |
|
86
|
|
|
self.leadingcar_id = xmlDict["@LCID"] |
|
87
|
|
|
self.set_number = xmlDict['@SetNo'] |
|
88
|
|
|
self.trip_number = xmlDict['@TripNo'] |
|
89
|
|
|
self.arrival_seconds = xmlDict['@SecondsTo'] |
|
90
|
|
|
self.arrival_time = xmlDict['@TimeTo'] |
|
91
|
|
|
self.current_location = xmlDict['@Location'] |
|
92
|
|
|
self.destination = xmlDict['@Destination'] |
|
93
|
|
|
self.destination_code = xmlDict['@DestCode'] |
|
94
|
|
|
self.platform_departure_time = xmlDict['@DepartTime'] |
|
95
|
|
|
self.interval_between_previous_train = xmlDict['@DepartInterval'] |
|
96
|
|
|
self.departed_current_station = xmlDict['@Departed'] |
|
97
|
|
|
self.direction = xmlDict['@Direction'] |
|
98
|
|
|
self.track_code = xmlDict['@TrackCode'] |
|
99
|
|
|
|