diff options
author | Christian Struck <christian@struck.se> | 2013-02-04 21:16:12 +0100 |
---|---|---|
committer | Christian Struck <christian@struck.se> | 2013-02-04 21:16:12 +0100 |
commit | 1ede09d2c27066f161e65aec0a309c9cdc32a54b (patch) | |
tree | 14ae6defb41af13b8abad77059f2ca486d426b9f /bvggrabber/api | |
parent | e1981063359b0910614e5ea3f861a5ce1e54970d (diff) | |
download | bvg-grabber-1ede09d2c27066f161e65aec0a309c9cdc32a54b.tar.gz bvg-grabber-1ede09d2c27066f161e65aec0a309c9cdc32a54b.tar.bz2 bvg-grabber-1ede09d2c27066f161e65aec0a309c9cdc32a54b.zip |
made Response object json serializable reworked tests and removed to_json from departure object
Diffstat (limited to 'bvggrabber/api')
-rw-r--r-- | bvggrabber/api/__init__.py | 22 | ||||
-rw-r--r-- | bvggrabber/api/actualdeparture.py | 4 | ||||
-rw-r--r-- | bvggrabber/api/scheduleddeparture.py | 6 |
3 files changed, 13 insertions, 19 deletions
diff --git a/bvggrabber/api/__init__.py b/bvggrabber/api/__init__.py index c489e8b..3cd1239 100644 --- a/bvggrabber/api/__init__.py +++ b/bvggrabber/api/__init__.py @@ -9,6 +9,7 @@ from math import floor from dateutil.parser import parse from bvggrabber.utils.format import fullformat, timeformat +from bvggrabber.utils.json import ObjectJSONEncoder def compute_remaining(start, end): @@ -32,9 +33,9 @@ class QueryApi(object): class Response(object): - def __init__(self, state, departures, error=None): + def __init__(self, state, station=None, departures=None, error=None): self._state = state - self._departures = departures + self._departures = [(station, departures)] self._error = error def merge(self, other): @@ -49,6 +50,10 @@ class Response(object): raise TypeError("The given object is not a response object") @property + def to_json(self): + return ObjectJSONEncoder(ensure_ascii=False).encode(self.departures) + + @property def state(self): return self._state @@ -88,18 +93,7 @@ class Departure(object): @property def remaining(self): - return compute_remaining(self.now, self.when) - - @property - def to_json(self): - return json.dumps({'start': self.start, - 'end': self.end, - 'line': self.line, - 'now_full': fullformat(self.now), - 'now_hour': timeformat(self.now), - 'when_full': fullformat(self.when), - 'when_hour': timeformat(self.when), - 'remaining': round(self.remaining)}) + return int(compute_remaining(self.now, self.when)) def __eq__(self, other): """Two departures are assumed to be equal iff their remaining time diff --git a/bvggrabber/api/actualdeparture.py b/bvggrabber/api/actualdeparture.py index 7f091cc..d4a3002 100644 --- a/bvggrabber/api/actualdeparture.py +++ b/bvggrabber/api/actualdeparture.py @@ -3,7 +3,7 @@ import requests from bs4 import BeautifulSoup -from bvggrabber.api import QueryApi, Departure +from bvggrabber.api import QueryApi, Departure, Response ACTUAL_API_ENDPOINT = 'http://mobil.bvg.de/IstAbfahrtzeiten/index/mobil' @@ -52,7 +52,7 @@ class ActualDepartureQueryApi(QueryApi): when=td[0].text.strip(), line=td[1].text.strip()) departures.append(dep) - return Response(True, departures) + return Response(True, self.station, departures) else: try: response.raise_for_status() diff --git a/bvggrabber/api/scheduleddeparture.py b/bvggrabber/api/scheduleddeparture.py index e144d58..c001f55 100644 --- a/bvggrabber/api/scheduleddeparture.py +++ b/bvggrabber/api/scheduleddeparture.py @@ -5,9 +5,9 @@ import datetime from bs4 import BeautifulSoup -from bvggrabber.api import QueryApi, Departure, Response +from bvggrabber.api import QueryApi, Departure, Response, timeformat -from bvggrabber.utils.format import dateformat, int2bin, timeformat +from bvggrabber.utils.format import dateformat, int2bin SCHEDULED_API_ENDPOINT = 'http://mobil.bvg.de/Fahrinfo/bin/stboard.bin/dox' @@ -75,7 +75,7 @@ class ScheduledDepartureQueryApi(QueryApi): when=tds[0].text.strip(), line=tds[1].text.strip()) departures.append(dep) - return Response(True, departures) + return Response(True, self.station, departures) else: try: response.raise_for_status() |