2015-04-27 09:55:57 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import requests
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
import re
|
2015-05-24 05:10:22 +00:00
|
|
|
import time
|
|
|
|
import datetime
|
|
|
|
import os
|
2015-04-27 09:55:57 +00:00
|
|
|
|
|
|
|
def main():
|
2015-05-24 05:10:22 +00:00
|
|
|
ts = int(datetime.datetime.now().strftime("%s"))
|
|
|
|
loc = geolocate(wifidata=listAccessPoints())
|
|
|
|
loc['timestamp'] = ts
|
|
|
|
loc = json.dumps(loc)
|
|
|
|
path = os.path.expanduser('~/.data/location')
|
|
|
|
writeFile(os.path.join(path, 'latest.json'), loc)
|
|
|
|
writeFile(os.path.join(path, "%i.json" % ts), loc)
|
|
|
|
|
|
|
|
def writeFile(fn,content):
|
|
|
|
with open(fn,'w') as f:
|
|
|
|
f.write(content)
|
2015-04-27 09:55:57 +00:00
|
|
|
|
|
|
|
def geolocate(wifidata=None):
|
|
|
|
url = "https://location.services.mozilla.com/v1/geolocate?key=test"
|
|
|
|
postdata = { }
|
|
|
|
if wifidata:
|
|
|
|
postdata['wifiAccessPoints'] = wifidata
|
2015-05-24 05:10:22 +00:00
|
|
|
return requests.post(url, data=json.dumps(postdata)).json()
|
2015-04-27 09:55:57 +00:00
|
|
|
|
2015-05-24 05:10:22 +00:00
|
|
|
def listAccessPoints():
|
|
|
|
bssids = []
|
|
|
|
cmd = "nmcli -t d list".split(' ')
|
2015-04-27 09:55:57 +00:00
|
|
|
output = subprocess.check_output(cmd)
|
2015-05-24 05:10:22 +00:00
|
|
|
for line in output.split('\n'):
|
|
|
|
m = re.match('AP\[\d+\]\.BSSID\:(\S+)',line)
|
|
|
|
if m:
|
|
|
|
bssids.append(m.group(1))
|
|
|
|
if len(bssids) < 2:
|
|
|
|
return None
|
|
|
|
return [ {'macAddress': x} for x in bssids ]
|
2015-04-27 09:55:57 +00:00
|
|
|
|
|
|
|
sys.exit(main())
|