Skip to content

Instantly share code, notes, and snippets.

@sash13
Created April 21, 2014 10:42
Show Gist options
  • Select an option

  • Save sash13/11139051 to your computer and use it in GitHub Desktop.

Select an option

Save sash13/11139051 to your computer and use it in GitHub Desktop.
Yandex Locator python test
# Yandex Locator (http://api.yandex.ru/locator/)
# wifi position big bro
# api key from (http://api.yandex.ru/maps/form.xml)
# pok
import subprocess
import shlex
import webbrowser
import urllib
import urllib2
import json
url_map = 'http://maps.yandex.com/?text='
url = 'http://api.lbs.yandex.net/geolocation'
version = "1.0"
api = "your_api_key"
command = 'sudo iwlist wlan0 scan'
def run_command(command):
p = subprocess.Popen(shlex.split(command),stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
return iter(p.stdout.readline, b'')
def getPok(text, start, end):
#if len(text<10):
# return 0
t=text
start_p = t.find(start)
if start_p >0:
start_p+=len(start)
end_p = t[start_p:].find(end)+start_p
out = t[start_p: end_p]
return out
return 0
def pok():
points = []
point = {}
#f=open(url)
#h=f.readline()
for h in run_command(command):
#while h:
pok1 = getPok(h, 'ESSID:"', '"')
pok2 = getPok(h, 'Address: ', '\n')
pok3 = getPok(h, 'level=', ' dBm')
if pok1:
#print pok1
point['age'] = 0
points.append(point)
point = {}
if pok2:
point['mac'] = pok2.replace(':', '-')
#print pok2
if pok3:
point['signal_strength'] = int(pok3)
#print pok3
#h = f.readline()
#f.close()
return points
def getCom(v, k):
ans = {}
ans['version'] = v
ans['api_key'] = k
return ans
def getQuery():
out = {}
common = getCom(version, api)
wifi = pok()
print 'Found:' +str(len(wifi)) + ' wifi AP'
for ap in wifi:
print ap['mac'],',' ,ap['signal_strength'] , 'dBm'
out['common'] = common
out['wifi_networks'] = wifi
return out
def goTo(urli):
webbrowser.open(urli,new=2)
print 'Open'
def main():
qry = json.dumps(getQuery())
values = {'json': qry}
data = urllib.urlencode(values)
req = urllib2.Request(url, data)
req.add_header('Content-type', 'application/x-www-form-urlencoded')
print 'Make request'
response = urllib2.urlopen(req).read()
jres = json.loads(response)
jres = jres['position']
coor = str(jres['latitude'])+','+str(jres['longitude'])
print 'Position:' + coor
while 1:
try:
text = raw_input('Show on map?(y/n)')
except EOFError:
break
else:
if text is 'y':
goTo(url_map+coor)
break
elif text is 'n':
break
return 0
main()
$python test.py
Found:5 wifi AP
00-22-B0-xx-07-xx , -45 dBm
10-xx-48-xx-7A-xx , -87 dBm
xx-1A-67-xx-2F-xx , -81 dBm
xx-F6-52-xx-B6-xx , -87 dBm
xx-60-00-xx-BF-xx , -85 dBm
Make request
Position:50.xxxx,30.xxxx
Show on map?(y/n)n
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment