Created
November 1, 2013 03:06
-
-
Save acomminos/7260470 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# | |
# Simple translink stop estimate fetcher. Wonderful to use with Conky. | |
# Copyright (C) 2013 Andrew Comminos | |
# | |
# This program is free software: you can redistribute it and/or modify | |
# it under the terms of the GNU General Public License as published by | |
# the Free Software Foundation, either version 3 of the License, or | |
# (at your option) any later version. | |
# | |
# This program is distributed in the hope that it will be useful, | |
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
# GNU General Public License for more details. | |
# | |
# You should have received a copy of the GNU General Public License | |
# along with this program. If not, see | |
# <http://www.gnu.org/licenses/>. | |
import sys | |
import json | |
import http.client | |
import argparse | |
API_URL = "api.translink.ca" | |
STOP_ESTIMATE_URL = "/RTTIAPI/V1/stops/%(STOP_NUMBER)s/estimates?apiKey=%(API_KEY)s&count=%(COUNT)s&timeframe=%(TIMEFRAME)s" | |
parser = argparse.ArgumentParser(description="Fetches the estimated stop information from TransLink.") | |
parser.add_argument("stop") | |
parser.add_argument("apikey") | |
parser.add_argument("--count", default="3") | |
parser.add_argument("--timeframe", default="120") | |
args = parser.parse_args() | |
connection = http.client.HTTPConnection(API_URL) | |
connection.request('GET', STOP_ESTIMATE_URL % { 'STOP_NUMBER': args.stop, 'API_KEY': args.apikey, 'COUNT': args.count, 'TIMEFRAME': args.timeframe}, headers={'accept': 'application/JSON'}) | |
result = connection.getresponse().readall().decode('utf-8') | |
jsonresult = json.loads(result) | |
# If there is an error code in the response, report it. | |
if "Code" in jsonresult: | |
print(jsonresult["Message"]) | |
sys.exit() | |
resultobj = jsonresult[0] | |
schedules = resultobj['Schedules'] | |
# Output formatting | |
print('#'+resultobj["RouteNo"]+" "+resultobj["RouteName"]) | |
for schedule in schedules: | |
print(" "+schedule["ExpectedLeaveTime"]+" - To "+schedule["Destination"]+" - Updated "+schedule["LastUpdate"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment