mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2025-10-27 08:49:27 +00:00
66 lines
3.1 KiB
Python
66 lines
3.1 KiB
Python
import re
|
|
from datetime import datetime, timedelta
|
|
|
|
import pytz
|
|
from requests_cache import CachedSession
|
|
|
|
from core.constants import HTTP_HEADERS
|
|
from core.sig_utils import get_icon_for_sig, get_ref_regex_for_sig
|
|
from data.spot import Spot
|
|
from spotproviders.http_spot_provider import HTTPSpotProvider
|
|
|
|
|
|
# Spot provider for Parks on the Air
|
|
class POTA(HTTPSpotProvider):
|
|
POLL_INTERVAL_SEC = 120
|
|
SPOTS_URL = "https://api.pota.app/spot/activator"
|
|
# Might need to look up extra park data
|
|
PARK_URL_ROOT = "https://api.pota.app/park/"
|
|
PARK_DATA_CACHE_TIME_DAYS = 30
|
|
PARK_DATA_CACHE = CachedSession("cache/pota_park_data_cache",
|
|
expire_after=timedelta(days=PARK_DATA_CACHE_TIME_DAYS))
|
|
|
|
def __init__(self, provider_config):
|
|
super().__init__(provider_config, self.SPOTS_URL, self.POLL_INTERVAL_SEC)
|
|
|
|
def http_response_to_spots(self, http_response):
|
|
new_spots = []
|
|
# Iterate through source data
|
|
for source_spot in http_response.json():
|
|
# Convert to our spot format
|
|
spot = Spot(source=self.name,
|
|
source_id=source_spot["spotId"],
|
|
dx_call=source_spot["activator"].upper(),
|
|
de_call=source_spot["spotter"].upper(),
|
|
freq=float(source_spot["frequency"]) * 1000,
|
|
mode=source_spot["mode"].upper(),
|
|
comment=source_spot["comments"],
|
|
sig="POTA",
|
|
sig_refs=[source_spot["reference"]],
|
|
sig_refs_names=[source_spot["name"]],
|
|
sig_refs_urls=["https://pota.app/#/park/" + source_spot["reference"]],
|
|
icon=get_icon_for_sig("POTA"),
|
|
time=datetime.strptime(source_spot["spotTime"], "%Y-%m-%dT%H:%M:%S").replace(
|
|
tzinfo=pytz.UTC).timestamp(),
|
|
dx_grid=source_spot["grid6"],
|
|
dx_latitude=source_spot["latitude"],
|
|
dx_longitude=source_spot["longitude"])
|
|
|
|
# Sometimes we can get other refs in the comments for n-fer activations, extract them
|
|
all_comment_refs = re.findall(get_ref_regex_for_sig("POTA"), spot.comment)
|
|
for r in all_comment_refs:
|
|
if r not in spot.sig_refs:
|
|
spot.sig_refs.append(r.upper())
|
|
spot.sig_refs_urls.append("https://pota.app/#/park/" + r.upper())
|
|
|
|
# Now we need to look up the name of that reference from the API, because the comment won't have it
|
|
park_response = self.PARK_DATA_CACHE.get(self.PARK_URL_ROOT + r.upper(), headers=HTTP_HEADERS)
|
|
park_data = park_response.json()
|
|
if park_data and "name" in park_data:
|
|
spot.sig_refs_names.append(park_data["name"])
|
|
|
|
# Add to our list. Don't worry about de-duping, removing old spots etc. at this point; other code will do
|
|
# that for us.
|
|
new_spots.append(spot)
|
|
return new_spots
|