Fix a bug that caused repeated lookup attempts for callsigns that were unknown to QRZ/ClubLog and especially those with prefixes/suffixes.

This commit is contained in:
Ian Renton
2025-10-25 09:33:44 +01:00
parent bb7b6d6f3c
commit ab05824c5d

View File

@@ -3,7 +3,7 @@ import logging
from datetime import timedelta from datetime import timedelta
from diskcache import Cache from diskcache import Cache
from pyhamtools import LookupLib, Callinfo from pyhamtools import LookupLib, Callinfo, callinfo
from pyhamtools.exceptions import APIKeyMissingError from pyhamtools.exceptions import APIKeyMissingError
from pyhamtools.frequency import freq_to_band from pyhamtools.frequency import freq_to_band
from pyhamtools.locator import latlong_to_locator from pyhamtools.locator import latlong_to_locator
@@ -266,36 +266,46 @@ class LookupHelper:
# Utility method to get QRZ.com data from cache if possible, if not get it from the API and cache it # Utility method to get QRZ.com data from cache if possible, if not get it from the API and cache it
def get_qrz_data_for_callsign(self, call): def get_qrz_data_for_callsign(self, call):
# Fetch from cache if we can, otherwise fetch from the API and cache it # Fetch from cache if we can, otherwise fetch from the API and cache it
qrz_data = self.QRZ_CALLSIGN_DATA_CACHE.get(call) if call in self.QRZ_CALLSIGN_DATA_CACHE:
if qrz_data: return self.QRZ_CALLSIGN_DATA_CACHE.get(call)
return qrz_data
elif self.QRZ_AVAILABLE: elif self.QRZ_AVAILABLE:
try: try:
data = self.LOOKUP_LIB_QRZ.lookup_callsign(callsign=call) data = self.LOOKUP_LIB_QRZ.lookup_callsign(callsign=call)
self.QRZ_CALLSIGN_DATA_CACHE.add(call, data, expire=604800) # 1 week in seconds self.QRZ_CALLSIGN_DATA_CACHE.add(call, data, expire=604800) # 1 week in seconds
return data return data
except KeyError: except KeyError:
# QRZ had no info for the call, that's OK. Cache a None so we don't try to look this up again # QRZ had no info for the call, but maybe it had prefixes or suffixes. Try again with the base call.
self.QRZ_CALLSIGN_DATA_CACHE.add(call, None, expire=604800) # 1 week in seconds try:
return None data = self.LOOKUP_LIB_QRZ.lookup_callsign(callsign=callinfo.Callinfo.get_homecall(call))
self.QRZ_CALLSIGN_DATA_CACHE.add(call, data, expire=604800) # 1 week in seconds
return data
except KeyError:
# QRZ had no info for the call, that's OK. Cache a None so we don't try to look this up again
self.QRZ_CALLSIGN_DATA_CACHE.add(call, None, expire=604800) # 1 week in seconds
return None
else: else:
return None return None
# Utility method to get Clublog API data from cache if possible, if not get it from the API and cache it # Utility method to get Clublog API data from cache if possible, if not get it from the API and cache it
def get_clublog_api_data_for_callsign(self, call): def get_clublog_api_data_for_callsign(self, call):
# Fetch from cache if we can, otherwise fetch from the API and cache it # Fetch from cache if we can, otherwise fetch from the API and cache it
clublog_data = self.CLUBLOG_CALLSIGN_DATA_CACHE.get(call) if call in self.CLUBLOG_CALLSIGN_DATA_CACHE:
if clublog_data: return self.CLUBLOG_CALLSIGN_DATA_CACHE.get(call)
return clublog_data
elif self.CLUBLOG_API_AVAILABLE: elif self.CLUBLOG_API_AVAILABLE:
try: try:
data = self.LOOKUP_LIB_CLUBLOG_API.lookup_callsign(callsign=call) data = self.LOOKUP_LIB_CLUBLOG_API.lookup_callsign(callsign=call)
self.CLUBLOG_CALLSIGN_DATA_CACHE.add(call, data, expire=604800) # 1 week in seconds self.CLUBLOG_CALLSIGN_DATA_CACHE.add(call, data, expire=604800) # 1 week in seconds
return data return data
except KeyError: except KeyError:
# Clublog had no info for the call, that's OK. Cache a None so we don't try to look this up again # Clublog had no info for the call, but maybe it had prefixes or suffixes. Try again with the base call.
self.CLUBLOG_CALLSIGN_DATA_CACHE.add(call, None, expire=604800) # 1 week in seconds try:
return None data = self.LOOKUP_LIB_CLUBLOG_API.lookup_callsign(callsign=callinfo.Callinfo.get_homecall(call))
self.CLUBLOG_CALLSIGN_DATA_CACHE.add(call, data, expire=604800) # 1 week in seconds
return data
except KeyError:
# Clublog had no info for the call, that's OK. Cache a None so we don't try to look this up again
self.CLUBLOG_CALLSIGN_DATA_CACHE.add(call, None, expire=604800) # 1 week in seconds
return None
except APIKeyMissingError: except APIKeyMissingError:
# User API key was wrong, warn # User API key was wrong, warn
logging.error("Could not look up via Clublog API, key " + self.CLUBLOG_API_KEY + " was rejected.") logging.error("Could not look up via Clublog API, key " + self.CLUBLOG_API_KEY + " was rejected.")