mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-08-06 02:21:42 +00:00
Refactor of caching & data storage part 11 #118
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
import logging
|
||||
from datetime import datetime
|
||||
from threading import Thread, Event
|
||||
|
||||
import pytz
|
||||
from requests import ReadTimeout
|
||||
from requests.exceptions import ConnectionError, ConnectTimeout
|
||||
|
||||
from core.constants import HTTP_HEADERS
|
||||
from core.url_data_cache import URLDataCache
|
||||
from providers.callsigndata.callsign_data_provider import CallsignDataProvider
|
||||
|
||||
|
||||
class APIQueryCallsignDataProvider(CallsignDataProvider):
|
||||
"""Generic callsign data provider class for providers that fetch their data from the web on-demand using an API."""
|
||||
|
||||
def __init__(self, name, provider_config):
|
||||
""" Set up the provider."""
|
||||
super().__init__(name, provider_config)
|
||||
|
||||
def start(self):
|
||||
pass
|
||||
|
||||
def stop(self):
|
||||
pass
|
||||
@@ -0,0 +1,43 @@
|
||||
import logging
|
||||
from datetime import datetime
|
||||
|
||||
import pytz
|
||||
from pyhamtools import LookupLib, Callinfo
|
||||
|
||||
from core.utils import get_callsign_object_from_pyhamtools_callinfo
|
||||
from data.callsign import Callsign
|
||||
from providers.callsigndata.api_query_callsign_data_provider import APIQueryCallsignDataProvider
|
||||
|
||||
|
||||
class ClublogAPI(APIQueryCallsignDataProvider):
|
||||
"""Callsign data provider for Clublog's API."""
|
||||
|
||||
_callinfo = None
|
||||
|
||||
def __init__(self, provider_config):
|
||||
# API key required for this provider
|
||||
self._api_key = provider_config.get("api-key", "")
|
||||
if self._api_key != "":
|
||||
lookuplib = LookupLib(lookuptype="clublogapi", apikey=self._api_key)
|
||||
self._callinfo = Callinfo(lookuplib)
|
||||
else:
|
||||
provider_config["enabled"] = False
|
||||
logging.warning(
|
||||
"Clublog XML callsign data provider configured but no api key was provided, this has been disabled.")
|
||||
|
||||
super().__init__("Clublog API", provider_config)
|
||||
|
||||
|
||||
def lookup(self, callsign, lookup_credentials):
|
||||
callsign_data = Callsign(call=callsign)
|
||||
|
||||
try:
|
||||
callsign_data = get_callsign_object_from_pyhamtools_callinfo(callsign, self._callinfo)
|
||||
self.status = "OK"
|
||||
self.last_update_time = datetime.now(pytz.UTC)
|
||||
|
||||
except Exception as e:
|
||||
self.status = "Error"
|
||||
logging.error("Exception when looking up data from Clublog API", e, exc_info=True)
|
||||
|
||||
return callsign_data
|
||||
@@ -3,6 +3,7 @@ import logging
|
||||
|
||||
from pyhamtools import LookupLib, Callinfo
|
||||
|
||||
from core.utils import get_callsign_object_from_pyhamtools_callinfo
|
||||
from data.callsign import Callsign
|
||||
from providers.callsigndata.file_download_callsign_data_provider import FileDownloadCallsignDataProvider
|
||||
|
||||
@@ -48,21 +49,4 @@ class ClublogXML(FileDownloadCallsignDataProvider):
|
||||
return False
|
||||
|
||||
def lookup(self, callsign, lookup_credentials):
|
||||
# Lookup credentials are not required for this source.
|
||||
# Lat/lon will only be centre of country or capital city from this source
|
||||
ll = self._callinfo.get_lat_long(callsign)
|
||||
lat = None
|
||||
lon = None
|
||||
if ll and "latitude" in ll and "longitude" in ll:
|
||||
lat = float(ll["latitude"])
|
||||
lon = float(ll["longitude"])
|
||||
|
||||
return Callsign(call=callsign,
|
||||
home_call=self._callinfo.get_homecall(callsign),
|
||||
country=self._callinfo.get_country_name(callsign),
|
||||
dxcc_id=self._callinfo.get_adif_id(callsign),
|
||||
continent=self._callinfo.get_continent(callsign),
|
||||
cq_zone=self._callinfo.get_cqz(callsign),
|
||||
itu_zone=self._callinfo.get_ituz(callsign),
|
||||
latitude=lat,
|
||||
longitude=lon)
|
||||
return get_callsign_object_from_pyhamtools_callinfo(callsign, self._callinfo)
|
||||
|
||||
@@ -2,6 +2,7 @@ import logging
|
||||
|
||||
from pyhamtools import LookupLib, Callinfo
|
||||
|
||||
from core.utils import get_callsign_object_from_pyhamtools_callinfo
|
||||
from data.callsign import Callsign
|
||||
from providers.callsigndata.file_download_callsign_data_provider import FileDownloadCallsignDataProvider
|
||||
|
||||
@@ -28,21 +29,4 @@ class CountryFiles(FileDownloadCallsignDataProvider):
|
||||
return False
|
||||
|
||||
def lookup(self, callsign, lookup_credentials):
|
||||
# Lookup credentials are not required for this source.
|
||||
# Lat/lon will only be centre of country or capital city from this source
|
||||
ll = self._callinfo.get_lat_long(callsign)
|
||||
lat = None
|
||||
lon = None
|
||||
if ll and "latitude" in ll and "longitude" in ll:
|
||||
lat = float(ll["latitude"])
|
||||
lon = float(ll["longitude"])
|
||||
|
||||
return Callsign(call=callsign,
|
||||
home_call=self._callinfo.get_homecall(callsign),
|
||||
country=self._callinfo.get_country_name(callsign),
|
||||
dxcc_id=self._callinfo.get_adif_id(callsign),
|
||||
continent=self._callinfo.get_continent(callsign),
|
||||
cq_zone=self._callinfo.get_cqz(callsign),
|
||||
itu_zone=self._callinfo.get_ituz(callsign),
|
||||
latitude=lat,
|
||||
longitude=lon)
|
||||
return get_callsign_object_from_pyhamtools_callinfo(callsign, self._callinfo)
|
||||
|
||||
@@ -12,8 +12,7 @@ from providers.callsigndata.callsign_data_provider import CallsignDataProvider
|
||||
|
||||
|
||||
class FileDownloadCallsignDataProvider(CallsignDataProvider):
|
||||
"""Generic static reference data provider class for providers that fetch their data from the web by downloading a
|
||||
file."""
|
||||
"""Generic callsign data provider class for providers that fetch their data from the web by downloading a file."""
|
||||
|
||||
def __init__(self, name, provider_config, url, cache_file_path, poll_interval):
|
||||
""" Set up the provider, note poll_interval is in *days*."""
|
||||
|
||||
Reference in New Issue
Block a user