about summary refs log tree commit diff
path: root/weather.py
diff options
context:
space:
mode:
Diffstat (limited to 'weather.py')
-rwxr-xr-xweather.py48
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())