From 1ede09d2c27066f161e65aec0a309c9cdc32a54b Mon Sep 17 00:00:00 2001 From: Christian Struck Date: Mon, 4 Feb 2013 21:16:12 +0100 Subject: made Response object json serializable reworked tests and removed to_json from departure object --- bvggrabber/api/__init__.py | 22 ++++++++-------------- bvggrabber/api/actualdeparture.py | 4 ++-- bvggrabber/api/scheduleddeparture.py | 6 +++--- bvggrabber/utils/json.py | 19 +++++++++++++++++++ 4 files changed, 32 insertions(+), 19 deletions(-) create mode 100644 bvggrabber/utils/json.py (limited to 'bvggrabber') 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): @@ -48,6 +49,10 @@ class Response(object): else: 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() diff --git a/bvggrabber/utils/json.py b/bvggrabber/utils/json.py new file mode 100644 index 0000000..d5bec53 --- /dev/null +++ b/bvggrabber/utils/json.py @@ -0,0 +1,19 @@ +# -*- coding: utf-8 -*- + +import inspect + +from json import JSONEncoder + + +def is_not_method(o): + return not inspect.isroutine(o) + + +class ObjectJSONEncoder(JSONEncoder): + + def default(self, reject): + non_methods = inspect.getmembers(reject, is_not_method) + return {attr: value for attr, value in non_methods + if (not attr.startswith('__') + and attr != 'when' + and attr != 'now')} -- cgit v1.2.3