-
Notifications
You must be signed in to change notification settings - Fork 0
/
luas_api.py
87 lines (73 loc) · 2.75 KB
/
luas_api.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
import requests
import xmltodict
import luas_stops
class Luas(object):
def __init__(self, stop):
self.url = 'https://luasforecasts.rpa.ie/xml/get.ashx'
self.stop_info_json = luas_stops.stops['stations']
self.stop_abbreviation = self.stop_names_abbreviation(stop.lower())
self.response = self.request_stop_info
self.response_content = self.stop_request_content
self.mapping = {
"Inbound": self.trams_inbound,
"Outbound": self.trams_outbound
}
def stop_names_abbreviation(self, stop):
for stop_info in self.stop_info_json:
if stop in stop_info['displayName'].lower():
return stop_info['shortName']
@property
def request_stop_info(self):
params = {
'stop': self.stop_abbreviation,
'action': 'forecast',
'encrypt': 'false'
}
return requests.get(self.url, params=params)
@property
def stop_request_content(self):
xml_text = self.response.text
return xmltodict.parse(xml_text)
@property
def time_of_request(self):
return self.response_content['stopInfo']['@created']
@property
def stop(self):
return self.response_content['stopInfo']['@stop']
@property
def message(self):
return self.response_content['stopInfo']['message']
@property
def trams_inbound(self):
return self.response_content['stopInfo']['direction'][0]['tram']
@property
def trams_outbound(self):
return self.response_content['stopInfo']['direction'][1]['tram']
@property
def schedule(self):
message = '{}\n{}\n'.format(self.stop, self.message)
message = self.direction_trams_message("Inbound", message)
message = self.direction_trams_message("Outbound", message)
return message
def direction_trams_message(self, direction, message):
message += '\n==============================\n'
message += f'{direction}'
message += '\n==============================\n'
for luas in self.mapping[direction]:
try:
message += '{} - {}\n'.format(luas['@destination'], luas['@dueMins'])
except TypeError:
if self.mapping[direction]['@dueMins'] == '':
message += '{}\n'.format(
self.mapping[direction]['@destination']
)
else:
message += '{} - {}\n'.format(
self.mapping[direction]['@destination'],
self.mapping[direction]['@dueMins']
)
return message
return message
if __name__ == '__main__':
rti = Luas('central park')
print(rti.schedule)