mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2025-10-27 08:49:27 +00:00
Disk cache for QRZ lookup data #9
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user