about summary refs log tree commit diff
path: root/weather.py
blob: 12ae7d6c61aeb3f6ace441ad4648e19a1dacb681 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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())