Created
January 20, 2014 18:48
-
-
Save ramann/8526510 to your computer and use it in GitHub Desktop.
this watir script drives a browser to write a distance matrix for mass transit times.
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
# google maps has a nice distance matrix api, that allows you to get distance (either | |
# time or physical), provided that your means of transportation is driving, cycling, | |
# or walking. this watir script drives a browser to write a distance matrix for | |
# mass transit times. naturally, it'll have to change when google does a significant | |
# update to their google maps interface. | |
# | |
# robert mann | |
# january 2014 | |
require 'rubygems' | |
require 'watir-webdriver' | |
browser = Watir::Browser.new | |
browser.goto 'http://www.google.com/maps' | |
l = browser.link :id => 'd_launch' #get directions toggle button | |
l.exists? | |
l.click | |
l = browser.link :id => 'dir_r_btn' #transit button | |
l.exists? | |
l.click | |
puts browser.select_list(:id => 'tr_nwhen').options | |
browser.select_list(:id => 'tr_nwhen').select 'Depart at' | |
browser.text_field(:id => 'tr_date').set '01/27/14' | |
browser.text_field(:id => 'tr_time').set '08:30am' | |
# we could set destinations to be same as origins if we want a matrix | |
# but sometimes google will randomly not be able to locate a place (lincoln memorial) | |
# so it's easier to split them up, so we can restart at a failure point | |
origins = [ '18th St and Columbia Rd NW', #adams morgan | |
'Connecticut Ave and Q St NW', #dupont | |
'17th St and H St NW', #neob | |
'9th St and U St NW', #dc9 | |
'14th St and Irving St NW', #columbia heights | |
'Georgia Ave NW and Randolph St NW', #petworth | |
'16th St and Spring Rd NW', #16th near rock creek | |
'Union Station', #union station | |
'7th St and G St NW', #gallery place | |
'Lincoln Memorial Circle NW', #lincoln mem | |
'The U.S. Capitol, First Street SE', #congress | |
'13th St and H St NE', #h st | |
] | |
destinations = ['18th St and Columbia Rd NW', #adams morgan | |
'Connecticut Ave and Q St NW', #dupont | |
'17th St and H St NW', #neob | |
'9th St and U St NW', #dc9 | |
'14th St and Irving St NW', #columbia heights | |
'Georgia Ave NW and Randolph St NW', #petworth | |
'16th St and Spring Rd NW', #16th near rock creek | |
'Union Station', #union station | |
'7th St and G St NW', #gallery place | |
'Lincoln Memorial Circle NW', #lincoln mem | |
'The U.S. Capitol, First Street SE', #congress | |
'13th St and H St NE', #h st | |
] | |
city = "Washington, DC" | |
origins.each { |origin| | |
origin = origin + " " +city | |
destinations.each { |destination| | |
destination = destination + " " + city | |
next if origin == destination | |
puts "Origin: "+origin | |
puts "Dest: "+destination | |
browser.text_field(:name => 'saddr').set origin | |
browser.text_field(:name => 'daddr').set destination | |
sleep(1); | |
btn = browser.button :id => 'd_sub' #Get Directions submit | |
btn.exists? | |
btn.click | |
sleep(3); | |
li = browser.li :id => 'altroute_0' | |
answer = li.text | |
puts answer | |
outs= "{" | |
outs=outs+ '"origin":"'+origin+'",' | |
outs=outs+ '"destination":"'+destination+'",' | |
outs=outs+ '"response": {"'+answer.gsub("\n",';')+'"}' | |
outs=outs+ "}," | |
File.open('map-test.txt', 'a') { |f| f.write(outs) } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment