Skip to content

Instantly share code, notes, and snippets.

@hiway
Created June 21, 2013 22:54
Show Gist options
  • Save hiway/5834926 to your computer and use it in GitHub Desktop.
Save hiway/5834926 to your computer and use it in GitHub Desktop.
Quickly query Traffline API to get estimated time to reach your destination in current traffic.
"""Traffline ETA
Usage:
traffline.py <source> <destination> <city>
"""
from docopt import docopt
import requests
import json
def get_estimate_times(origin, destination, city):
endpoint = 'http://traffline.com/callWebService_Get.php?webMethodName=%s&parameters=%s'
method = 'GetTrafficTTEForAlternateRoute'
parameters = [('city',city),
('source',origin),
('sourceCords',''),
('destination',destination),
('destinationCords',''),
('isWapUpdate','true')
]
parameters = ''.join(['%s:%s~' % x for x in parameters])
r = requests.get(endpoint %(method, parameters))
data = json.loads(r.text)
# This API is so badly designed, sometimes it sends back a dict, sometimes a list.
if type(data) == type(dict) and data.has_key('InfoMsg') and int(data['InfoMsg']) == -1:
raise Exception('Traffline API retuned "-1". Probably means your spellings for locations are b0rked.')
# If we're here, probably we have some information
routes = []
for x in data:
if x.has_key('Route'):
x['Route'] = ''
if x.has_key('Summary'):
routes.append((x['Summary'], x['TTE']))
return routes
if __name__ == '__main__':
args = docopt(__doc__, help=True, version='Traffline ETA 0.1')
routes = get_estimate_times(args['<source>'], args['<destination>'], args['<city>'])
for r in routes:
print "Route: %s. ETA: %s" % r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment