Files
spothole/core/utils.py
2025-09-27 10:00:12 +01:00

68 lines
1.9 KiB
Python

from core.constants import BANDS, UNKNOWN_BAND, CW_MODES, PHONE_MODES, DATA_MODES, ALL_MODES
from pyhamtools import LookupLib, Callinfo
# Static lookup helpers from pyhamtools
# todo in future add QRZ as a second lookup option in case it provides more data?
lookuplib = LookupLib(lookuptype="countryfile")
callinfo = Callinfo(lookuplib)
# Infer a mode from the comment
def infer_mode_from_comment(comment):
for mode in ALL_MODES:
if mode in comment.upper():
return mode
return None
# Infer a "mode family" from a mode.
def infer_mode_family_from_mode(mode):
if mode.upper() in CW_MODES:
return "CW"
elif mode.upper() in PHONE_MODES:
return "PHONE"
elif mode.upper() in DATA_MODES:
return "DATA"
else:
print("Found an unrecognised mode: " + mode + ". Developer should categorise this.")
return None
# Infer a band from a frequency in kHz
def infer_band_from_freq(freq):
for b in BANDS:
if b.start_freq <= freq <= b.end_freq:
return b
return UNKNOWN_BAND
# Infer a country name from a callsign
def infer_country_from_callsign(call):
try:
return callinfo.get_country_name(call)
except KeyError as e:
return None
# Infer a DXCC ID from a callsign
def infer_dxcc_id_from_callsign(call):
try:
return callinfo.get_adif_id(call)
except KeyError as e:
return None
# Infer a continent shortcode from a callsign
def infer_continent_from_callsign(call):
try:
return callinfo.get_continent(call)
except KeyError as e:
return None
# Infer a CQ zone from a callsign
def infer_cq_zone_from_callsign(call):
try:
return callinfo.get_cqz(call)
except KeyError as e:
return None
# Infer a ITU zone from a callsign
def infer_itu_zone_from_callsign(call):
try:
return callinfo.get_ituz(call)
except KeyError as e:
return None