Repository navigation
Expand file tree
/
Copy pathNWS.py
More file actions
177 lines (138 loc) · 6.23 KB
/
Copy pathNWS.py
File metadata and controls
177 lines (138 loc) · 6.23 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import json
import logging
import re
import requests
from util import (
Translator,
convert_time,
headline_to_gmt_minus_4,
load_parsed_data,
render_template,
save_parsed_data,
)
logger = logging.getLogger('NWS')
def fetch_nws_data():
logger.debug("fetching NWS data")
# TODO consolidate all NWS specific behavior in one place, probably NWS.py
###NWS PRODUCTS
apiurl = "https://api.weather.gov/alerts/active/area/PR"
headers = {"accept": "application/geo+json" ,
"User-Agent": "El Vocero / AP / Knight Lab Weatherbot"
}
response = requests.get(apiurl, headers=headers)
#add url for product nhc
if response.ok:
return response.json()
else:
logger.warning(f"NWS API request NOT OK: {response.status_code}")
return None
def get_weather_bulletin(bulletin, test_mode) -> list:
PARSED_ID_FILE = 'NWSdata.json'
translate = Translator()
data = []
relevant_stories= { # if there's an image that should be posted for a given story type, include the code here.
'flash flood warning': 'aviso_de_inundaciones',
'flood warning': None,
'flash flood watch': 'vigilancia_de_inundaciones',
'flood advisory': None,
'special weather statement': 'reporte_especial',
'hurricane local statement': 'reporte_especial_de_huracan',
'heat advisory': 'calor_extremo',
'heat warning': 'calor_extremo',
'rip current statement': None
}
#pub date , end time , event , areas affected, headline , what , where , impacts
parsed_ids=load_parsed_data(PARSED_ID_FILE)
with open(bulletin) as f :
bulletin = json.load(f)
if bulletin['features'] == []:
logger.debug("No weather alerts")
return []
for feature in bulletin['features']:
event = feature['properties']['event'].casefold()
eventid=feature["id"]
if eventid in parsed_ids and not test_mode:
logger.debug(f"Skipping '{event}' -- already processed. [id: {eventid}]")
continue
if not event in relevant_stories :
logger.debug(f"Skipping '{event}' -- not relevant")
continue
parsed_ids[eventid]=True
eventdict = {}
eventdict['event'] = translate(event)
eventdict['image_code'] = relevant_stories[event]
eventdict['@id'] = eventid
refs= []
for ref in feature['properties']['references']:
refs.append({'@id': ref['@id'], 'sent': ref['sent']})
eventdict['references'] = refs
#print(refs)
areas = feature['properties']['areaDesc'].split(";")
areas = [translate(area.split(",")[0].strip()) for area in areas]
eventdict['areas_affected'] = format_list_strings(areas)
if len(areas) == 1:
eventdict['onearea'] = True
#print('areas affected', eventdict['areas_affected'])
#print('Areas affected:', eventdict['areas_affected'])
eventdict['sent'] =convert_time(feature['properties']['sent'] , format="NWS")
eventdict['effective'] = convert_time(feature['properties']['effective'] , format="NWS")
eventdict['expires'] = convert_time(feature['properties']['expires'] , format="NWS")
headline =headline_to_gmt_minus_4(feature['properties']['headline'], eventdict['effective'], eventdict['expires'])
eventdict['headline'] = translate(headline)
description = feature['properties']['description']
#print('Headline:', headline)
eventdict['description'] = translate(description)
description_dict = process_description(description, translate)
if len(description_dict) > 0:
eventdict.update(description_dict)
else:
logger.warning(f"process_description() came up empty for {eventid}")
eventdict['use_description'] = True
data.append(eventdict)
logger.info(f"Added [{event}] [{eventid}] to data")
if type(parsed_ids) == dict and not test_mode:
save_parsed_data(parsed_ids, PARSED_ID_FILE)
else:
logger.warning(f"save_parsed_data got non-dict (or test mode), so not saving {parsed_ids}")
return data
DESCRIPTION_EXTRACTORS = [
(re.compile(r"^\* WHAT"), 'what', lambda line: line.split("...")[1]),
(re.compile(r"^\* WHERE"), 'where', lambda line: line.split("...")[1]),
(re.compile(r"^\* WHEN"), 'when', lambda line: line.split("...")[1]),
(re.compile(r"^\* IMPACTS"), 'impacts', lambda line: line.split("...")[1]),
(re.compile(r"^\* RAIN"), 'rain', lambda line: line.split("...")[1]),
(re.compile(r"^\* WIND"), 'wind', lambda line: line.split("...")[1]),
(re.compile(r"^\* STORM SURGE"), 'storm_surge', lambda line: line.split("...")[1]),
]
def process_description(description: str, translate: callable) -> dict:
d = {}
lines = description.split("\n\n")
for line in lines:
for pat, key, func in DESCRIPTION_EXTRACTORS:
if pat.match(line):
d[key] = translate(func(line))
return d
def format_list_strings(strings):
if len(strings) == 0:
return ""
elif len(strings) == 1:
return strings[0]
else:
last_string = strings[-1]
other_strings = ", ".join(strings[:-1])
return f"{other_strings}, y {last_string}"
# Given a bulletin from the NWS, see if stories should be written
# for each event that needs a story, generate the content
# return a list of story content for dispatch elsewhere
def generate_nws_stories(bulletin, test_mode=False) :
data = get_weather_bulletin(bulletin, test_mode)
generated_stories=[]
for event in data:
new_story = render_template('story_templates/NWS.html', data=event)
generated_stories.append({
"content" : new_story,
"headline": event["headline"],
'image_code': event.get('image_code'),
'event_id': event.get('@id', 'event ID missing')
})
return generated_stories