Created
July 22, 2012 00:40
-
-
Save akkunchoi/3157749 to your computer and use it in GitHub Desktop.
Fetch address from wifi for OSX
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 | |
# | |
# Fetch my current address from mac addresses on wi-fi for MacOSX | |
# | |
# via http://unknownplace.org/memo/2012/07/21/1/ | |
# | |
# GeolocationAPI | |
# http://code.google.com/p/gears/wiki/GeolocationAPI | |
# Reverse Geocoding | |
# https://developers.google.com/maps/documentation/geocoding/?hl=ja#ReverseGeocoding | |
require 'json' | |
require 'net/http' | |
require 'uri' | |
addrs = {} # mac_address => signal_strength(RSSI) | |
wifi = `/System/Library/PrivateFrameworks/Apple80211.framework/Versions/A/Resources/airport -s` | |
wifi.split("\n").slice(1..wifi.size).each do |line| | |
a = line.strip.split(/\s+/) | |
addrs[a[1]] = a[2] | |
end | |
query = JSON::generate({ | |
version: '1.1.0', | |
host: 'maps.google.com', | |
request_address: true, | |
address_language: 'ja_JP', | |
wifi_towers: addrs.keys.map{|addr| { | |
mac_address: addr, | |
signal_strength: addrs[addr], | |
age: 0 | |
}} | |
}) | |
# p query | |
Net::HTTP.start('www.google.com', 80){ |http| | |
response = http.post('/loc/json', query) | |
begin | |
loc = JSON::parse(response.body) | |
latlng = [loc['location']['latitude'], loc['location']['longitude']] | |
# puts "http://maps.google.com/?q=%s,%s" % latlng | |
revgeo = JSON::parse( | |
http.get( | |
"http://maps.googleapis.com/maps/api/geocode/json?latlng=%s,%s&sensor=true" % latlng, | |
#{"Accept-Language" => "ja,en-US"} | |
{"Accept-Language" => ENV["LANG"].match(/(\w+)_/)[1]} | |
).body) | |
puts revgeo["results"][0]["formatted_address"] | |
rescue | |
p response.body | |
end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment