From aa254b490025020343629f02f04ac211f6cfd904 Mon Sep 17 00:00:00 2001 From: Starfall Date: Tue, 29 Nov 2022 13:06:12 -0600 Subject: 40 minute proof of concept --- weather.py | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100755 weather.py (limited to 'weather.py') diff --git a/weather.py b/weather.py new file mode 100755 index 0000000..12ae7d6 --- /dev/null +++ b/weather.py @@ -0,0 +1,48 @@ +#!/usr/bin/env python3 +from argparse import ArgumentParser +import requests + +# doco is at https://www.weather.gov/documentation/services-web-api +# openAPI spec is at https://api.weather.gov/openapi.json + +base_url = 'https://api.weather.gov' +headers = { + 'user-agent': 'Starweather/0.1 (us@starfall.systems)', + 'accept': 'application/ld+json' +} + +def handle_args(): + parser = ArgumentParser() + parser.add_argument('lat', help='latitude') + parser.add_argument('lon', help='longitude') + return parser.parse_args() + +def print_alerts(zone): + alerts = requests.get(base_url + f'/alerts/active/zone/{zone}', + headers = headers).json() + print(alerts.get('title')) + print() + for alert in alerts.get('@graph'): + print(f'- {alert.get("headline")}') + print(f' Severity: {alert.get("severity")}') + print(f' Expires: {alert.get("expires")}') + print(f'{alert.get("description")}') + print() + +def main(): + args = handle_args() + + point = requests.get(base_url + f'/points/{args.lat},{args.lon}', + headers = headers).json() + + zone = point.get('forecastZone').split('/')[-1] + print_alerts(zone) + + forecast_url = point.get('forecast') + # print_forecast(forecast_url) + + # hourly_forecast_url = point.get('forecastHourly') + +if __name__ == '__main__': + import sys + sys.exit(main()) -- cgit