From e1981063359b0910614e5ea3f861a5ce1e54970d Mon Sep 17 00:00:00 2001 From: Christian Struck Date: Sat, 2 Feb 2013 04:17:03 +0100 Subject: 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. --- bvggrabber/api/scheduleddeparture.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'bvggrabber/api/scheduleddeparture.py') 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")) -- cgit v1.2.3