mirror of
https://github.com/craigerl/aprsd.git
synced 2024-09-26 07:06:41 -04:00
Hemna
2349024539
This patch adds the openweathermap weather plugin. Also adds a new config option to set the overall units setting from imperial (default) to metric. to change it add the following to the ~/.config/aprsd/aprsd.yaml ... aprsd: units: metric
79 lines
2.2 KiB
Python
79 lines
2.2 KiB
Python
# Utilities for plugins to use
|
|
import json
|
|
import logging
|
|
|
|
import requests
|
|
|
|
LOG = logging.getLogger("APRSD")
|
|
|
|
|
|
def get_aprs_fi(api_key, callsign):
|
|
LOG.debug("Fetch aprs.fi location for '{}'".format(callsign))
|
|
try:
|
|
url = (
|
|
"http://api.aprs.fi/api/get?"
|
|
"&what=loc&apikey={}&format=json"
|
|
"&name={}".format(api_key, callsign)
|
|
)
|
|
response = requests.get(url)
|
|
except Exception:
|
|
raise Exception("Failed to get aprs.fi location")
|
|
else:
|
|
response.raise_for_status()
|
|
return json.loads(response.text)
|
|
|
|
|
|
def get_weather_gov_for_gps(lat, lon):
|
|
LOG.debug("Fetch station at {}, {}".format(lat, lon))
|
|
try:
|
|
url2 = (
|
|
"https://forecast.weather.gov/MapClick.php?lat=%s"
|
|
"&lon=%s&FcstType=json" % (lat, lon)
|
|
)
|
|
LOG.debug("Fetching weather '{}'".format(url2))
|
|
response = requests.get(url2)
|
|
except Exception as e:
|
|
LOG.error(e)
|
|
raise Exception("Failed to get weather")
|
|
else:
|
|
response.raise_for_status()
|
|
return json.loads(response.text)
|
|
|
|
|
|
def get_weather_gov_metar(station):
|
|
LOG.debug("Fetch metar for station '{}'".format(station))
|
|
try:
|
|
url = "https://api.weather.gov/stations/{}/observations/latest".format(
|
|
station,
|
|
)
|
|
response = requests.get(url)
|
|
except Exception:
|
|
raise Exception("Failed to fetch metar")
|
|
else:
|
|
response.raise_for_status()
|
|
return json.loads(response)
|
|
|
|
|
|
def fetch_openweathermap(api_key, lat, lon, units="metric", exclude=None):
|
|
LOG.debug("Fetch openweathermap for {}, {}".format(lat, lon))
|
|
if not exclude:
|
|
exclude = "minutely,hourly,daily,alerts"
|
|
try:
|
|
url = (
|
|
"https://api.openweathermap.org/data/2.5/onecall?"
|
|
"lat={}&lon={}&appid={}&units={}&exclude={}".format(
|
|
lat,
|
|
lon,
|
|
api_key,
|
|
units,
|
|
exclude,
|
|
)
|
|
)
|
|
response = requests.get(url)
|
|
except Exception as e:
|
|
LOG.error(e)
|
|
raise Exception("Failed to get weather")
|
|
else:
|
|
response.raise_for_status()
|
|
return json.loads(response.text)
|