Skip to content

Instantly share code, notes, and snippets.

@nobonobo
Created August 3, 2012 00:03
Show Gist options
  • Save nobonobo/3242346 to your computer and use it in GitHub Desktop.
Save nobonobo/3242346 to your computer and use it in GitHub Desktop.
「「wi-fi の情報をつかって位置情報を得る」をPythonでやってみた」をOSX対応にした。
import sys
from subprocess import Popen, PIPE
import json
import requests
if sys.platform=='linux':
iwlist = Popen('iwlist wlan0 scan'.split(), stdout=PIPE)
grep = Popen('grep Address:'.split(),
stdin=iwlist.stdout, stdout=PIPE)
awk = Popen(['awk', '{print $5}'],
stdin=grep.stdout, stdout=PIPE)
mac_address = awk.communicate()[0].splitlines()
elif sys.platform=='darwin':
iwlist = Popen('/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -s'.split(), stdout=PIPE)
awk = Popen(['awk', '{print $2}'],
stdin=iwlist.stdout, stdout=PIPE)
mac_address = awk.communicate()[0].splitlines()[1:]
query = json.dumps(
{'version': '1.1.0',
'host': 'maps.google.com',
'request_address': True,
'address_language': 'ja_JP',
'wifi_towers': [
{'mac_address': d,
'signal_strength': 8,
'age': 0,
} for d in mac_address],
}
)
r = requests.post('http://www.google.com/loc/json', query)
d = json.loads(r.content)
print json.dumps(d, ensure_ascii=False, indent=4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment