summaryrefslogtreecommitdiff
path: root/bvggrabber
diff options
context:
space:
mode:
authorChristian Struck <christian@struck.se>2013-02-02 04:17:03 +0100
committerChristian Struck <christian@struck.se>2013-02-02 04:17:03 +0100
commite1981063359b0910614e5ea3f861a5ce1e54970d (patch)
treeced65c7d63233ced0892ed8800c01b7ccfe1d5c6 /bvggrabber
parent9caefee9893048f169bc451cd9c454e0af668deb (diff)
downloadbvg-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')
-rw-r--r--bvggrabber/api/__init__.py31
-rw-r--r--bvggrabber/api/actualdeparture.py17
-rw-r--r--bvggrabber/api/scheduleddeparture.py21
3 files changed, 55 insertions, 14 deletions
diff --git a/bvggrabber/api/__init__.py b/bvggrabber/api/__init__.py
index 36c913b..c489e8b 100644
--- a/bvggrabber/api/__init__.py
+++ b/bvggrabber/api/__init__.py
@@ -30,6 +30,37 @@ class QueryApi(object):
"the call() method!")
+class Response(object):
+
+ def __init__(self, state, departures, error=None):
+ self._state = state
+ self._departures = departures
+ self._error = error
+
+ def merge(self, other):
+ if isinstance(other, Response):
+ if not other.state:
+ raise ValueError("The response contains errors: " + str(other.error))
+ elif not self.state:
+ raise ValueError("The response contains errors: " + str(self.error))
+ else:
+ self.departures.extend(other.departures)
+ else:
+ raise TypeError("The given object is not a response object")
+
+ @property
+ def state(self):
+ return self._state
+
+ @property
+ def departures(self):
+ return self._departures
+
+ @property
+ def error(self):
+ return self._error
+
+
@total_ordering
class Departure(object):
diff --git a/bvggrabber/api/actualdeparture.py b/bvggrabber/api/actualdeparture.py
index 93213ef..7f091cc 100644
--- a/bvggrabber/api/actualdeparture.py
+++ b/bvggrabber/api/actualdeparture.py
@@ -24,7 +24,7 @@ class ActualDepartureQueryApi(QueryApi):
def call(self):
params = {'input': self.station_enc}
response = requests.get(ACTUAL_API_ENDPOINT, params=params)
- if response.status_code == requests.codes.ok:
+ if response.ok:
soup = BeautifulSoup(response.text)
if soup.find_all('form'):
# The station we are looking for is ambiguous or does not exist
@@ -32,16 +32,16 @@ class ActualDepartureQueryApi(QueryApi):
if stations:
# The station is ambiguous
stationlist = [s.get('value') 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
result = soup.find('div', {'id': '',
'class': 'ivu_result_box'})
if result is None:
- return (False, [])
+ return Response(False, [])
rows = result.find_all('tr')
departures = []
for row in rows:
@@ -52,6 +52,11 @@ class ActualDepartureQueryApi(QueryApi):
when=td[0].text.strip(),
line=td[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"))
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"))