-
Notifications
You must be signed in to change notification settings - Fork 0
/
wigle_geolocate.py
144 lines (116 loc) · 3.65 KB
/
wigle_geolocate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python3
import os
import csv
import time
import sqlite3
import requests
import gmplot
from mac_vendor_lookup import MacLookup
MacLookup().update_vendors()
WIGLE_API_KEY = open("wigle.api").read().strip()
GOOGLE_API_KEY = open("google.api").read().strip()
db = sqlite3.connect('signals_1.db')
cur = db.cursor()
results = cur.execute("SELECT DISTINCT SSID FROM SIGNALS_CLEAN")
ssid_list = [ result[0] for result in results ]
LAT = 42.3020275
LON = -83.712292
geo_by_ssid = {}
if os.path.exists('geo-ssid-cache.csv'):
with open('geo-ssid-cache.csv', 'rt') as file:
reader = csv.DictReader(file)
for row in reader:
if row['lat'] == '':
row['lat'] = None
else:
row['lat'] = float(row['lat'])
if row['lon'] == '':
row['lon'] = None
else:
row['lon'] = float(row['lon'])
geo_by_ssid[row['ssid']] = row
for ssid in ssid_list:
if ssid in geo_by_ssid:
print(f'Skipping {ssid}')
continue
url = f"https://api.wigle.net/api/v2/network/search?onlymine=false&closestLat={LAT}&closestLong={LON}&freenet=false&paynet=false&ssid={ssid}"
print(f'GET {url}')
result = requests.get(
url,
headers={
'Accept': 'application/json',
'Authorization': f'Basic {WIGLE_API_KEY}',
}
)
if not result.headers['content-type'].startswith('application/json'):
print(result.content)
continue
result = result.json()
try:
if result['resultCount'] == 0:
geo_by_ssid[ssid] = {
'ssid': ssid,
'lat': None,
'lon': None,
'city': None,
'region': None,
'country': None,
'last_updated': None,
}
continue
first_result = result['results'][0]
geo_by_ssid[ssid] = {
'ssid': ssid,
'lat': first_result['trilat'],
'lon': first_result['trilong'],
'city': first_result['city'],
'region': first_result['region'],
'country': first_result['country'],
'last_updated': first_result['lastupdt'],
}
except KeyError:
print(result)
geo_by_ssid[ssid] = {
'ssid': ssid,
'lat': None,
'lon': None,
'city': None,
'region': None,
'country': None,
'last_updated': None,
}
break
time.sleep(1)
with open('geo-ssid-cache.csv', 'wt') as file:
first_ssid = next(iter(geo_by_ssid.keys()))
headings = geo_by_ssid[first_ssid].keys()
writer = csv.DictWriter(file, headings)
writer.writeheader()
for row in geo_by_ssid.values():
writer.writerow(row)
print(geo_by_ssid.keys())
colors=[
"red",
"orange",
"yellow",
"green",
"cornflowerblue",
"violet",
]
gmap = gmplot.GoogleMapPlotter(LAT, LON, 14, apikey=GOOGLE_API_KEY)
for i, loc in enumerate(geo_by_ssid.values()):
if loc['lat'] is None:
continue
results = cur.execute("SELECT DISTINCT MAC FROM SIGNALS_CLEAN WHERE SSID = ?", (loc['ssid'],))
mac_list = [ result[0] for result in results ]
html= '<h3>' + loc['ssid'] + '</h3>'
for mac in mac_list:
vendor = 'randomized mac'
try:
vendor = MacLookup().lookup(mac)
except:
pass
html+= mac + ' (' + vendor +')<br>'
gmap.marker(loc['lat'],loc['lon'],color=colors[i%len(colors)], info_window=html, label=loc['ssid'][0], title=loc['ssid'])
# Draw the map to an HTML file:
gmap.draw('map.html')