diff options
author | Markus Holtermann <info@markusholtermann.eu> | 2013-01-24 21:32:04 +0100 |
---|---|---|
committer | Markus Holtermann <info@markusholtermann.eu> | 2013-01-24 21:32:04 +0100 |
commit | d6f0b39753f1b55f86a0eba9a822c367a26c45dc (patch) | |
tree | 5bcd204369f967e748d9f014b923e21a06e3261f /bvggrabber/api | |
parent | 0cdc756ff3ef4c4f83739cb6db41b282a24fc152 (diff) | |
download | bvg-grabber-d6f0b39753f1b55f86a0eba9a822c367a26c45dc.tar.gz bvg-grabber-d6f0b39753f1b55f86a0eba9a822c367a26c45dc.tar.bz2 bvg-grabber-d6f0b39753f1b55f86a0eba9a822c367a26c45dc.zip |
Add base QueryApi and Departure classes including simple tests
Diffstat (limited to 'bvggrabber/api')
-rw-r--r-- | bvggrabber/api/__init__.py | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/bvggrabber/api/__init__.py b/bvggrabber/api/__init__.py index 40a96af..063e7e2 100644 --- a/bvggrabber/api/__init__.py +++ b/bvggrabber/api/__init__.py @@ -1 +1,48 @@ # -*- coding: utf-8 -*- + + +from datetime import datetime +from dateutil.parser import parse + + +fullformat = lambda dt: dt.strftime('%Y-%m-%d %H:%M') + + +class QueryApi(): + + def __init__(self): + pass + + def call(self): + raise NotImplementedError("The inheriting class needs to implement " + "the call() method!") + + +class Departure(): + + def __init__(self, start, end, when, line): + self.start = start + self.end = end + if isinstance(when, (int, float)): + # We assume to get a UNIX timestamp + self.when = datetime.fromtimestamp(when) + elif isinstance(when, str): + self.when = parse(when) + now = datetime.now() + if (self.when - now).total_seconds() < 0: + self.when = self.when.replace(day=self.when.day + 1) + elif isinstance(when, datetime): + 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, line: %s" % ( + self.start, self.end, fullformat(self.when), self.line) + + def remaining(self): + return self.when - datetime.now() + + def to_json(self): + pass
\ No newline at end of file |