File size: 774 Bytes
6a82282
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""NWS API: active alerts for NY."""

import json
import sys
import urllib.request

from _runner import cli, write_cache  # noqa: E402

URL = "https://api.weather.gov/alerts/active?area=NY"


def probe():
    req = urllib.request.Request(URL,
        headers={"User-Agent": "riprap-experiments (dev) msrahmanadam@gmail.com",
                 "Accept": "application/geo+json"})
    with urllib.request.urlopen(req, timeout=15) as r:
        d = json.loads(r.read())
    feats = d.get("features", [])
    titles = [f["properties"].get("event") for f in feats[:5]]
    write_cache("nws_alerts", {"n_active": len(feats), "first_5_events": titles})
    return True, f"{len(feats)} active alerts; first 5: {titles}", d


if __name__ == "__main__":
    sys.exit(cli("nws", probe))