Created
April 22, 2011 13:14
-
-
Save toto/936628 to your computer and use it in GitHub Desktop.
Gets your location using the google wifi location service (also used by Firefox) using the Mac OS X CoreWLAN framework on Snow Leopard. Also compare to the CoreLocation based version https://gist.github.com/266916
This file contains hidden or 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
require 'pp' | |
require 'net/http' | |
require 'net/https' | |
require 'rubygems' | |
require 'json' | |
def bssid_to_api(bssidstr) | |
bssidstr.gsub(':', '-').gsub(/(\-|\A)(\d)(\-|\Z)/) {|match| "#{match[0]}0#{match[1]}#{match[2]}"} | |
end | |
def request_location(body_hash) | |
host = 'www.google.com' | |
http = Net::HTTP.new(host, 443) | |
http.set_debug_output $stderr if $DEBUG | |
http.use_ssl = true | |
body_data = body_hash.to_json | |
headers = {'Content-Type' => 'text/plain; charset=UTF-8', | |
'Content-Length' => body_data.size.to_s, | |
'Host' => host} | |
path = '/loc/json' | |
resp, data = http.post(path, body_data, headers) | |
return [resp, data] | |
end | |
request_body = {"version" => "1.1.0", | |
"request_address" => true, | |
"wifi_towers" => []} | |
if defined? MACRUBY_VERSION | |
framework 'CoreWLAN' | |
interface = CWInterface.interfaceWithName 'en1' | |
nets = interface.scanForNetworksWithParameters({}, error: nil) | |
nets.each do |network| | |
request_body['wifi_towers'] << {'mac_address' => bssid_to_api(network.bssid), | |
'ssid' => network.ssid, | |
'signal_strength' => network.rssi.to_s.to_i} | |
end | |
else | |
# TODO: do this in other rubies | |
request_body['wifi_towers'] = [] | |
end | |
res, data = request_location(request_body) | |
pp JSON.parse(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment