diff options
author | Starfall <us@starfall.systems> | 2022-11-29 13:06:12 -0600 |
---|---|---|
committer | Starfall <us@starfall.systems> | 2022-11-29 13:06:12 -0600 |
commit | aa254b490025020343629f02f04ac211f6cfd904 (patch) | |
tree | 8feb10f001a14d9baad74e1d8a41f681e6629711 |
40 minute proof of concept
-rwxr-xr-x | weather.py | 48 |
1 files changed, 48 insertions, 0 deletions
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()) |