summaryrefslogtreecommitdiff
path: root/bvggrabber
diff options
context:
space:
mode:
authorChristian Struck <christian@struck.se>2013-01-25 14:45:42 +0100
committerChristian Struck <christian@struck.se>2013-01-25 14:45:42 +0100
commitda008b711a28cd2f20d56c6adda0a3d517230102 (patch)
tree92690d480a4dba0acf435ac9c7da5f02e0918ae6 /bvggrabber
parentda1fe427ca3a6ba0ac52259c21e7b6cbef54dbac (diff)
downloadbvg-grabber-da008b711a28cd2f20d56c6adda0a3d517230102.tar.gz
bvg-grabber-da008b711a28cd2f20d56c6adda0a3d517230102.tar.bz2
bvg-grabber-da008b711a28cd2f20d56c6adda0a3d517230102.zip
Added support for scheduled departure times which allows to show U+S departure times.
Diffstat (limited to 'bvggrabber')
-rw-r--r--bvggrabber/api/scheduleddeparture.py51
1 files changed, 51 insertions, 0 deletions
diff --git a/bvggrabber/api/scheduleddeparture.py b/bvggrabber/api/scheduleddeparture.py
index 40a96af..594dd5d 100644
--- a/bvggrabber/api/scheduleddeparture.py
+++ b/bvggrabber/api/scheduleddeparture.py
@@ -1 +1,52 @@
# -*- coding: utf-8 -*-
+import requests
+
+from bs4 import BeautifulSoup
+
+from bvggrabber.api import QueryApi, Departure
+
+SCHEDULE_QUERY_API_ENDPOINT = 'http://mobil.bvg.de/Fahrinfo/bin/stboard.bin/dox'
+
+
+class ScheduleDepartureQueryApi(QueryApi):
+
+ def __init__(self, station):
+ super(ScheduleDepartureQueryApi, self).__init__()
+ if isinstance(station, str):
+ self.station_enc = station.encode('iso-8859-1')
+ elif isinstance(station, bytes):
+ self.station_enc = station
+ else:
+ raise ValueError("Invalid type for station")
+ self.station = station
+
+ def call(self):
+ params = {'input': self.station_enc, 'time': '12:00', 'date': '25.01.2013','productsFilter': 11, 'maxJourneys': 7, 'start': 'yes'}
+ response = requests.get(SCHEDULE_QUERY_API_ENDPOINT, params=params)
+ if response.status_code == requests.codes.ok:
+ soup = BeautifulSoup(response.text)
+ if soup.find('span', 'error'):
+ # The station we are looking for is ambiguous or does not exist
+ stations = soup.find('span', 'select').find_all('a')
+ if stations:
+ # The station is ambiguous
+ stationlist = [s.text.strip() for s in stations]
+ return (False, stationlist)
+ else:
+ # The station does not exist
+ return (False, [])
+ else:
+ # The station seems to exist
+ #rows = soup.find('tbody').find_all('tr')
+ rows = soup.find('tbody').find_all('tr')
+ departures = []
+ for row in rows:
+ tds = row.find_all('td')
+ dep = Departure(start=self.station,
+ end=tds[2].text.strip(),
+ when=tds[0].text.strip(),
+ line=tds[1].text.strip())
+ departures.append(dep)
+ return (True, departures)
+ else:
+ response.raise_for_status()