From 41a89baa0cd3cd777e8ddb1d1fc533f4293b2742 Mon Sep 17 00:00:00 2001 From: Dominik Heidler Date: Thu, 3 Sep 2015 14:32:08 +0200 Subject: [PATCH] add wifi -> location api example --- contrib/geolocate.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100755 contrib/geolocate.py diff --git a/contrib/geolocate.py b/contrib/geolocate.py new file mode 100755 index 0000000..059a79e --- /dev/null +++ b/contrib/geolocate.py @@ -0,0 +1,43 @@ +#!/usr/bin/python + +import requests +import subprocess + +# doku: https://developers.google.com/maps/documentation/geolocation/intro#wifi_access_point_object + +""" +r = requests.post("https://www.googleapis.com/geolocation/v1/geolocate", params={"key": "AIzaSyDwr302FpOSkGRpLlUpPThNTDPbXcIn_FM"}, json={ + "wifiAccessPoints": [ + { + "macAddress": "10-fe-ed-af-43-44", + "signalStrength": 100 + }, + { + "macAddress": "02-ca-ff-ee-ba-be", + "signalStrength": 100 + } + ] +}) +print(r.text) +""" + +networks = [] + +o = subprocess.check_output(["iwlist", "wlan0", "scanning"]) +ls = o.decode("UTF-8").split(" Cell") +for wifi in ls[1:]: + for field in wifi.split("\n"): + if "Address:" in field: + mac = field.split("Address: ")[1] + elif "Signal level=" in field: + signal_strength = field.split("Signal level=")[1].split(" dBm")[0] + print("%s -> %s" % (mac, signal_strength)) + networks.append({"macAddress": mac, "signalStrength": signal_strength}) + +r = requests.post("https://www.googleapis.com/geolocation/v1/geolocate", + params={"key": "AIzaSyDwr302FpOSkGRpLlUpPThNTDPbXcIn_FM"}, + json={"wifiAccessPoints": networks} +) + +print({"wifiAccessPoints": networks}) +print(r.text)