Disk cache for QRZ lookup data #9

This commit is contained in:
Ian Renton
2025-09-29 20:54:27 +01:00
parent bb01deba52
commit 269e67225c
3 changed files with 9 additions and 5 deletions

1
.gitignore vendored
View File

@@ -2,6 +2,7 @@
/.venv
__pycache__
*.pyc
/.qrz_callsign_lookup_cache
/sota_summit_data_cache.sqlite
/gma_ref_info_cache.sqlite
/config.yml

View File

@@ -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

View File

@@ -6,3 +6,4 @@ telnetlib3~=2.0.8
pytz~=2025.2
requests~=2.32.5
aprslib~=0.7.2
diskcache~=5.6.3