-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGovWeatherAlertInterface.py
More file actions
49 lines (40 loc) · 1.98 KB
/
Copy pathGovWeatherAlertInterface.py
File metadata and controls
49 lines (40 loc) · 1.98 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
from typing import List, Dict
from NetworkInterface import NetworkInterface
class GovWeatherAlertInterface:
def __init__(self, networkInterface: NetworkInterface, states: List[str], zones: List[str]) -> None:
self.networkInterface = networkInterface
self.states: List[str] = states
self.zones: List[str] = zones
def __constructMessage(self, headline: str, desc: str, instruction: str) -> str:
result: str = ''
if headline != None:
result += headline + "\n"
if desc != None:
result += desc + "\n"
if instruction != None:
result += instruction + "\n"
return result
def __apiCall(self, url: str) -> Dict:
results: Dict = {}
r = self.networkInterface.get(url)
if 'features' in r:
for feature in r['features']:
if 'properties' in feature and 'id' in feature:
properties = feature['properties']
message: str = self.__constructMessage(
properties['headline'], properties['description'], properties['instruction'])
if message:
results[feature['id']] = message
return results
def __processState(self, state: str) -> Dict:
return self.__apiCall('https://api.weather.gov/alerts/active?area='+state)
def __processStates(self) -> Dict:
return {k: v for d in [self.__processState(state) for state in self.states] for k, v in d.items()}
def __processZone(self, zone: str) -> Dict:
return self.__apiCall('https://api.weather.gov/alerts/active/zone/'+zone)
def __processZones(self) -> Dict:
return {k: v for d in [self.__processZone(zone) for zone in self.zones] for k, v in d.items()}
def getEvents(self) -> Dict:
# requires python 3.5+
return {**self.__processStates(), **self.__processZones()}
# return self.__processStates() | self.__processZones() #requires python 3.9+