diff --git a/core/lookup_helper.py b/core/lookup_helper.py index d4779d3..5a575cf 100644 --- a/core/lookup_helper.py +++ b/core/lookup_helper.py @@ -3,7 +3,7 @@ import logging from datetime import timedelta from diskcache import Cache -from pyhamtools import LookupLib, Callinfo +from pyhamtools import LookupLib, Callinfo, callinfo from pyhamtools.exceptions import APIKeyMissingError from pyhamtools.frequency import freq_to_band 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 def get_qrz_data_for_callsign(self, call): # Fetch from cache if we can, otherwise fetch from the API and cache it - qrz_data = self.QRZ_CALLSIGN_DATA_CACHE.get(call) - if qrz_data: - return qrz_data + if call in self.QRZ_CALLSIGN_DATA_CACHE: + return self.QRZ_CALLSIGN_DATA_CACHE.get(call) elif self.QRZ_AVAILABLE: try: data = self.LOOKUP_LIB_QRZ.lookup_callsign(callsign=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 + # QRZ had no info for the call, but maybe it had prefixes or suffixes. Try again with the base call. + try: + 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: return None # 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): # Fetch from cache if we can, otherwise fetch from the API and cache it - clublog_data = self.CLUBLOG_CALLSIGN_DATA_CACHE.get(call) - if clublog_data: - return clublog_data + if call in self.CLUBLOG_CALLSIGN_DATA_CACHE: + return self.CLUBLOG_CALLSIGN_DATA_CACHE.get(call) elif self.CLUBLOG_API_AVAILABLE: try: data = self.LOOKUP_LIB_CLUBLOG_API.lookup_callsign(callsign=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 + # Clublog had no info for the call, but maybe it had prefixes or suffixes. Try again with the base call. + try: + 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: # User API key was wrong, warn logging.error("Could not look up via Clublog API, key " + self.CLUBLOG_API_KEY + " was rejected.")