Skip to content

Instantly share code, notes, and snippets.

@sash13
Last active November 4, 2015 18:37
Show Gist options
  • Select an option

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

Select an option

Save sash13/8d8521249faf45e30f3e to your computer and use it in GitHub Desktop.
Geolocation service script
+CENG: 0,"0522,27,00,255,06,22,2ab4,05,00,1159,255"
+CENG: 1,"0710,25,30,0bbc,255,06,FFFd"
+CENG: 2,"0706,20,15,0bb1,255,06,FFFd"
+CENG: 3,"0708,17,24,0bb3,255,06,FFFd"
+CENG: 4,"0540,10,59,0bba,255,06,FFFd"
import requests
import json
GOOGLE_API_KEY = 'Your api code here'
YANDEX_API_KEY = 'Your api code here'
GOOGLE_URL = 'https://www.googleapis.com/geolocation/v1/geolocate?key=' + GOOGLE_API_KEY
YANDEX_URL = 'http://api.lbs.yandex.net/geolocation'
fname = 'cells.txt'
#For test purpose, Update with you operatr info
MCC = 255
MNC = 6
radioType = 'gsm'
carrier = "Astelit"
data_google = {"homeMobileCountryCode": MCC,
"homeMobileNetworkCode": MNC,
"radioType": radioType,
"carrier": carrier,
"cellTowers": []
}
data_yandex = {"common": {"version": "1.0", "api_key": YANDEX_API_KEY},
"gsm_cells": []
}
content = []
""" From http://stackoverflow.com/questions/4154969/how-to-map-numbers-in-range-099-to-range-1-01-0 """
def scale(val, src, dst):
"""
Scale the given value from the scale of src to the scale of dst.
"""
return ((val - src[0]) / (src[1]-src[0])) * (dst[1]-dst[0]) + dst[0]
with open(fname) as f:
content = [x.strip('\n') for x in f.readlines()]
for line in content:
cellId_idx = 4
locArea_idx = 7
mcc_idx = 5
mnc_idx = 6
first = line.split(',')
if first[0][-1] in '0':
print 'Connected station'
cellId_idx = 7
cellId_idx = 11
mcc = 4
mnc = 5
try:
idx = int(first[0][-1])
cellId = int(first[cellId_idx].replace('"', ''), 16)
locArea = int(first[locArea_idx].replace('"', ''), 16)
sigStre = int(first[2])
mcc = int(first[mcc_idx])
mnc = int(first[mnc_idx])
except:
break
sigStre = int(scale(sigStre, (0.0, 36.0), (-110.0, -48.0)))
data_google["cellTowers"].append({"cellId": cellId,
"locationAreaCode":locArea,
"mobileCountryCode": mcc,
"mobileNetworkCode": mnc,
"age": 255 if idx else 0,
"signalStrength": sigStre}
)
data_yandex["gsm_cells"].append({"cellid": cellId,
"lac":locArea,
"countrycode": mcc,
"operatorid": mnc,
"age": 255 if idx else 0,
"signal_strength": sigStre}
)
print str(idx), str(cellId), str(locArea), str(sigStre)
print data_google
print data_yandex
r = requests.post(GOOGLE_URL, json=data_google)
google_r = r.json()
r = requests.post(YANDEX_URL, data={"json": json.dumps(data_yandex)})
yandex_r =json.loads(r.text)
print 'Google:\t', google_r['location']['lat'] + ',', google_r['location']['lng'], '\t', google_r['accuracy']
print 'Yandex:\t', yandex_r['position']['latitude']+ ',', yandex_r['position']['longitude'], '\t', yandex_r['position']['precision']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment