diff options
author | Markus Holtermann <info@markusholtermann.eu> | 2013-01-24 21:33:11 +0100 |
---|---|---|
committer | Markus Holtermann <info@markusholtermann.eu> | 2013-01-24 21:33:11 +0100 |
commit | 324c61e220fcb66973007cab4b95ca249aef3274 (patch) | |
tree | 485aa6bce8abd5bf0b96da178f8b091ca274d386 | |
parent | d6f0b39753f1b55f86a0eba9a822c367a26c45dc (diff) | |
download | bvg-grabber-324c61e220fcb66973007cab4b95ca249aef3274.tar.gz bvg-grabber-324c61e220fcb66973007cab4b95ca249aef3274.tar.bz2 bvg-grabber-324c61e220fcb66973007cab4b95ca249aef3274.zip |
Add first attempt of ActualDepartureQueryApi
-rw-r--r-- | bvggrabber/api/actualdeparture.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/bvggrabber/api/actualdeparture.py b/bvggrabber/api/actualdeparture.py index 40a96af..a282c7f 100644 --- a/bvggrabber/api/actualdeparture.py +++ b/bvggrabber/api/actualdeparture.py @@ -1 +1,53 @@ # -*- coding: utf-8 -*- + + +import requests + +from bs4 import BeautifulSoup + +from bvggrabber.api import QueryApi, Departure + + +ACTUAL_QUERY_API_ENDPOINT = 'http://mobil.bvg.de/IstAbfahrtzeiten/index/mobil' + + +class ActualDepartureQueryApi(QueryApi): + + def __init__(self, station): + super(ActualDepartureQueryApi, 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} + response = requests.get(ACTUAL_QUERY_API_ENDPOINT, params=params) + if response.status_code == requests.codes.ok: + soup = BeautifulSoup(response.text) + if soup.find_all('form'): + # The station we are looking for is ambiguous or does not exist + stations = soup.find_all('option') + if stations: + # The station is ambiguous + stationlist = [s.get('value') 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') + departures = [] + for row in rows: + tds = row.find_all('td') + dep = Departure(start=self.station, + end=tds[2].text.strip(), + line=tds[1].text.strip()) + departures.append(dep) + return (True, departures) + else: + response.raise_for_status() |