-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathhistorical_locations_export.py
More file actions
67 lines (57 loc) · 2.57 KB
/
historical_locations_export.py
File metadata and controls
67 lines (57 loc) · 2.57 KB
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
"""Export NCM API historical_locations/ data to .csv file.
This script will export the data from the Cradlepoint NCM API
historical_locations/ endpoint for all devices for the given date range.
Data is written to the .csv file defined below.
Cradlepoint NCM API keys are required.
Attributes
----------
start_date : beginning of date range to export
end_date : end of date range to export
output_file : name of .csv file to be created
headers : Replace values with your NCM API keys for use in HTTP requests.
"""
import requests
import csv
start_date = '2021-09-02'
end_date = '2021-09-03'
output_file = 'historical_locations.csv'
headers = {'X-ECM-API-ID': 'YOUR',
'X-ECM-API-KEY': 'KEYS',
'X-CP-API-ID': 'GO',
'X-CP-API-KEY': 'HERE',
'Content-Type': 'application/json'}
top_line = ["router", "name", "accuracy", "altitude", "carrier_id", "cinr",
"created_at", "created_at_timeuuid", "dbm", "ecio",
"latitude", "longitude", "mph", "net_device_name",
"rfband", "rfband_5g", "rsrp", "rsrp_5g", "rsrq",
"rsrq_5g", "rssi", "signal_percent", "sinr",
"sinr_5g", "summary"]
server = 'https://www.cradlepointecm.com'
with open(output_file, 'wt') as f:
writer = csv.writer(f, delimiter=',')
writer.writerow(top_line) # write header
routers_url = f'{server}/api/v2/routers/?state__in=online,offline' \
f'&limit=500'
try:
while routers_url:
routers_req = requests.get(routers_url, headers=headers).json()
routers = routers_req['data']
for router in routers:
locations_url = f'{server}/api/v2/historical_locations/' \
f'?router={router["id"]}&created_at__gt={start_date}' \
f'&created_at__lte={end_date}'
while locations_url:
locations_req = requests.get(
locations_url, headers=headers).json()
print(f'Router ID: {router["id"]} Historical Locations: '
f'{locations_req["data"]}')
locations = locations_req['data']
for location in locations:
if location['carrier_id']:
loc_values = [x for x in location.values()]
row = [router["id"], router["name"]] + loc_values
writer.writerow(row)
locations_url = locations_req['meta']['next']
routers_url = routers_req['meta']['next']
except Exception as e:
print(e)