<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: Sooli's Code Snippets</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 06 Sep 2008 20:07:48 GMT</pubDate>
    <description>DZone Snippets: Sooli's Code Snippets</description>
    <item>
      <title>Get Google maps informations (distance and time, French version)</title>
      <link>http://snippets.dzone.com/posts/show/3964</link>
      <description>Here a little script to get time and distance between two towns with google maps. You can change "voiture" by your langage word "car", or anything else.&lt;br /&gt;&lt;br /&gt;this is a command line version : # ruby myscript.rb "Lyon" "Paris"&lt;br /&gt;You can easily change it for a Ruby on Rails project :)&lt;br /&gt;Maybie we can upgrade this script using regexp.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;require "open-uri"&lt;br /&gt;require "cgi"&lt;br /&gt;&lt;br /&gt;# Command line arguments ... &lt;br /&gt;$*.each {|argu| argu.gsub!("\"","")}&lt;br /&gt;# Defaults towns&lt;br /&gt;src = "Lyon"&lt;br /&gt;dest = "Paris"&lt;br /&gt;# If we have fill two aguments ...&lt;br /&gt;if $*.size == 2 then&lt;br /&gt;        src = $*[0]&lt;br /&gt;        dest = $*[1]&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;html = ""&lt;br /&gt;url = "http://maps.google.com/maps?f=d&amp;hl=fr&amp;saddr=" + CGI.escape(src) + "&amp;daddr="+ CGI.escape(dest)&lt;br /&gt;# Read the html page&lt;br /&gt;open(url) {|f|&lt;br /&gt;        f.each_line {|line| html += line}&lt;br /&gt;}&lt;br /&gt;pos1 = html.index("voiture") # French version&lt;br /&gt;pos2 = html.index("&lt;/td&gt;",pos1) unless pos1 == nil&lt;br /&gt;pos1 = html.index("&gt;",pos2+6)+1 unless pos2 == nil&lt;br /&gt;pos2 = html.index("&lt;/td&gt;",pos2+1) -1 unless pos2 == nil&lt;br /&gt;ret = ""&lt;br /&gt;if pos1 != nil and pos2 != nil&lt;br /&gt;        ret = html[pos1..pos2].gsub!("&amp;#160;", " ")&lt;br /&gt;else&lt;br /&gt;        ret = "0"&lt;br /&gt;end&lt;br /&gt;html = nil&lt;br /&gt;p ret&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 08 May 2007 14:03:29 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3964</guid>
      <author>sooli (BONNAURE)</author>
    </item>
  </channel>
</rss>
