Created
August 2, 2012 22:15
-
-
Save shokai/3241217 to your computer and use it in GitHub Desktop.
Get your location with WiFi BSSID and GoogleMap
This file contains 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
#!/usr/bin/env ruby | |
require 'net/http' | |
require 'uri' | |
require 'json' | |
def get_addrs | |
case RUBY_PLATFORM | |
when /darwin/ | |
lines = `/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -s`.split(/[\r\n]/) | |
lines.shift | |
lines.map{|i| | |
a = i.scan(/[^\s]+/) | |
{:bssid => a[1], :signal => a[2].to_i} | |
} | |
when /linux/ | |
lines = `iwlist wlan0 scan`.split(/[\r\n]/) | |
addrs = [] | |
lines.each do |line| | |
addrs.push({:bssid => line.split(/\s+/).last, :signal => 8}) if line =~ /Address: / | |
addrs.last[:signal] = line.scan(/Signal level=(\-?\d+)/)[0][0].to_i rescue next if line =~ /Signal level=/ | |
end | |
addrs | |
end | |
end | |
def get_location(addrs) | |
uri = URI.parse 'http://www.google.com/loc/json' | |
query = { | |
:version => '1.1.0', | |
:host => 'maps.google.com', | |
:request_address => true, | |
:address_language => ENV['LANG'].scan(/([^\.]+)/)[0][0], | |
:wifi_towers => addrs.map{|addr| {:mac_address => addr[:bssid], :signal_strength => addr[:signal], :age => 0} } | |
}.to_json | |
res = Net::HTTP.start(uri.host, uri.port).request(Net::HTTP::Post.new(uri.request_uri), query) | |
raise "Response Error (#{res.code})" unless res.code.to_i == 200 | |
JSON.parse(res.body)['location'] | |
end | |
p addrs = get_addrs | |
puts location = get_location(addrs) | |
puts "https://maps.google.com/?ll=#{location['latitude']},#{location['longitude']}&z=16" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I get the following: