Created
August 28, 2013 23:11
-
-
Save jl2/6372547 to your computer and use it in GitHub Desktop.
Read travel time data from http://cotrip.org/speed.htm and do neat stuff with it...
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 python3 | |
import io | |
import sys | |
import json | |
import httplib2 | |
from lxml import etree | |
roads_url = 'http://cotrip.org/speed.htm' | |
def printJSON(json): | |
for obj in json: | |
print(obj) | |
def main(args): | |
h = httplib2.Http(".cache") | |
resp, content = h.request(roads_url, "GET") | |
road_data = content.decode('utf8') | |
parser = etree.HTMLParser() | |
root = etree.parse(io.StringIO(road_data), parser) | |
scripts = root.xpath('/html/body/div/div/div/script/text()') | |
for scr in scripts: | |
lines = scr.split(';') | |
for line in lines: | |
foundIt = False | |
if line.find('var roadsJSONStr')>0: | |
line = line.replace(" var roadsJSONStr = new String('",'',1) | |
line = line[:-2] | |
foundIt = True | |
theJSON = json.loads(line) | |
genDate = theJSON['RoadDetails']['Header']['GeneratedDate'] | |
print("Showing road data generated on: ", genDate) | |
rData = theJSON['RoadDetails']['Road'] | |
for road in rData: | |
rtype = road['Type'] | |
rname = road['Name'] | |
print('{}, {}'.format(rname, rtype)) | |
elif line.find('var camerasJSONStr')>0: | |
line = line.replace(" var camerasJSONStr = new String('",'',1) | |
line = line[:-2] | |
foundIt = True | |
theJSON = json.loads(line) | |
camDetails = theJSON['CameraDetails']['Camera'] | |
for cam in camDetails: | |
print('{} - ({}, {})'.format(cam['Name'], cam['Location']['Latitude'], cam['Location']['Longitude'])) | |
elif line.find('var segmentsJSONStr')>0: | |
line = line.replace(" var segmentsJSONStr = new String('",'',1) | |
line = line[:-2] | |
foundIt = True | |
elif line.find('var alertsJSONStr')>0: | |
# Ignore alerts for now | |
pass | |
if __name__=='__main__': | |
main(sys.argv[1:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment