mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2026-09-20 14:27:42 +00:00
145 lines
6.0 KiB
Python
145 lines
6.0 KiB
Python
import logging
|
|
import re
|
|
from datetime import datetime
|
|
from typing import ClassVar
|
|
|
|
import pytz
|
|
import requests
|
|
|
|
from core.constants import HTTP_HEADERS
|
|
from core.enums import ActivityName, Mode
|
|
from data.activity_ref import ActivityRef
|
|
from data.spot import Spot
|
|
from providers.spot.http_spot_provider import HTTPSpotProvider
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class ParksNPeaks(HTTPSpotProvider):
|
|
"""Spot provider for Parks n Peaks"""
|
|
|
|
POLL_INTERVAL_SEC = 120
|
|
SPOTS_URL = "https://www.parksnpeaks.org/api/ALL"
|
|
SUBMIT_URL = "https://www.parksnpeaks.org/api/SPOT/"
|
|
SUBMITTABLE_ACTIVITIES: ClassVar[list[ActivityName]] = [
|
|
ActivityName.POTA,
|
|
ActivityName.SOTA,
|
|
ActivityName.WWFF,
|
|
ActivityName.HEMA,
|
|
ActivityName.WOTA,
|
|
ActivityName.ZLOTA,
|
|
ActivityName.SIOTA,
|
|
ActivityName.KRMNPA,
|
|
ActivityName.SANPCPA,
|
|
]
|
|
|
|
def __init__(self, provider_config):
|
|
super().__init__("ParksNPeaks", provider_config, self.SPOTS_URL, self.POLL_INTERVAL_SEC)
|
|
|
|
def _http_response_to_spots(self, http_response):
|
|
new_spots = []
|
|
# Iterate through source data
|
|
if http_response and http_response != "":
|
|
for source_spot in http_response.json():
|
|
# Convert to our spot format
|
|
spot = Spot(
|
|
source=self.name,
|
|
source_id=source_spot["actID"],
|
|
dx_call=source_spot["actCallsign"].upper(),
|
|
de_call=source_spot["actSpoter"].upper() if source_spot["actSpoter"] != "" else None,
|
|
# typo exists in API
|
|
freq=float(source_spot["actFreq"].replace(",", "").replace("+-", "").replace("+/-", "").strip())
|
|
* 1000000
|
|
if (source_spot["actFreq"] != "")
|
|
else None,
|
|
# Seen PNP spots with empty frequency, and with comma-separated thousands digits
|
|
mode=Mode.from_name(source_spot["actMode"].upper()),
|
|
comment=source_spot["actComments"],
|
|
time=datetime.strptime(source_spot["actTime"], "%Y-%m-%d %H:%M:%S")
|
|
.replace(tzinfo=pytz.UTC)
|
|
.timestamp(),
|
|
)
|
|
|
|
# Extract a de_call if it's in the comment but not in the "actSpoter" field
|
|
m = re.search(r"\(de ([A-Za-z0-9]*)\)", spot.comment or "")
|
|
if not spot.de_call and m:
|
|
spot.de_call = str(m.group(1))
|
|
|
|
# Record activity information
|
|
activity = source_spot["actClass"].upper()
|
|
ref_id = source_spot["actSiteID"]
|
|
|
|
if activity:
|
|
spot.sig = activity
|
|
|
|
if ref_id:
|
|
activity_refs = [
|
|
ActivityRef(
|
|
id=ref_id,
|
|
sig=activity,
|
|
# Free text location is not present in all spots, so only add it if it's set
|
|
name=source_spot["actLocation"]
|
|
if "actLocation" in source_spot and source_spot["actLocation"] != ""
|
|
else None,
|
|
)
|
|
]
|
|
spot.sig_refs = activity_refs
|
|
|
|
else:
|
|
# If no actSiteID is set, e.g. because actClass is "QRP", sometimes we still have an actLocation
|
|
# which is free text like "SOTA G/SC-001". If we have that, and not a normal comment field, use
|
|
# that location as the comment field so the information doesn't get lost.
|
|
if (
|
|
"actLocation" in source_spot
|
|
and source_spot["actLocation"] != ""
|
|
and ("actComments" not in source_spot or source_spot["actComments"] == "")
|
|
):
|
|
spot.comment = source_spot["actLocation"]
|
|
|
|
# Log a warning for the developer if PnP gives us an unknown programme we've never seen before
|
|
if activity not in [
|
|
ActivityName.POTA,
|
|
ActivityName.SOTA,
|
|
ActivityName.WWFF,
|
|
ActivityName.HEMA,
|
|
ActivityName.SIOTA,
|
|
ActivityName.ZLOTA,
|
|
ActivityName.KRMNPA,
|
|
ActivityName.SANPCPA,
|
|
ActivityName.LLOTA,
|
|
ActivityName.QRP,
|
|
]:
|
|
logger.warning(
|
|
f"PNP spot found with activity {activity}, developer needs to add support for this!"
|
|
)
|
|
|
|
# Add new spot to the list
|
|
new_spots.append(spot)
|
|
return new_spots
|
|
|
|
def can_submit_spot(self, activity):
|
|
return activity in self.SUBMITTABLE_ACTIVITIES
|
|
|
|
def submit_spot(self, spot, credentials):
|
|
# TODO test this works
|
|
user_id = credentials.get("user_id", "")
|
|
api_key = credentials.get("api_key", "")
|
|
if not user_id or not api_key:
|
|
raise ValueError(
|
|
"Parks N Peaks user ID and API key are required. Get yours from your Parks N Peaks account."
|
|
)
|
|
ref_id = spot.sig_refs[0].id if spot.sig_refs else ""
|
|
body = {
|
|
"actClass": spot.sig or "",
|
|
"actCallsign": spot.dx_call,
|
|
"actSite": ref_id,
|
|
"mode": spot.mode or "",
|
|
"freq": str(spot.freq / 1000000.0),
|
|
"comments": spot.comment or "",
|
|
"userID": user_id,
|
|
"APIKey": api_key,
|
|
}
|
|
response = requests.post(self.SUBMIT_URL, json=body, headers=HTTP_HEADERS, timeout=(5, 30))
|
|
if not response.ok:
|
|
raise RuntimeError(f"Parks N Peaks API returned {response.status_code!s}: {response.text}")
|