「wi-fi の情報をつかって位置情報を得る」をPythonでやってみた
2012-07-30 - ⧖ 2.0 min以下の話題をPythonで。 久々にスクリプト書いてみる。
- Perl - Google に無線 LAN アクセスポイントの場所を聞く - It'll be
- linux のコマンドラインから、wi-fi の情報をつかって位置情報を得る - tokuhirom's blog.
- OSX のコマンドラインから、wi-fi の情報をつかって位置情報を得る - unknownplace.org
from subprocess import Popen , PIPE
import json
import requests
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 ()
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 )
以下、実行結果。
$ python checkloc.py
{
"access_token": "2:E5ZbyL1e4a1MYgIM:R0INJY94uyy5Pxx8",
"location": {
"latitude": 34.693737,
"accuracy": 140000.0,
"longitude": 135.502165,
"address": {
"city": "大阪市",
"street_number": "3",
"country": "日本",
"region": "大阪府",
"street": "1丁目",
"country_code": "JP"
}
}
}
Pythonでもでけました。終わり。