Add QRZ fallback to DXCC/country/zone lookups

This commit is contained in:
Ian Renton
2025-10-10 06:47:42 +01:00
parent c455e6e1a7
commit 5c3adcdd4d

View File

@@ -47,47 +47,86 @@ def infer_band_from_freq(freq):
# Infer a country name from a callsign # Infer a country name from a callsign
def infer_country_from_callsign(call): def infer_country_from_callsign(call):
try: try:
# Get base callsign, assuming this will be the longest of any /-separated sections. # Use the full callsign, falling back to the base callsign, assuming this will be the longest of any /-separated
base_call = max(call.split("/"), key=len) # sections.
return CALL_INFO_BASIC.get_country_name(base_call) country = CALL_INFO_BASIC.get_country_name(call)
if not country:
base_call = max(call.split("/"), key=len)
country = CALL_INFO_BASIC.get_country_name(base_call)
except KeyError as e: except KeyError as e:
return None country = None
if not country:
qrz_data = get_qrz_data_for_callsign(call)
if qrz_data and "country" in qrz_data:
country = qrz_data["country"]
return country
# Infer a DXCC ID from a callsign # Infer a DXCC ID from a callsign
def infer_dxcc_id_from_callsign(call): def infer_dxcc_id_from_callsign(call):
try: try:
# Get base callsign, assuming this will be the longest of any /-separated sections. # Start with the basic country-files.com-based decoder. Use the full callsign, falling back to the base
base_call = max(call.split("/"), key=len) # callsign, assuming this will be the longest of any /-separated sections. Then if that doesn't provide data,
return CALL_INFO_BASIC.get_adif_id(base_call) # and we have QRZ data, try the same with that.
dxcc = CALL_INFO_BASIC.get_adif_id(call)
if not dxcc:
base_call = max(call.split("/"), key=len)
dxcc = CALL_INFO_BASIC.get_adif_id(base_call)
except KeyError as e: except KeyError as e:
return None dxcc = None
if not dxcc:
qrz_data = get_qrz_data_for_callsign(call)
if qrz_data and "adif" in qrz_data:
dxcc = qrz_data["adif"]
return dxcc
# Infer a continent shortcode from a callsign # Infer a continent shortcode from a callsign
def infer_continent_from_callsign(call): def infer_continent_from_callsign(call):
try: try:
# Get base callsign, assuming this will be the longest of any /-separated sections. # Start with the basic country-files.com-based decoder. Use the full callsign, falling back to the base
base_call = max(call.split("/"), key=len) # callsign, assuming this will be the longest of any /-separated sections.
return CALL_INFO_BASIC.get_continent(base_call) continent = CALL_INFO_BASIC.get_continent(call)
if not continent:
base_call = max(call.split("/"), key=len)
continent = CALL_INFO_BASIC.get_continent(base_call)
except KeyError as e: except KeyError as e:
return None continent = None
return continent
# Infer a CQ zone from a callsign # Infer a CQ zone from a callsign
def infer_cq_zone_from_callsign(call): def infer_cq_zone_from_callsign(call):
try: try:
# Get base callsign, assuming this will be the longest of any /-separated sections. # Start with the basic country-files.com-based decoder. Use the full callsign, falling back to the base
base_call = max(call.split("/"), key=len) # callsign, assuming this will be the longest of any /-separated sections. Then if that doesn't provide data,
return CALL_INFO_BASIC.get_cqz(base_call) # and we have QRZ data, try the same with that.
cqz = CALL_INFO_BASIC.get_cqz(call)
if not cqz:
base_call = max(call.split("/"), key=len)
cqz = CALL_INFO_BASIC.get_cqz(base_call)
except KeyError as e: except KeyError as e:
return None cqz = None
if not cqz:
qrz_data = get_qrz_data_for_callsign(call)
if qrz_data and "cqz" in qrz_data:
cqz = qrz_data["cqz"]
return cqz
# Infer a ITU zone from a callsign # Infer a ITU zone from a callsign
def infer_itu_zone_from_callsign(call): def infer_itu_zone_from_callsign(call):
try: try:
# Get base callsign, assuming this will be the longest of any /-separated sections. # Start with the basic country-files.com-based decoder. Use the full callsign, falling back to the base
base_call = max(call.split("/"), key=len) # callsign, assuming this will be the longest of any /-separated sections. Then if that doesn't provide data,
return CALL_INFO_BASIC.get_ituz(base_call) # and we have QRZ data, try the same with that.
ituz = CALL_INFO_BASIC.get_ituz(call)
if not ituz:
base_call = max(call.split("/"), key=len)
ituz = CALL_INFO_BASIC.get_ituz(base_call)
except KeyError as e: except KeyError as e:
return None ituz = None
if not ituz:
qrz_data = get_qrz_data_for_callsign(call)
if qrz_data and "ituz" in qrz_data:
ituz = qrz_data["ituz"]
return ituz
# 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(call): def get_qrz_data_for_callsign(call):