Files
spothole/providers/alert/parksnpeaks.py
T

79 lines
3.0 KiB
Python

import logging
from datetime import datetime
import pytz
from core.enums import AlertType
from data.activity_ref import ActivityRef
from data.alert import Alert
from providers.alert.http_alert_provider import HTTPAlertProvider
logger = logging.getLogger(__name__)
class ParksNPeaks(HTTPAlertProvider):
"""Alert provider for Parks n Peaks"""
POLL_INTERVAL_SEC = 1800
ALERTS_URL = "https://parksnpeaks.org/api/ALERTS/"
def __init__(self, provider_config):
super().__init__("ParksNPeaks", provider_config, self.ALERTS_URL, self.POLL_INTERVAL_SEC)
def _http_response_to_alerts(self, http_response):
new_alerts = []
# Iterate through source data
for source_alert in http_response.json():
# Calculate some things
activity = source_alert["Class"].upper()
if " - " in source_alert["Location"]:
split = source_alert["Location"].split(" - ")
ref_id = split[0]
ref_name = split[1]
else:
ref_id = source_alert["WWFFID"]
ref_name = source_alert["Location"]
start_time = (
datetime.strptime(source_alert["alTime"], "%Y-%m-%d %H:%M:%S").replace(tzinfo=pytz.UTC).timestamp()
)
activity_refs = []
# PnP can give us an alert of class "QRP" which is the only one that's not a real activity in Spothole's
# list, so mask this out if we got it.
if activity != "QRP":
activity_refs = [ActivityRef(id=ref_id, sig=activity, name=ref_name)]
# Convert to our alert format
alert = Alert(
source=self.name,
source_id=source_alert["alID"],
dx_calls=[source_alert["CallSign"].upper()],
freqs_modes=f"{source_alert['Freq']} {source_alert['MODE']}",
comment=source_alert["Comments"],
sig_refs=activity_refs,
start_time=start_time,
alert_type=AlertType.XOTA,
)
# Log a warning for the developer if PnP gives us an unknown programme we've never seen before
if activity and activity not in [
"POTA",
"SOTA",
"WWFF",
"HEMA",
"SIOTA",
"ZLOTA",
"KRMNPA",
"SANPCPA",
"LLOTA",
"QRP",
]:
logger.warning(f"PNP alert found with activity {activity}, developer needs to add support for this!")
# If this is POTA, SOTA or WWFF data we already have it through other means, so ignore. Otherwise, add to
# the alert list. Note that while ZLOTA has its own spots API, it doesn't have its own alerts API. So that
# means the PnP *spot* provider rejects ZLOTA spots here, but the PnP *alerts* provider here allows ZLOTA.
if activity not in ["POTA", "SOTA", "WWFF"]:
new_alerts.append(alert)
return new_alerts