diff options
author | Christian Struck <christian@struck.se> | 2013-01-23 16:00:15 +0100 |
---|---|---|
committer | Christian Struck <christian@struck.se> | 2013-01-23 16:00:15 +0100 |
commit | 1d41ba1e16a921b77337f695ba073780021e9bf5 (patch) | |
tree | a3e64c1701640ea0ed785a387f1106da21783c0b | |
parent | 523527b6a90675b419cf20514aa368bbc6ea75df (diff) | |
download | bvg-grabber-1d41ba1e16a921b77337f695ba073780021e9bf5.tar.gz bvg-grabber-1d41ba1e16a921b77337f695ba073780021e9bf5.tar.bz2 bvg-grabber-1d41ba1e16a921b77337f695ba073780021e9bf5.zip |
Added the latest nongit app-prototype version to git
-rwxr-xr-x | bvggrabber/bvg-grabber.py | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/bvggrabber/bvg-grabber.py b/bvggrabber/bvg-grabber.py new file mode 100755 index 0000000..d725948 --- /dev/null +++ b/bvggrabber/bvg-grabber.py @@ -0,0 +1,53 @@ +#! /usr/bin/python3 +#-.- coding: UTF-8 -.- + +import json +import time + +from datetime import datetime +from subprocess import call +from urllib.parse import quote +from urllib.request import urlopen + + +def printOutput(json_return, haltestelle): + print('Station: %s' % haltestelle) + if 'error' in json_return: + print(json_return.get('error')) + else: + actual = int(datetime.today().hour) * 3600 + int(datetime.today().minute) * 60 + for station in json_return.get('stations', []): + for departure in station.get('departures', []): + arrival = int(departure['time'].replace("*", "").split(":")[0]) * 3600 + int(departure['time'].replace("*", "").split(":")[1]) * 60 + if (arrival - actual) / 60 < 0: + arrival += 60 * 60 * 24 + arrival_text = "in %2d min" % (int((arrival - actual)) / 60) + print('%-9s%-31s%12s' % (departure['line'], departure['direction'], arrival_text)) + print() + +def queryAPI(haltestelle): + error = '' + try: + ret = urlopen('http://bvg-api.herokuapp.com/stations?input=' + quote(haltestelle)) + return json.loads(ret.read().decode('UTF-8')) + except (IOError, UnicodeDecodeError) as e: + error = 'Error grabbing %s!\n' % haltestelle + if hasattr(e, 'reason'): + error += 'failed to connect\n' + error += 'Reason: ' + str(e.reason) + elif hasattr(e, 'code'): + error += 'serverside problem\n' + error += 'Code: ' + str(e.code) + '\n' + str(e.headers) + else: + error += 'Unexpected Error' + return {'error': error} + +if __name__ == '__main__': + while(True): + march = queryAPI('Marchbrücke') + # ernst = queryAPI('U Ernst-Reuter-Platz') + call("clear", shell=True) + print('%-9s%-31s%12s' % ("Line", "Destination", "Departure")) + printOutput(march, 'Marchbrücke') + # printOutput(ernst, 'U Ernst-Reuter-Platz') + time.sleep(20) |