From 0ae102d19bf40cf8e65c4f7905b9d54b41efe80f Mon Sep 17 00:00:00 2001 From: "Fabian P. Schmidt" Date: Sat, 2 Nov 2019 23:17:57 +0100 Subject: [PATCH] get_scheduled_passes_from_network: Fix next_url logic The observations endpoint is paginated and ordered by observation start time, i.e. most recent / future observations come at first. The `get_scheduled_passes_from_network` method fetches the first page, parses it, and then fetches new pages as long as the last observation is still before the user-provided pass scheduling end time. "Established" stations have plenty of past observations, so this pass scheduling end time will be reached before the endpoint runs out of observations (and thus stops to provide a `links['next']['url']` header. For "young" stations the endpoint will run out of observations though and the old `get_scheduled_passes_from_network` implementation didn't detect the missing header and thus failed as described in issue #15. This commit introduces logic to detect a missing link header. Fixes issue #15. --- satnogs_client.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/satnogs_client.py b/satnogs_client.py index 092810f..761892c 100644 --- a/satnogs_client.py +++ b/satnogs_client.py @@ -67,6 +67,8 @@ def get_scheduled_passes_from_network(ground_station, tmin, tmax): # Loop start = True + next_url = '{}/api/observations/?ground_station={:d}'.format( + settings.NETWORK_BASE_URL, ground_station) scheduledpasses = [] logging.info("Requesting scheduled passes for ground station %d" % ground_station) @@ -74,17 +76,17 @@ def get_scheduled_passes_from_network(ground_station, tmin, tmax): # before the start time of the selected timerange for scheduling # NOTE: This algorithm is based on the order in which the API returns the observations, i.e. # most recent observations are returned at first! - while True: - if start: - r = client.get('{}/api/observations/?ground_station={:d}'.format( - settings.NETWORK_BASE_URL, ground_station)) - start = False + while next_url: + r = client.get(next_url) + + if 'next' in r.links: + next_url = r.links['next']['url'] else: - nextpage = r.links.get("next") - r = client.get(nextpage["url"]) + logging.debug("No further pages with observations") + next_url = None if not r.json(): - # Ground station has no observations yet + logging.info("Ground station has no observations yet") break # r.json() is a list of dicts/observations