mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-08-05 18:11:41 +00:00
41 lines
1.4 KiB
Python
41 lines
1.4 KiB
Python
import logging
|
|
import re
|
|
|
|
from core.data_store import DATA_STORE
|
|
from staticdataproviders.file_download_static_data_provider import FileDownloadStaticDataProvider
|
|
|
|
|
|
class K0SWE(FileDownloadStaticDataProvider):
|
|
"""Static data provider for K0SWE's dxcc.json, which provides callsign regex to DXCC entity mapping, plus DXCC to
|
|
continent, flag emoji etc."""
|
|
|
|
POLL_INTERVAL_DAYS = 7
|
|
DATA_URL = "https://raw.githubusercontent.com/k0swe/dxcc-json/refs/heads/main/dxcc.json"
|
|
|
|
def __init__(self, provider_config):
|
|
super().__init__("K0SWE DXCC JSON", provider_config, self.DATA_URL, self.POLL_INTERVAL_DAYS)
|
|
|
|
def _handle_http_response(self, http_response):
|
|
try:
|
|
dxcc_list = http_response.json()["dxcc"]
|
|
# Reformat as a map for to place in the data store
|
|
dxcc_map = {}
|
|
for dxcc in dxcc_list:
|
|
dxcc_map[dxcc["entityCode"]] = dxcc
|
|
|
|
# Precompile regex matches for DXCCs to improve efficiency when iterating through them
|
|
for dxcc in dxcc_map.values():
|
|
dxcc["_prefixRegexCompiled"] = re.compile(dxcc["prefixRegex"])
|
|
|
|
# Add to data store
|
|
for k, v in dxcc_map.items():
|
|
DATA_STORE.dxcc_data[k] = v
|
|
|
|
return True
|
|
|
|
except Exception as e:
|
|
logging.error("Exception when loading K0SWE dxcc.json.", e, exc_info=True)
|
|
return False
|
|
|
|
|