direction.coffee 1.3 KB

123456789101112131415161718192021222324252627282930
  1. # Get driving direction using Google Directions API.
  2. page = require('webpage').create()
  3. system = require 'system'
  4. if system.args.length < 3
  5. console.log 'Usage: direction.coffee origin destination'
  6. console.log 'Example: direction.coffee "San Diego" "Palo Alto"'
  7. phantom.exit 1
  8. else
  9. origin = system.args[1]
  10. dest = system.args[2]
  11. page.open encodeURI('http://maps.googleapis.com/maps/api/directions/xml?origin=' + origin +
  12. '&destination=' + dest + '&units=imperial&mode=driving&sensor=false'),
  13. (status) ->
  14. if status isnt 'success'
  15. console.log 'Unable to access network'
  16. else
  17. steps = page.content.match(/<html_instructions>(.*)<\/html_instructions>/ig)
  18. if not steps
  19. console.log 'No data available for ' + origin + ' to ' + dest
  20. else
  21. for ins in steps
  22. ins = ins.replace(/\&lt;/ig, '<').replace(/\&gt;/ig, '>')
  23. ins = ins.replace(/\<div/ig, '\n<div')
  24. ins = ins.replace(/<.*?>/g, '')
  25. console.log(ins)
  26. console.log ''
  27. console.log page.content.match(/<copyrights>.*<\/copyrights>/ig).join('').replace(/<.*?>/g, '')
  28. phantom.exit()