Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Python script to query via its API and launch a html page with result using leaflet
<!DOCTYPE html>
<meta charset="utf-8">
<meta name='description' content="A page showing results from API">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico">
<link rel="stylesheet" href="">
<link rel="stylesheet" href="">
<link rel="stylesheet" href="">
<script src=""></script>
<script src=""></script>
<script src=""></script>
<script src="search.geojson"></script>
#map {height: 800px; }
<div class="container">
<header class="page-header">
<h1>Search result from</h1>
<div class="row">
<div class="col-md-11" id="map"></div>
<div class="col-md-1" id="ssids">
<table id="results" class="table table-stripped table-condensed">
var map = new L.Map('map');
// create the tile layer with correct attribution
var osmUrl='http://{s}{z}/{x}/{y}.png';
var osmAttrib='Map data © <a href="">OpenStreetMap</a> contributors';
var osm = new L.TileLayer(osmUrl, {minZoom: 6, maxZoom: 19, attribution: osmAttrib});
var blueIcon = new L.Icon({
iconUrl: '',
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34],
shadowSize: [41, 41]
// Add data from geojson file
var layer = new L.geoJson(data, {
pointToLayer: function(point, latlng) {
// customize marker for each feature point
var marker = L.marker(latlng, {icon: blueIcon});
marker.SSID =;
marker.MAC =; =;
marker.first_time =;
marker.last_time =;
marker.bindTooltip('SSID: '+marker.SSID+
'<br>MAC: '+marker.MAC+
'<br>channel: '
'<br>First time seen: '+marker.first_time+
'<br>Last time seen: '+marker.last_time);
return marker;
// generate list of results
var first = true;
for (f of data.features) {
var res = {};
res.lon = f.geometry.coordinates[0]; = f.geometry.coordinates[1];
res.MAC =;
res.SSID =; =;
res.first_time =;
res.last_time =;
$('#results').append('<tr><td><a class="result" href="#" data-lat="''" data-lon="'+res.lon+'">'+res.MAC+'</a></td></tr>');
if (first) {
// move map to first result location
map.setView(new L.LatLng(, res.lon), 12);
first = false;
// pan map to location of ssid
$('.result').click(function() {
map.panTo([$(this).data('lat'), $(this).data('lon')]);
<!-- vim: set ts=2 sw=2 et: -->
import sys
import requests
import json
import argparse
import geojson
import subprocess
YOUR_API_KEY = 'xxxx'
parser = argparse.ArgumentParser(description='Query via its API')
parser.add_argument('--onlymine', action='store_true', help='only use data I have uploaded')
parser.add_argument('--freenet', action='store_true', help='only take into account free APs')
parser.add_argument('--paynet', action='store_true', help='only takeinto account paying APs')
parser.add_argument('--ssid', help='look for matching SSID')
args = parser.parse_args()
class AP(object):
def __init__(self, mac, ssid, channel, lat, lon, first_time, last_time):
self.mac = mac
self.ssid = ssid = channel
self.coord = (round(lon, 6), round(lat, 6))
self.first_time = first_time
self.last_time = last_time
def __str__(self):
return 'MAC=%s, SSID=%s, channel=%d, lat=%f, lon=%f' % (self.mac, self.ssid,, self.coord[1], self.coord[0])
APs = []
headers = {'Accept': 'application/json', 'Authorization': 'Basic %s' % YOUR_API_KEY}
url = ''
payload = {'onlymine': args.onlymine,
'freenet': args.freenet,
'paynet': args.paynet,
#'latrange1': 46.27,
#'latrange2': 49.24,
#'longrange1': -5.0,
#'longrange2': 0.00,
if args.ssid:
payload['ssid'] = args.ssid
r = requests.get(url, params=payload, headers=headers)
j = r.json()
for res in j['results']:
ap = AP(res['netid'].lower(), res['ssid'], res['channel'], res['trilat'], res['trilong'], res['firsttime'], res['lasttime'])
except KeyError:
print j
# loop until there is no more result but stop at MAX_LOOP
count = 0
payload['searchAfter'] = j['search_after']
except KeyError:
print j
while payload['searchAfter'] != None:
r = requests.get(url, params=payload, headers=headers)
j = r.json()
payload['searchAfter'] = j['search_after']
for res in j['results']:
ap = AP(res['netid'].lower(), res['ssid'], res['channel'], res['trilat'], res['trilong'], res['firsttime'], res['lasttime'])
count += 1
if count > MAX_LOOP:
print ':: found %d result(s)' % len(APs)
if len(APs) == 0:
# create APs feature collection for geojson
tmp = []
for ap in APs:
point = geojson.Point(ap.coord)
feature = geojson.Feature(geometry=point,
properties={'MAC':ap.mac, 'SSID':ap.ssid, 'channel', 'first_time':ap.first_time,
'last_time': ap.last_time})
fc = geojson.FeatureCollection(tmp)
f = open('search.geojson', 'wb')
f.write('var data=%s' % geojson.dumps(fc))
cmd = 'xdg-open search.html'' '))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.