-
Notifications
You must be signed in to change notification settings - Fork 0
/
transilienAPI.py
116 lines (86 loc) · 3.03 KB
/
transilienAPI.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
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
#!/usr/bin/python3
from TransilienAccount import TransilienAccount
from codeUIC import get_uic_from_name, get_name_from_uic
import lxml.etree
import lxml.html
import urllib
""" this module retrieves times of the trains from Transilien API """
BASE_URL = 'http://api.transilien.com'
URLDEPART = BASE_URL + '/gare/{}/depart'
URLDEPARTARRIVEE = URLDEPART + '/{}'
def get_url_depart_uic(uic_depart:int):
doc = URLDEPART.format(uic_depart)
return doc
def get_url_depart(depart: str):
uic_depart = get_uic_from_name(depart)
if uic_depart > 0:
return get_url_depart_uic(uic_depart)
return None
def get_url_depart_arrivee_uic(uic_depart: int, uic_arrivee: int):
doc = URLDEPARTARRIVEE.format(uic_depart, uic_arrivee)
return doc
def get_url_depart_arrivee(depart: str, arrivee: str):
uic_depart = get_uic_from_name(depart)
uic_arrivee = get_uic_from_name(arrivee)
if uic_depart > 0 and uic_arrivee > 0:
return get_url_depart_arrivee_uic(uic_depart, uic_arrivee)
return None
def get_url(val):
if type(val) is str:
return get_url_depart(val)
elif type(val) is tuple:
if len(val) == 1:
return get_url_depart(*val)
elif len(val) == 2:
return get_url_depart_arrivee(*val)
return None
def get_url_from_key(pathInfo: str):
return get_url(urls[pathInfo])
def horaires_dict(pathInfo: str):
(login, password) = TransilienAccount.account
manager = urllib.request.HTTPPasswordMgrWithDefaultRealm()
manager.add_password(None, BASE_URL, login, password)
auth = urllib.request.HTTPBasicAuthHandler(manager)
opener = urllib.request.build_opener(auth)
urllib.request.install_opener(opener)
http_url = get_url_from_key(pathInfo)
xml = urllib.request.urlopen(http_url).read()
root = lxml.etree.XML(xml)
trains = root.xpath('//passages/train')
result = []
for train in trains:
uic_dest_str = train.xpath('term/text()')
uic_dest_int = int(*uic_dest_str)
dest = get_name_from_uic(uic_dest_int)
result.append({
'heure': train.xpath('date/text()')[0],
'reel': train.xpath('date/@mode')[0],
'num': train.xpath('num/text()')[0],
'code': train.xpath('miss/text()')[0],
'dest': dest,
})
return result
def horaires(pathInfo:str ):
list = horaires_dict(pathInfo)
return [dico.values() for dico in list]
def getUrls():
return urls
CHARS = 'Chars'
PONTOISE = 'Pontoise'
SARTROUVILLE = 'Sartrouville'
PSL = 'Paris Saint-Lazare'
CERGYPREF = 'Cergy Préfecture'
NANTERREU = 'Nanterre Université'
urls = {
"t_chars": CHARS,
"t_pontoise": PONTOISE,
"t_p-chars": (PONTOISE, CHARS),
"t_sartrouville": SARTROUVILLE,
"t_s-cergypref": (SARTROUVILLE, CERGYPREF),
"t_psl": PSL,
"t_psl-nu": (PSL, NANTERREU),
"t_psl-pontoise": (PSL, PONTOISE),
}
if __name__ == '__main__':
for (pathInfo, data) in urls.items():
print(pathInfo + ' : ' + repr(horaires(pathInfo)))