Python - Driving Time Meter
They've been doing some major road construction in my city lately and has made for some frustration.
My commute home can take over an hour with traffic, I'm not in a hurry to get home as much as I'd rather not sit in stop'n'no-go traffic, wasting gas, wearing out my clutch, inhaling fumes. Plus it's going to be Winter soon and since I ride a motorcycle 90% of the year, I don't wanna freeze to death out there. 15 minutes is fine, hour plus of exposure is life threatening.
I typically will just loiter at work after-hours till traffic subdues, rather surf the web for an hour than sit in traffic anyways. But at the same time, I'd rather be at home.
So I wrote this script that takes starting/ending addresses, a desired driving time I think is tolerable to endure, how long I want the script to run. Then uses bing maps to find out what the current driving time will be with traffic, find the fastest route home, compare that to the desired time to get home and then prints out the time/route as well as lights up my BlinkTape with visual information.
Each lit light is how many minutes long my driving time will be. The color of the light tells me if the drive time is longer than I'd like it to be or not.
My commute home can take over an hour with traffic, I'm not in a hurry to get home as much as I'd rather not sit in stop'n'no-go traffic, wasting gas, wearing out my clutch, inhaling fumes. Plus it's going to be Winter soon and since I ride a motorcycle 90% of the year, I don't wanna freeze to death out there. 15 minutes is fine, hour plus of exposure is life threatening.
I typically will just loiter at work after-hours till traffic subdues, rather surf the web for an hour than sit in traffic anyways. But at the same time, I'd rather be at home.
So I wrote this script that takes starting/ending addresses, a desired driving time I think is tolerable to endure, how long I want the script to run. Then uses bing maps to find out what the current driving time will be with traffic, find the fastest route home, compare that to the desired time to get home and then prints out the time/route as well as lights up my BlinkTape with visual information.
Each lit light is how many minutes long my driving time will be. The color of the light tells me if the drive time is longer than I'd like it to be or not.
from BlinkyTape import BlinkyTape
from time import sleep
import simplejson, urllib, sys, os, time
bb = BlinkyTape('/dev/tty.usbmodem1441')
bingkey = 'put your bing maps API key here' # <---- won't work without one! You'll need to get a Bing Maps API key, they are free within reasonable limits for personal/educational use
def blink(driving_time,desired):
lights = []
for x in range(60):
if x < driving_time:
rgb = 0,255,0 #green
if driving_time > desired:
rgb = 255,0,0 #red
lights.append(rgb)
else:
rgb = 0,0,0
lights.append(rgb)
for d in range(60*5): #lazy, but keeps the lights on for 5 minutes before requesting more traffic/map data
time.sleep(1)
for x in lights:
bb.sendPixel(x[0],x[1],x[2])
bb.show()
def travel(origin,destination,desired,runtime):
url = "http://dev.virtualearth.net/REST/V1/Routes?wp.0={0}&wp.1={1}&optmz=timeWithTraffic&key={2}".format(str(origin),str(destination),str(bingkey))
runfor = int(runtime / 5)
for i in range(runfor):
result = simplejson.load(urllib.urlopen(url))
driving_time = round(result['resourceSets'][0]['resources'][0]['travelDuration']/60.0)
print "Current travel time is: %s minutes" % (int(driving_time))
if driving_time < float(desired):
os.system('say GTFO') #Mac OSX method to read output aloud, this one says 'go home' in Southern
else:
shittosay = 'say "travel time is %s minutes"' % (int(driving_time)) #handy for when away from my desk
os.system(shittosay)
route = result['resourceSets'][0]['resources'][0]['routeLegs'][0]['itineraryItems']
for i in route:
for i in i['details']:
if i.has_key('names'):
print '%-20s: %-100s' % (i['maneuverType'],i['names'][0])
blink(driving_time,desired)
travel('201 Poplar Ave, Memphis, TN','W Valleywood Dr, Collierville, TN',15,60)
Comments