mirror of
https://git.ianrenton.com/ian/spothole.git
synced 2025-10-27 16:59:25 +00:00
First commit
This commit is contained in:
65
providers/pota.py
Normal file
65
providers/pota.py
Normal file
@@ -0,0 +1,65 @@
|
||||
from datetime import datetime, timezone
|
||||
import pytz
|
||||
from data.spot import Spot
|
||||
from providers.provider import Provider
|
||||
from threading import Timer
|
||||
import requests
|
||||
|
||||
class POTA(Provider):
|
||||
POLL_INTERVAL_SEC = 120
|
||||
SPOTS_URL = "https://api.pota.app/spot/activator"
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
self.poll_timer = None
|
||||
|
||||
def name(self):
|
||||
return "POTA"
|
||||
|
||||
def start(self):
|
||||
self.poll()
|
||||
|
||||
def stop(self):
|
||||
self.poll_timer.cancel()
|
||||
|
||||
def poll(self):
|
||||
try:
|
||||
# Request data from API
|
||||
source_data = requests.get(self.SPOTS_URL, headers=self.HTTP_HEADERS).json()
|
||||
# Build a list of spots we haven't seen before
|
||||
new_spots = []
|
||||
# Iterate through source data
|
||||
for source_spot in source_data:
|
||||
# Convert to our spot format
|
||||
spot = Spot(source="POTA",
|
||||
source_id=source_spot["spotId"],
|
||||
dx_call=source_spot["activator"],
|
||||
de_call=source_spot["spotter"],
|
||||
freq=float(source_spot["frequency"]),
|
||||
mode=source_spot["mode"],
|
||||
comment=source_spot["comments"],
|
||||
sig="POTA",
|
||||
sig_refs=[source_spot["reference"]],
|
||||
sig_refs_names=[source_spot["name"]],
|
||||
time=datetime.strptime(source_spot["spotTime"], "%Y-%m-%dT%H:%M:%S").replace(tzinfo=pytz.UTC),
|
||||
grid=source_spot["grid6"],
|
||||
latitude=source_spot["latitude"],
|
||||
longitude=source_spot["longitude"],
|
||||
qrt="QRT" in source_spot["comments"].upper())
|
||||
# Fill in any blanks
|
||||
spot.infer_missing()
|
||||
# Add to our list
|
||||
new_spots.append(spot)
|
||||
|
||||
# Submit the new spots for processing
|
||||
self.submit(new_spots)
|
||||
|
||||
self.status = "OK"
|
||||
self.last_update_time = datetime.now(timezone.utc)
|
||||
|
||||
|
||||
except requests.exceptions.RequestException as e:
|
||||
self.status = "Error"
|
||||
|
||||
self.poll_timer = Timer(self.POLL_INTERVAL_SEC, self.poll)
|
||||
self.poll_timer.start()
|
||||
41
providers/provider.py
Normal file
41
providers/provider.py
Normal file
@@ -0,0 +1,41 @@
|
||||
from datetime import datetime
|
||||
import pytz
|
||||
from core.constants import SOFTWARE_NAME, SOFTWARE_VERSION
|
||||
|
||||
# Generic data provider class. Subclasses of this query the individual APIs for data.
|
||||
class Provider:
|
||||
|
||||
# HTTP headers used for providers that use HTTP
|
||||
HTTP_HEADERS = { "User-Agent": SOFTWARE_NAME + " " + SOFTWARE_VERSION }
|
||||
|
||||
# Constructor
|
||||
def __init__(self):
|
||||
self.last_update_time = datetime.min.replace(tzinfo=pytz.UTC)
|
||||
self.last_spot_time = datetime.min.replace(tzinfo=pytz.UTC)
|
||||
self.status = "Not Started"
|
||||
self.spot_list = None
|
||||
|
||||
# Return the name of the provider
|
||||
def name(self):
|
||||
raise NotImplementedError("Subclasses must implement this method")
|
||||
|
||||
# Set up the provider, e.g. giving it the spot list to work from
|
||||
def setup(self, spot_list):
|
||||
self.spot_list = spot_list
|
||||
|
||||
# Start the provider. This should return immediately after spawning threads to access the remote resources
|
||||
def start(self):
|
||||
raise NotImplementedError("Subclasses must implement this method")
|
||||
|
||||
# Submit one or more new spots retrieved from the provider. Only spots that are newer than the last spot retrieved
|
||||
# by this provider will be added to the spot list, to prevent duplications. This is called by the subclasses on
|
||||
# receiving spots.
|
||||
def submit(self, spots):
|
||||
for spot in spots:
|
||||
if spot.time > self.last_spot_time:
|
||||
self.spot_list.append(spot)
|
||||
self.last_spot_time = max(map(lambda s: s.time, spots))
|
||||
|
||||
# Stop any threads and prepare for application shutdown
|
||||
def stop(self):
|
||||
raise NotImplementedError("Subclasses must implement this method")
|
||||
Reference in New Issue
Block a user