diff options
author | Christian Struck <christian@struck.se> | 2013-02-02 04:17:03 +0100 |
---|---|---|
committer | Christian Struck <christian@struck.se> | 2013-02-02 04:17:03 +0100 |
commit | e1981063359b0910614e5ea3f861a5ce1e54970d (patch) | |
tree | ced65c7d63233ced0892ed8800c01b7ccfe1d5c6 /bvggrabber/api/scheduleddeparture.py | |
parent | 9caefee9893048f169bc451cd9c454e0af668deb (diff) | |
download | bvg-grabber-e1981063359b0910614e5ea3f861a5ce1e54970d.tar.gz bvg-grabber-e1981063359b0910614e5ea3f861a5ce1e54970d.tar.bz2 bvg-grabber-e1981063359b0910614e5ea3f861a5ce1e54970d.zip |
Added a new object to return which is easier to handle an implements a merge function if it is wished to mix actualdeparture with scheduleddeparture departures.
Diffstat (limited to 'bvggrabber/api/scheduleddeparture.py')
-rw-r--r-- | bvggrabber/api/scheduleddeparture.py | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/bvggrabber/api/scheduleddeparture.py b/bvggrabber/api/scheduleddeparture.py index ba8a453..e144d58 100644 --- a/bvggrabber/api/scheduleddeparture.py +++ b/bvggrabber/api/scheduleddeparture.py @@ -5,7 +5,7 @@ import datetime from bs4 import BeautifulSoup -from bvggrabber.api import QueryApi, Departure +from bvggrabber.api import QueryApi, Departure, Response from bvggrabber.utils.format import dateformat, int2bin, timeformat @@ -37,7 +37,7 @@ class ScheduledDepartureQueryApi(QueryApi): else: raise ValueError("Invalid type for station") self.station = station - self.vehicles = int2bin(vehicles) + self.vehicles = int2bin(vehicles, 7) self.limit = limit def call(self): @@ -49,7 +49,7 @@ class ScheduledDepartureQueryApi(QueryApi): 'maxJourneys': self.limit, 'start': 'yes'} response = requests.get(SCHEDULED_API_ENDPOINT, params=params) - if response.status_code == requests.codes.ok: + if response.ok: soup = BeautifulSoup(response.text) if soup.find('span', 'error'): # The station we are looking for is ambiguous or does not exist @@ -57,15 +57,15 @@ class ScheduledDepartureQueryApi(QueryApi): if stations: # The station is ambiguous stationlist = [s.text.strip() for s in stations] - return (False, stationlist) + return Response(False, stationlist) else: # The station does not exist - return (False, []) + return Response(False, []) else: # The station seems to exist tbody = soup.find('tbody') if tbody is None: - return (False, []) + return Response(False, []) rows = tbody.find_all('tr') departures = [] for row in rows: @@ -75,6 +75,11 @@ class ScheduledDepartureQueryApi(QueryApi): when=tds[0].text.strip(), line=tds[1].text.strip()) departures.append(dep) - return (True, departures) + return Response(True, departures) else: - response.raise_for_status() + try: + response.raise_for_status() + except RequestException as e: + return Response(False, [], e) + else: + return Response(False, [], Exception("An unknown error occured")) |