From 74a3bea61989b6b93d19c4de96e1e76df0b93066 Mon Sep 17 00:00:00 2001 From: Athanasius Date: Thu, 15 Apr 2021 18:16:41 +0100 Subject: [PATCH 1/2] CAPI: catch lastStarport->services being an empty list (not dict)o This is caused, at least on Odyssey Alpha Phase 3, 4.0.0.20, by: 1. Jump to a new system. 2. Don't dock yet. 3. Cause CAPI pull with 'Update' button. 4. The PTS CAPI server is returning: "lastStarport": { "faction": "", "id": 3221604096, "minorfaction": "", "name": "", "services": [] }, So actually we need to decide it's not sane at all. --- companion.py | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/companion.py b/companion.py index bb9ce713..56f7a27b 100644 --- a/companion.py +++ b/companion.py @@ -603,7 +603,7 @@ class Session(object): return data - def station(self) -> CAPIData: + def station(self) -> CAPIData: # noqa: CCR001 """Perform CAPI /profile endpoint query for station data.""" data = self.query(URL_QUERY) if 'commander' not in data: @@ -614,6 +614,13 @@ class Session(object): return data services = data['lastStarport'].get('services', {}) + if not isinstance(services, dict): + # This happens if you're in-ship in-space and force an Update + logger.error(f'services is "{type(services)}", not dict !') + if __debug__: + self.dump_capi_data(data) + + services = {} last_starport_name = data['lastStarport']['name'] last_starport_id = int(data['lastStarport']['id']) @@ -682,13 +689,21 @@ class Session(object): def dump_capi_data(self, data: CAPIData) -> None: """Dump CAPI data to file for examination.""" if os.path.isdir('dump'): - system = data['lastSystem']['name'] + try: + system = data['lastSystem']['name'] - if data['commander'].get('docked'): - station = f'.{data["lastStarport"]["name"]}' + except (KeyError, ValueError): + system = '' - else: - station = '' + try: + if data['commander'].get('docked'): + station = f'.{data["lastStarport"]["name"]}' + + else: + station = '' + + except (KeyError, ValueError): + station = '' timestamp = time.strftime('%Y-%m-%dT%H.%M.%S', time.localtime()) with open(f'dump/{system}{station}.{timestamp}.json', 'wb') as h: From e4f7c998cd8aea82fa5ad34e07ebf9591e7b382c Mon Sep 17 00:00:00 2001 From: Athanasius Date: Thu, 15 Apr 2021 19:39:40 +0100 Subject: [PATCH 2/2] companion: Improve station() checks and documentation * Expand on the docstring to point out extra queries might be made. * Add checks for lastStarport and then name within that being properly present and set. --- companion.py | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/companion.py b/companion.py index 56f7a27b..60f12d4a 100644 --- a/companion.py +++ b/companion.py @@ -604,7 +604,17 @@ class Session(object): return data def station(self) -> CAPIData: # noqa: CCR001 - """Perform CAPI /profile endpoint query for station data.""" + """ + Perform CAPI quer(y|ies) for station data. + + A /profile query is performed to check that we are docked (or on foot) + and the station name and marketid match the prior Docked event. + If they do match, and the services list says they're present, also + retrieve CAPI market and/or shipyard/outfitting data and merge into + the /profile data. + + :return: Possibly augmented CAPI data. + """ data = self.query(URL_QUERY) if 'commander' not in data: logger.error('No commander in returned data') @@ -613,17 +623,33 @@ class Session(object): if not data['commander'].get('docked') and not monitor.state['OnFoot']: return data - services = data['lastStarport'].get('services', {}) + # Sanity checks in case data isn't as we expect, and maybe 'docked' flag + # is also lagging. + if (last_starport := data.get('lastStarport')) is None: + logger.error("No lastStarport in data!") + return data + + if ((last_starport_name := last_starport.get('name')) is None + or last_starport_name == ''): + # This could well be valid if you've been out exploring for a long + # time. + logger.warning("No lastStarport name!") + return data + + services = last_starport.get('services', {}) if not isinstance(services, dict): - # This happens if you're in-ship in-space and force an Update + # Odyssey Alpha Phase 3 4.0.0.20 has been observed having + # this be an empty list when you've jumped to another system + # and not yet docked. As opposed to no services key at all + # or an empty dict. logger.error(f'services is "{type(services)}", not dict !') if __debug__: self.dump_capi_data(data) + # Set an empty dict so as to not have to retest below. services = {} - last_starport_name = data['lastStarport']['name'] - last_starport_id = int(data['lastStarport']['id']) + last_starport_id = int(last_starport.get('id')) if services.get('commodities'): marketdata = self.query(URL_MARKET)