From e993f4da26b3adfa32dc4d069af46c5b8ba6ece7 Mon Sep 17 00:00:00 2001 From: Christian Struck Date: Thu, 24 Jan 2013 14:01:39 +0100 Subject: 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. --- bvggrabber/bvg-grabber.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) (limited to 'bvggrabber') 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) -- cgit v1.2.3