Skip to content

Instantly share code, notes, and snippets.

@w1ndy
Created November 16, 2016 06:17
Show Gist options
  • Save w1ndy/444d2dbb6d48fd7f85e67eee6151a7e6 to your computer and use it in GitHub Desktop.
Save w1ndy/444d2dbb6d48fd7f85e67eee6151a7e6 to your computer and use it in GitHub Desktop.
Query base station information by LAC and cell ID from cellocation.com
#!/usr/bin/python3
import requests
import time
import json
INPUT_FILE = 'diff.csv'
OUTPUT_FILE = 'diff.out.csv'
THRESHOLD = 290 #times
THRESHOLD_TIME = 300.0 #sec
URL = 'http://api.cellocation.com/cell/?mcc=460&mnc=0&lac={lac}&ci={cell}&output=json'
tbegin = 0
creq = 0
def brake():
global tbegin, creq
tsleep = THRESHOLD_TIME - (time.time() - tbegin)
print('Suspend for %.2f seconds...' % tsleep)
time.sleep(tsleep)
tbegin = time.time()
creq = 0
def main():
global tbegin, creq
f = open(INPUT_FILE, 'r')
fout = open(OUTPUT_FILE, 'w')
tbegin = time.time()
for line in f:
if not len(line): continue
data = line.split(',')
lac, cell = int(data[0]), int(data[1])
if time.time() - tbegin > THRESHOLD_TIME:
tbegin = time.time()
creq = 0
if creq >= THRESHOLD: brake()
while True:
print('Looking up %d, %d...' % (lac, cell))
r = requests.get(URL.format(lac=lac,cell=cell))
result = json.loads(r.text)
if result['errcode'] == 0:
fout.write('%d,%d,%s,%s,%s,%s\n' % (lac, cell, result['lat'], result['lon'], result['radius'], result['address']))
break
elif result['errcode'] == 10001:
fout.write('%d,%d,,,,\n' % (lac, cell))
break
else:
brake()
creq += 1
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment