1
0
Fork 0
satnogsmap/satnogs_api_client.py

160 lines
4.8 KiB
Python
Raw Normal View History

2018-12-18 11:27:19 -07:00
from __future__ import print_function
2018-10-24 14:32:53 -06:00
import re
2018-12-18 11:27:19 -07:00
import requests
2018-10-24 14:32:53 -06:00
NETWORK_DEV_BASE_URL = 'https://network-dev.satnogs.org'
NETWORK_BASE_URL = 'https://network.satnogs.org'
DB_BASE_URL = 'https://db.satnogs.org'
DB_DEV_BASE_URL = 'https://db-dev.satnogs.org'
2018-12-18 11:27:19 -07:00
def get_paginated_endpoint(url, max_entries=None):
r = requests.get(url=url)
r.raise_for_status()
2018-10-24 14:32:53 -06:00
2018-12-18 11:27:19 -07:00
data = r.json()
2018-10-24 14:32:53 -06:00
2018-12-18 11:27:19 -07:00
while 'next' in r.links and (not max_entries or len(data) < max_entries):
next_page_url = r.links['next']['url']
2018-10-24 14:32:53 -06:00
2018-12-18 11:27:19 -07:00
r = requests.get(url=next_page_url)
r.raise_for_status()
2018-10-24 14:32:53 -06:00
2018-12-18 11:27:19 -07:00
data.extend(r.json())
2018-10-24 14:32:53 -06:00
2018-12-18 11:27:19 -07:00
return data
2018-10-24 14:32:53 -06:00
2018-12-18 11:27:19 -07:00
def fetch_observation_data_from_id(norad_id, start, end, prod=True):
# Get all observations of the satellite
# with the given `norad_id` in the given timeframe
# https://network.satnogs.org/api/observations/?satellite__norad_cat_id=25544&start=2018-06-10T00:00&end=2018-06-15T00:00
2018-10-24 14:32:53 -06:00
2018-12-18 11:27:19 -07:00
query_str = '{}/api/observations/' \
'?satellite__norad_cat_id={}&start={}&end={}'
2018-10-24 14:32:53 -06:00
2018-12-18 11:27:19 -07:00
url = query_str.format(NETWORK_BASE_URL if prod else NETWORK_DEV_BASE_URL,
norad_id,
start.isoformat(),
end.isoformat())
2018-10-24 14:32:53 -06:00
2018-12-18 11:27:19 -07:00
observations = get_paginated_endpoint(url)
2018-10-24 14:32:53 -06:00
2018-12-18 11:27:19 -07:00
# Current prod is broken and can't filter on NORAD ID correctly,
# use client-side filtering instead
observations = list(observation for observation in observations
if observation['norad_cat_id'] == norad_id)
2018-10-24 14:32:53 -06:00
return observations
def fetch_observation_data(observation_ids, prod=True):
# Get station location from the observation via the observation_id
2018-12-18 11:27:19 -07:00
2018-10-24 14:32:53 -06:00
observations = []
for observation_id in observation_ids:
2018-12-18 11:27:19 -07:00
base_url = (NETWORK_BASE_URL if prod else NETWORK_DEV_BASE_URL)
r = requests.get(url='{}/api/observations/{}/'.format(base_url,
observation_id))
r.raise_for_status()
2018-10-24 14:32:53 -06:00
observations.append(r.json())
return observations
2018-12-18 11:27:19 -07:00
2018-10-24 14:32:53 -06:00
def fetch_ground_station_data(ground_station_ids, prod=True):
# Fetch ground station metadata from network
ground_stations = []
for ground_station_id in ground_station_ids:
2018-12-18 11:27:19 -07:00
# Skip frames from deleted groundstations, indidcated as ID 'None'
if str(ground_station_id) == 'None':
print("Skipping groundstation 'None'.")
continue
base_url = (NETWORK_BASE_URL if prod else NETWORK_DEV_BASE_URL)
r = requests.get(url='{}/api/stations/{}/'.format(base_url,
ground_station_id))
r.raise_for_status()
2018-10-24 14:32:53 -06:00
ground_stations.append(r.json())
return ground_stations
2018-12-18 11:27:19 -07:00
2018-10-24 14:32:53 -06:00
def fetch_satellite_data(norad_cat_id):
# Fetch satellite metadata from network
2018-12-18 11:27:19 -07:00
r = requests.get(url='{}/api/satellites/{}/'.format(DB_BASE_URL,
norad_cat_id))
r.raise_for_status()
2018-10-24 14:32:53 -06:00
return r.json()
2018-12-18 11:27:19 -07:00
2018-10-24 14:32:53 -06:00
def fetch_tle_of_observation(observation_id, prod=True):
2018-12-18 11:27:19 -07:00
base_url = (NETWORK_BASE_URL if prod else NETWORK_DEV_BASE_URL)
url = '{}/observations/{}/'.format(base_url,
2018-10-24 14:32:53 -06:00
observation_id)
r = requests.get(url=url)
observation_page_html = r.text
regex = r"<pre>1 (.*)<br>2 (.*)</pre>"
matches = re.search(regex, observation_page_html)
obs_tle_2 = '1 ' + matches.group(1)
obs_tle_3 = '2 ' + matches.group(2)
return [obs_tle_2, obs_tle_3]
2018-12-18 11:27:19 -07:00
def fetch_telemetry(norad_id, url):
2018-10-24 14:32:53 -06:00
# http://db-dev.satnogs.org/api/telemetry/?satellite=43595
query_str = '{}/api/telemetry/?satellite={}'
2018-12-18 11:27:19 -07:00
url = query_str.format(url, norad_id)
2018-10-24 14:32:53 -06:00
2018-12-18 11:27:19 -07:00
telemetry = get_paginated_endpoint(url)
2018-10-24 14:32:53 -06:00
2018-12-18 11:27:19 -07:00
return telemetry
2018-10-24 14:32:53 -06:00
2018-12-18 11:27:19 -07:00
def fetch_transmitters(norad_id, url):
# http://db-dev.satnogs.org/api/transmitters/?satellite__norad_cat_id=25544
2018-10-24 14:32:53 -06:00
2018-12-18 11:27:19 -07:00
query_str = '{}/api/transmitters/?satellite__norad_cat_id={}'
2018-10-24 14:32:53 -06:00
2018-12-18 11:27:19 -07:00
url = query_str.format(url, norad_id)
2018-10-24 14:32:53 -06:00
2018-12-18 11:27:19 -07:00
transmitters = get_paginated_endpoint(url)
return transmitters
2018-10-24 14:32:53 -06:00
2018-12-18 11:27:19 -07:00
def fetch_satellites(max_entries, url):
query_str = '{}/api/satellites/'
url = query_str.format(url)
2018-10-24 14:32:53 -06:00
2018-12-18 11:27:19 -07:00
satellites = get_paginated_endpoint(url, max_entries=max_entries)
return satellites
2018-10-24 14:32:53 -06:00
def post_telemetry(norad_id,
2018-12-18 11:27:19 -07:00
source, # Receiver Callsign
2018-10-24 14:32:53 -06:00
lon,
lat,
timestamp,
frame,
base_url=DB_DEV_BASE_URL):
payload = {'noradID': norad_id,
'source': source,
'timestamp': timestamp,
'latitude': lat,
'longitude': lon,
'frame': frame}
url = '{}/api/telemetry/'.format(base_url)
r = requests.post(url, data=payload)
if r.status_code != 201:
print('ERROR {}: {}'.format(r.status_code, r.text))