-
Notifications
You must be signed in to change notification settings - Fork 0
/
sl.py
43 lines (28 loc) · 1.14 KB
/
sl.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
# -*- coding: UTF-8 -*-
import httplib
import json
class SlData:
host = 'api.sl.se'
stationUrl = '/api2/typeahead.json?key=XXX&searchstring='
realtimeUrl = '/api2/realtimedeparturesV4.json?key=XXX&TimeWindow=60&siteid='
@staticmethod
def findStation(stationSearch):
connection = httplib.HTTPConnection(SlData.host)
connection.request('GET', SlData.stationUrl + stationSearch)
response = connection.getresponse()
stationData = json.loads(response.read(), 'utf-8')
connection.close()
if len(stationData['ResponseData']) == 0:
raise InputError('Hittade ingen hållplats')
return stationData['ResponseData'][0]
@staticmethod
def nextBusesFromStation(station, direction):
return SlData.nextBusesFromSiteId(station['SiteId'], direction)
@staticmethod
def nextBusesFromSiteId(siteId, direction):
connection = httplib.HTTPConnection(SlData.host)
connection.request('GET', SlData.realtimeUrl + siteId)
response = connection.getresponse()
realtimeData = json.loads(response.read(), 'utf-8')
connection.close()
return filter(lambda bus: bus['JourneyDirection'] == direction, realtimeData['ResponseData']['Buses'])