-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathirail.py
49 lines (38 loc) · 1.75 KB
/
irail.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
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
import urllib2
import json
import arrow
import bottle
# full station ID list here : https://github.com/iRail/stations/blob/master/stations.csv
departureID = "008814209" # Nivelles
destinationIDs = ("008886009", "008863545") # Ath & Yvoir
def getConnections() :
connections = []
for destinationID in destinationIDs :
data = urllib2.urlopen("https://api.irail.be/connections/?from=%s&to=%s&format=json" % (departureID, destinationID))
jsonList = json.loads(data.read())
connections.extend(jsonList["connection"][0:3])
connections.sort(key = lambda connection : connection["departure"]["time"])
return connections
def extractConnectionData(connection) :
destinationStationName = connection["arrival"]["stationinfo"]["name"]
durationSeconds = int(connection["duration"])
hours, minutes = durationSeconds / 3600, (durationSeconds % 3600) / 60
durationString = "%02d:%02d" % (hours, minutes)
delayMinutes = int(connection["departure"]["delay"]) / 60
departureTimestamp = connection["departure"]["time"]
departureTimeString = arrow.get(departureTimestamp).to("Europe/Brussels").format("HH:mm")
return {
"destinationStationName" : destinationStationName,
"durationString" : durationString,
"delayMinutes" : delayMinutes,
"departureTimeString" : departureTimeString
}
@bottle.route('/')
def upcomingTrains():
connectionsJSON = getConnections()
connectionsExtracted = [extractConnectionData(connection) for connection in connectionsJSON]
# bottle converts the dict into JSON and adds the "application/json" accept header.
return dict(data=connectionsExtracted)
bottle.run(host='localhost', port=8090)