From 47d1ad8ed082c43f1ff3bd157db077d1e494186a Mon Sep 17 00:00:00 2001 From: Markus Holtermann Date: Fri, 25 Jan 2013 19:46:02 +0100 Subject: Add to_json method for departures --- bvggrabber/api/__init__.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'bvggrabber') diff --git a/bvggrabber/api/__init__.py b/bvggrabber/api/__init__.py index 2793e3c..2d263fb 100644 --- a/bvggrabber/api/__init__.py +++ b/bvggrabber/api/__init__.py @@ -1,4 +1,6 @@ # -*- coding: utf-8 -*- +import json + from datetime import datetime from dateutil.parser import parse @@ -7,7 +9,7 @@ fullformat = lambda dt: dt.strftime('%Y-%m-%d %H:%M') hourformat = lambda dt: dt.strftime('%H:%M') -class QueryApi(): +class QueryApi(object): def __init__(self): pass @@ -17,11 +19,12 @@ class QueryApi(): "the call() method!") -class Departure(): +class Departure(object): def __init__(self, start, end, when, line): self.start = start self.end = end + self.line = line self.now = datetime.now() if isinstance(when, (int, float)): # We assume to get a UNIX / POSIX timestamp @@ -34,7 +37,6 @@ class Departure(): self.when = when else: ValueError("when must be a valid datetime, timestamp or string!") - self.line = line def __str__(self): return "Start: %s, End: %s, when: %s, now: %s, line: %s" % ( @@ -46,4 +48,11 @@ class Departure(): return self.when - self.now def to_json(self): - pass + return json.dumps({'start': self.start.decode('iso-8859-1'), + 'end': self.end, + 'line': self.line, + 'now_full': fullformat(self.now), + 'now_hour': hourformat(self.now), + 'when_full': fullformat(self.when), + 'when_hour': hourformat(self.when), + 'remaining': round(self.remaining.total_seconds())}) -- cgit v1.2.3