diff options
author | Christian Struck <christian@struck.se> | 2013-01-24 14:01:39 +0100 |
---|---|---|
committer | Christian Struck <christian@struck.se> | 2013-01-24 14:01:39 +0100 |
commit | e993f4da26b3adfa32dc4d069af46c5b8ba6ece7 (patch) | |
tree | 18332011106e22a9386b1d2b3ae40ded2ae5af73 /bvggrabber | |
parent | 1d41ba1e16a921b77337f695ba073780021e9bf5 (diff) | |
download | bvg-grabber-e993f4da26b3adfa32dc4d069af46c5b8ba6ece7.tar.gz bvg-grabber-e993f4da26b3adfa32dc4d069af46c5b8ba6ece7.tar.bz2 bvg-grabber-e993f4da26b3adfa32dc4d069af46c5b8ba6ece7.zip |
changed variable names to more corresponding ones. Code will be from now on completely english. Fixed the 1439min problem. And now showing the Bus will arrive now, when it arrives in the next 2min.
Diffstat (limited to 'bvggrabber')
-rwxr-xr-x | bvggrabber/bvg-grabber.py | 35 |
1 files changed, 19 insertions, 16 deletions
diff --git a/bvggrabber/bvg-grabber.py b/bvggrabber/bvg-grabber.py index d725948..0daee59 100755 --- a/bvggrabber/bvg-grabber.py +++ b/bvggrabber/bvg-grabber.py @@ -1,4 +1,4 @@ -#! /usr/bin/python3 +#! /usr/bin/env python3 #-.- coding: UTF-8 -.- import json @@ -10,28 +10,31 @@ 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')) +def printOutput(stations, stationName): + print('Station: %s' % stationName) + if 'error' in stations: + print(stations.get('error')) else: - actual = int(datetime.today().hour) * 3600 + int(datetime.today().minute) * 60 - for station in json_return.get('stations', []): + currentTime = int(datetime.today().hour) * 3600 + int(datetime.today().minute) * 60 + for station in stations.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)) + departureTime = int(departure['time'].replace("*", "").split(":")[0]) * 3600 + int(departure['time'].replace("*", "").split(":")[1]) * 60 + if (departureTime - currentTime) / 60 < -1: + departureTime += 60 * 60 * 24 + if (departureTime - currentTime) / 60 < 2: + departureText = "now" + else: + departureText = "in %2d min" % ((departureTime - currentTime) / 60) + print('%-9s%-31s%12s' % (departure['line'], departure['direction'], departureText)) print() -def queryAPI(haltestelle): +def queryAPI(stationName): error = '' try: - ret = urlopen('http://bvg-api.herokuapp.com/stations?input=' + quote(haltestelle)) - return json.loads(ret.read().decode('UTF-8')) + stations = urlopen('http://bvg-api.herokuapp.com/stations?input=' + quote(stationName)) + return json.loads(stations.read().decode('UTF-8')) except (IOError, UnicodeDecodeError) as e: - error = 'Error grabbing %s!\n' % haltestelle + error = 'Error grabbing %s!\n' % stationName if hasattr(e, 'reason'): error += 'failed to connect\n' error += 'Reason: ' + str(e.reason) |