From 269e67225cd5ef8b0ea1e42a20a0db202b4d2525 Mon Sep 17 00:00:00 2001 From: Ian Renton Date: Mon, 29 Sep 2025 20:54:27 +0100 Subject: [PATCH] Disk cache for QRZ lookup data #9 --- .gitignore | 1 + core/utils.py | 10 ++++++---- requirements.txt | 3 ++- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 735cee3..c50e035 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ /.venv __pycache__ *.pyc +/.qrz_callsign_lookup_cache /sota_summit_data_cache.sqlite /gma_ref_info_cache.sqlite /config.yml diff --git a/core/utils.py b/core/utils.py index cc92cd8..51e75e0 100644 --- a/core/utils.py +++ b/core/utils.py @@ -1,6 +1,7 @@ import logging from datetime import datetime +from diskcache import Cache from pyhamtools import LookupLib, Callinfo from pyhamtools.locator import latlong_to_locator @@ -14,7 +15,7 @@ QRZ_AVAILABLE = config["qrz-password"] != "" if QRZ_AVAILABLE: LOOKUP_LIB_QRZ = LookupLib(lookuptype="qrz", username=config["qrz-username"], pwd=config["qrz-password"]) # Cache of QRZ.com callsign lookups, so we don't repeatedly call the API for stuff we already know -QRZ_CALLSIGN_DATA_CACHE = {} +QRZ_CALLSIGN_DATA_CACHE = Cache('.qrz_callsign_lookup_cache') # Infer a mode from the comment def infer_mode_from_comment(comment): @@ -81,12 +82,13 @@ def infer_itu_zone_from_callsign(call): # 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): # Fetch from cache if we can, otherwise fetch from the API and cache it - if call in QRZ_CALLSIGN_DATA_CACHE: - return QRZ_CALLSIGN_DATA_CACHE[call] + qrz_data = QRZ_CALLSIGN_DATA_CACHE.get(call) + if qrz_data: + return qrz_data elif QRZ_AVAILABLE: try: data = LOOKUP_LIB_QRZ.lookup_callsign(callsign=call) - QRZ_CALLSIGN_DATA_CACHE[call] = data + 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 diff --git a/requirements.txt b/requirements.txt index bc46aa4..d2f6cf7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,5 @@ pyhamtools~=0.12.0 telnetlib3~=2.0.8 pytz~=2025.2 requests~=2.32.5 -aprslib~=0.7.2 \ No newline at end of file +aprslib~=0.7.2 +diskcache~=5.6.3 \ No newline at end of file