2011-04-22 19:39:51 +04:00
|
|
|
# Get driving direction using Google Directions API.
|
|
|
|
|
2011-09-13 00:17:54 +04:00
|
|
|
page = require('webpage').create()
|
2011-05-26 13:06:41 +04:00
|
|
|
|
|
|
|
if phantom.args.length < 2
|
|
|
|
console.log 'Usage: direction.js origin destination'
|
|
|
|
console.log 'Example: direction.js "San Diego" "Palo Alto"'
|
|
|
|
phantom.exit(1)
|
2011-04-22 19:39:51 +04:00
|
|
|
else
|
2011-05-26 13:06:41 +04:00
|
|
|
origin = phantom.args[0]
|
|
|
|
dest = phantom.args[1]
|
|
|
|
page.open encodeURI('http://maps.googleapis.com/maps/api/directions/xml?origin=' + origin +
|
|
|
|
'&destination=' + dest + '&units=imperial&mode=driving&sensor=false'),
|
|
|
|
(status) ->
|
|
|
|
if status isnt 'success'
|
|
|
|
console.log 'Unable to access network'
|
|
|
|
else
|
|
|
|
steps = page.content.match(/<html_instructions>(.*)<\/html_instructions>/ig)
|
|
|
|
if not steps
|
|
|
|
console.log 'No data available for ' + origin + ' to ' + dest
|
|
|
|
else
|
|
|
|
for ins in steps
|
|
|
|
ins = ins.replace(/\</ig, '<').replace(/\>/ig, '>')
|
|
|
|
ins = ins.replace(/\<div/ig, '\n<div')
|
|
|
|
ins = ins.replace(/<.*?>/g, '')
|
|
|
|
console.log(ins)
|
|
|
|
console.log ''
|
|
|
|
console.log page.content.match(/<copyrights>.*<\/copyrights>/ig).join('').replace(/<.*?>/g, '')
|
|
|
|
phantom.exit()
|