Refactor of caching & data storage part 14 #118

This commit is contained in:
Ian Renton
2026-08-03 19:25:25 +01:00
parent 50ab27e4a2
commit f82d611b7e
15 changed files with 144 additions and 142 deletions
+20 -27
View File
@@ -1,21 +1,4 @@
import gzip
import logging
import urllib.parse
from datetime import timedelta
import xmltodict
from diskcache import Cache
from pyhamtools import LookupLib, Callinfo, callinfo
from pyhamtools.exceptions import APIKeyMissingError
from pyhamtools.locator import latlong_to_locator
from requests.exceptions import ConnectionError, ReadTimeout, ConnectTimeout
from requests_cache import CachedSession
from core.config import config
from core.constants import HTTP_HEADERS, HAMQTH_PRG
from core.data_providers import DATA_PROVIDERS
from core.data_store import DATA_STORE
from core.url_data_cache import URLDataCache
from data.callsign import Callsign
@@ -24,14 +7,24 @@ def get_call_info(callsign, lookup_credentials):
lookup_credentials is an optional object that carries the user's QRZ.com/HamQTH credentials, if they provided them,
to enable lookup using those providers."""
callsign = Callsign(call=callsign)
for p in DATA_PROVIDERS.callsign_data_providers:
# Get new lookup data
data = p.lookup(callsign, lookup_credentials)
if data:
# Merge in turn, replacing any existing content where we have it.
for key, value in data.__dict__.items():
if value is not None:
callsign.__dict__[key] = value
callsign_data = Callsign(call=callsign)
return callsign
if callsign:
# Sort callsign providers by priority order, so we query the highest priority (lowest numbers) first, and only
# query other providers for data we are missing as we go along.
for p in sorted(DATA_PROVIDERS.callsign_data_providers, key=lambda p: p.priority):
if p.enabled:
# Get new lookup data
data = p.lookup(callsign, lookup_credentials)
if data:
# If we have new data for fields that were previously unpopulated, add them in
for key, value in data.__dict__.items():
if value is not None and callsign_data.__dict__.get(key) is None:
callsign_data.__dict__[key] = value
# If callsign data is fully populated, avoid looking up using other providers as their data will not
# be used
if callsign_data.fully_populated():
break
return callsign_data
+24 -18
View File
@@ -84,23 +84,29 @@ def get_callsign_object_from_pyhamtools_callinfo(callsign, callinfo):
"""Utility function to take the data provided by a PyHamTools CallInfo object and populate our own Callsign data
object from it"""
home_call = callinfo.get_homecall(callsign)
data = callinfo.get_all()
try:
home_call = callinfo.get_homecall(callsign)
data = callinfo.get_all(callsign)
country = data["country"] if "country" in data else None
dxcc_id = data["adif"] if "adif" in data else None
continent = data["continent"] if "continent" in data else None
cq_zone = data["cqz"] if "cqz" in data else None
itu_zone = data["ituz"] if "ituz" in data else None
lat = float(data["latitude"]) if "latitude" in data else None
lon = float(data["longitude"]) if "longitude" in data else None
country = data["country"] if "country" in data else None
dxcc_id = data["adif"] if "adif" in data else None
continent = data["continent"] if "continent" in data else None
cq_zone = data["cqz"] if "cqz" in data else None
itu_zone = data["ituz"] if "ituz" in data else None
lat = float(data["latitude"]) if "latitude" in data else None
lon = float(data["longitude"]) if "longitude" in data else None
return Callsign(call=callsign,
home_call=home_call,
country=country,
dxcc_id=dxcc_id,
continent=continent,
cq_zone=cq_zone,
itu_zone=itu_zone,
latitude=lat,
longitude=lon)
return Callsign(call=callsign,
home_call=home_call,
country=country,
dxcc_id=dxcc_id,
continent=continent,
cq_zone=cq_zone,
itu_zone=itu_zone,
latitude=lat,
longitude=lon,
location_source="DXCC")
except ValueError:
# Unknown callsign, can't look anything up, return a Callsign object with basic data so that gets cached
return Callsign(call=callsign)