Commit b1a2dc0a by Demid Merzlyakov

Python script to fetch alerts.

parent a32a7dfc
......@@ -4,6 +4,9 @@
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore
# Python venv
/scripts/NWSAlertsHelperScripts/nws
## Build generated
build/
DerivedData/
......@@ -162,7 +165,8 @@ DerivedData
.LSOverride
# Icon must end with two \r
Icon
Icon
# Thumbnails
._*
......
This script gets the list of FIPS codes with active alerts, fetches the alerts through our NWS API for those locations, stores them in AlertFiles. Then fetches alert messages for all those alerts, and stores them into AlertFiles/Details.
\ No newline at end of file
import json
import requests
import os
baseParams = {
# "echoCity": "Rapid City",
"format": "json",
}
directory = "AlertFiles"
directory_details = os.path.join(directory, "Details")
url = "https://sta-nwsalert.onelouder.com/current_events"
from urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(category=InsecureRequestWarning)
# for fips_code in range(2262, 1000000):
# # for fips_code in range(46103, 46104):
# params = dict(baseParams)
# params["weather_id"] = f'{fips_code:06}'
# response = requests.get(url, params=params, verify=False)
# responseDict = json.loads(response.text)
# events = responseDict["events"]["events"]
# if events is not None and len(events) > 0:
# print(f'{fips_code:06}' + " - SAVE")
# with open(os.path.join(directory, str(fips_code) + ".json"), "w") as file:
# json.dump(events, file, indent=4, sort_keys=True)
# for event in events:
# url = event["messageURL"]
# alert_id = url.split("/")[-1]
# print(alert_id)
# response = requests.get(url, verify=False)
# with open(os.path.join(directory_details, alert_id + ".txt"), "w") as details_file:
# details_file.write(response.text)
# else:
# if fips_code % 100 == 0:
# print(f'{fips_code:06} - no')
active_fips_codes = set()
active_alerts_url = "https://api.weather.gov/alerts/active"
response = requests.get(active_alerts_url, verify=False)
responseDict = json.loads(response.text)
features = responseDict["features"]
for feature in features:
properties = feature["properties"]
if properties is None:
continue
geocode = properties["geocode"]
if geocode is None:
continue
fips_codes = geocode["SAME"]
if fips_codes is None or len(fips_codes) == 0:
continue
for fips in fips_codes:
active_fips_codes.add(fips)
print("Active fips codes: ")
print(active_fips_codes)
for fips_code in active_fips_codes:
# for fips_code in range(46103, 46104):
params = dict(baseParams)
params["weather_id"] = fips_code
nwsResponse = requests.get(url, params=params, verify=False)
responseDict = json.loads(nwsResponse.text)
events = responseDict["events"]["events"]
if events is not None and len(events) > 0:
print(fips_code + " - SAVE")
with open(os.path.join(directory, str(fips_code) + ".json"), "w") as file:
json.dump(events, file, indent=4, sort_keys=True)
for event in events:
singleUrl = event["messageURL"]
alert_id = singleUrl.split("/")[-1]
print(alert_id)
singleResponse = requests.get(singleUrl, verify=False)
with open(os.path.join(directory_details, alert_id + ".txt"), "w") as details_file:
details_file.write(singleResponse.text)
\ No newline at end of file
python3 -m venv nws
source nws/bin/activate
pip install -r requirements.txt
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment