#!/usr/bin/python
# Copyright (C) 2010 Kees Cook <kees@outflux.net>
# License: GPLv3
# Find location of a MAC address via Google Location Services
# http://code.google.com/p/gears/wiki/GeolocationAPI
import sys, urllib2
import simplejson

loc_req = { 'version': '1.1.0',
            'request_address': True,
            'address_language': 'en',
            'wifi_towers': [] }
for bssid in sys.argv[1:]:
    loc_req['wifi_towers'] += [{ 'mac_address': bssid.replace(':','-'),
                                 'signal_strength': 1 } ]

data = simplejson.JSONEncoder().encode(loc_req)
print data

# If 'accuracy': >22000.0 it seems to only know general area, or is that
# an indicator of unknown location?
output = urllib2.urlopen('https://www.google.com/loc/json', data).read()
print simplejson.loads(output)
