Download country-files.com cty.plist separately so we can cache the result and handle exceptions

This commit is contained in:
Ian Renton
2025-10-13 19:54:20 +01:00
parent dbeebe32f3
commit 9d2b2a1f66
2 changed files with 4 additions and 3 deletions

View File

@@ -1,3 +1,4 @@
from core.config import SERVER_OWNER_CALLSIGN
from data.band import Band from data.band import Band
# General software # General software

View File

@@ -20,10 +20,10 @@ from core.constants import BANDS, UNKNOWN_BAND, CW_MODES, PHONE_MODES, DATA_MODE
# requests_cache library to prevent re-downloading too quickly if the software keeps restarting. # requests_cache library to prevent re-downloading too quickly if the software keeps restarting.
def download_country_files_cty_plist(): def download_country_files_cty_plist():
try: try:
logging.info("Downloading Country-files.com cty.plist...")
response = COUNTRY_FILES_CTY_PLIST_CACHE.get("https://www.country-files.com/cty/cty.plist", response = COUNTRY_FILES_CTY_PLIST_CACHE.get("https://www.country-files.com/cty/cty.plist",
headers=HTTP_HEADERS).text headers=HTTP_HEADERS).text
# write to file
with open(COUNTRY_FILES_CTY_PLIST_DOWNLOAD_LOCATION, "w") as f: with open(COUNTRY_FILES_CTY_PLIST_DOWNLOAD_LOCATION, "w") as f:
f.write(response) f.write(response)
return True return True
@@ -36,13 +36,12 @@ def download_country_files_cty_plist():
# database live if possible. # database live if possible.
def download_clublog_ctyxml(): def download_clublog_ctyxml():
try: try:
# Read the file inside the .gz archive located at url logging.info("Downloading Clublog cty.xml...")
response = CLUBLOG_CTY_XML_CACHE.get("https://cdn.clublog.org/cty.php?api=" + CLUBLOG_API_KEY, response = CLUBLOG_CTY_XML_CACHE.get("https://cdn.clublog.org/cty.php?api=" + CLUBLOG_API_KEY,
headers=HTTP_HEADERS).raw headers=HTTP_HEADERS).raw
with gzip.GzipFile(fileobj=response) as uncompressed: with gzip.GzipFile(fileobj=response) as uncompressed:
file_content = uncompressed.read() file_content = uncompressed.read()
# write to file in binary mode 'wb'
with open(CLUBLOG_XML_DOWNLOAD_LOCATION, "wb") as f: with open(CLUBLOG_XML_DOWNLOAD_LOCATION, "wb") as f:
f.write(file_content) f.write(file_content)
return True return True
@@ -57,6 +56,7 @@ def download_clublog_ctyxml():
# Clublog (XML download). The lookup functions iterate through these in a sensible order, looking for suitable data. # Clublog (XML download). The lookup functions iterate through these in a sensible order, looking for suitable data.
COUNTRY_FILES_CTY_PLIST_CACHE = CachedSession("cache/country_files_city_plist_cache", expire_after=timedelta(days=10)) COUNTRY_FILES_CTY_PLIST_CACHE = CachedSession("cache/country_files_city_plist_cache", expire_after=timedelta(days=10))
COUNTRY_FILES_CTY_PLIST_DOWNLOAD_LOCATION = "cache/cty.plist" COUNTRY_FILES_CTY_PLIST_DOWNLOAD_LOCATION = "cache/cty.plist"
download_country_files_cty_plist()
LOOKUP_LIB_BASIC = LookupLib(lookuptype="countryfile", filename=COUNTRY_FILES_CTY_PLIST_DOWNLOAD_LOCATION) LOOKUP_LIB_BASIC = LookupLib(lookuptype="countryfile", filename=COUNTRY_FILES_CTY_PLIST_DOWNLOAD_LOCATION)
CALL_INFO_BASIC = Callinfo(LOOKUP_LIB_BASIC) CALL_INFO_BASIC = Callinfo(LOOKUP_LIB_BASIC)