mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-09-28 18:22:05 +00:00
Support PARC references #156
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import json
|
||||
import logging
|
||||
from time import sleep
|
||||
|
||||
from pyhamtools.locator import locator_to_latlong
|
||||
|
||||
from core.enums import ActivityName, ActivityRefType
|
||||
from data.activity_ref import ActivityRef
|
||||
from providers.activityrefdata.file_download_activity_ref_data_provider import (
|
||||
FileDownloadActivityRefDataProvider,
|
||||
)
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class PARC(FileDownloadActivityRefDataProvider):
|
||||
"""Activity ref data provider for Protected Area Radiu Community"""
|
||||
|
||||
POLL_INTERVAL_DAYS = 30
|
||||
ACTIVITY = ActivityName.PARC
|
||||
DATA_URL = "https://parc-community.com/api/partner/parks?format=json"
|
||||
|
||||
def __init__(self, provider_config):
|
||||
# Ensure there is an API key in our config, and set up the query URL using it. If no key is provided,
|
||||
# disable this spot provider.
|
||||
api_key = provider_config.get("api_key", "")
|
||||
if api_key == "":
|
||||
provider_config["enabled"] = False
|
||||
logger.warning(
|
||||
"PARC activity reference provider configured but no api key was provided, this API will not be queried."
|
||||
)
|
||||
|
||||
super().__init__(self.ACTIVITY, provider_config, self.DATA_URL + "&key=" + api_key, self.POLL_INTERVAL_DAYS)
|
||||
|
||||
def _http_response_to_data(self, http_response):
|
||||
new_data = []
|
||||
# Use stdlib json rather than http_response.json(), as the PARC data can contain NaN values which simplejson
|
||||
# rejects
|
||||
data = json.loads(http_response.text)
|
||||
for ref in data["parks"]:
|
||||
ref_id = ref["reference"]
|
||||
name = ref["name"]
|
||||
grid = ref["grid"]
|
||||
lat = ref["latitude"]
|
||||
lon = ref["longitude"]
|
||||
|
||||
# grid, lat & lon can all be null, but sometimes we have grid but not lat/lon
|
||||
if grid and (lat is None or lon is None):
|
||||
lat, lon = locator_to_latlong(grid)
|
||||
|
||||
new_data.append(
|
||||
ActivityRef(
|
||||
activity=self.ACTIVITY,
|
||||
id=ref_id,
|
||||
# Name is sometimes NaN, not even a string somehow?
|
||||
name=name if isinstance(name, str) else None,
|
||||
ref_type=ActivityRefType.PARK,
|
||||
url=f"https://parc-community.com/parks/{ref_id}",
|
||||
grid=grid,
|
||||
latitude=lat,
|
||||
longitude=lon,
|
||||
)
|
||||
)
|
||||
|
||||
# Bail out if a stop has been requested, i.e. the program is shutting down - no need to parse the rest
|
||||
# of the data in this case
|
||||
if self._stop_event.is_set():
|
||||
break
|
||||
|
||||
# Very short pause. This will extend the time to handle activity refs by a few seconds but will ensure some time
|
||||
# is available for other threads e.g. the web server.
|
||||
sleep(0.001)
|
||||
|
||||
return new_data
|
||||
Reference in New Issue
Block a user