DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

IP Location On Google Maps

04.14.2007
| 14220 views |
  • submit to reddit
        /*
Example of usage:
$ ./lip.rb snippets.dzone.com
. Hostname: snippets.dzone.com
. Country Code: US
. Country Name: United States
. Region: CA
. Region Name: California
. City: Los Angeles
. Postal Code: 90017
. Latitude: 34.0530
. Longitude: -118.2642
. ISP: CoreExpress
. Organization: CoreExpress
. Metro Code: 803
. Area Code: 213
. Google Maps URL: http://maps.google.com/maps?q=34.0530,+-118.2642&iwloc=A&hl=en
*/

#!/usr/bin/env ruby -w

if ARGV.empty?
  puts <<-T
Locate IP by haqu
usage: ./lip.rb ip|domain ...
  T
  exit
end

require 'net/http'
require 'uri'

uri = URI.parse('http://www.maxmind.com/app/locate_ip')
res = Net::HTTP.post_form(uri,
  { 'ips' => ARGV.join(' '),
    'type' => '', 'u' => '', 'p' => ''
  } )
fstr = res.body

fstr.gsub!("Edition Results<\/span><p>","CHECKPOINT")
fstr =~ /CHECKPOINT(.+?)<\/table>/m
fields = $1.grep(/<(th|td)>/)
fields.each do |f|
  f.strip!
  f.gsub!(/<[^>]+>/,"")
end

(0...13).each do |i|
  puts ". #{fields[i]}: #{fields[i+13]}"
end

maplink = "http://maps.google.com/maps?q=#{fields[20]},+#{fields[21]}&iwloc=A&hl=en"
puts ". Google Maps URL: #{maplink}"