-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweather_classes.py
82 lines (67 loc) · 2.62 KB
/
weather_classes.py
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
import requests
alerts = {"HUR" : "Hurricane Local Statement",
"TOR": "Tornado Warning",
"TOW": "Tornado Watch",
"WRN": "Severe Thunderstorm Warning",
"SEW": "Severe Thunderstorm Watch",
"WIN": "Winter Weather Advisory",
"FLO": "Flood Warning",
"WAT": "Flood Watch / Statement",
"WND": "High Wind Advisory",
"SVR": "Severe Weather Statement",
"HEA": "Heat Advisory",
"FOG": "Dense Fog Advisory",
"SPE": "Special Weather Statement",
"FIR": "Fire Weather Advisory",
"VOL": "Volcanic Activity Statement",
"HWW": "Hurricane Wind Warning",
"REC": "Record Set",
"REP": "Public Reports",
"PUB": "Public Information Statement",
}
class ZipCode():
def __init__(self, zip_code):
self.zip_code = zip_code
self.weather_info = False
self.current_conditions = False
self.forecast = False
self.sunrise = False
self.sunset = False
self.weather_alert = False
self.hurricane_alert = False
self.location = False
def get_base_url(self):
return 'http://api.wunderground.com/api/785718799666cf26'
def retrieve_weather_info(self):
if not self.weather_info:
url = self.get_base_url() + '/conditions/forecast10day/astronomy/alerts/currenthurricane/q/{}.json'.format(self.zip_code)
weather_info = requests.get(url)
self.weather_info = weather_info.json()
return self.weather_info
def check_current_conditions(self):
if not self.current_conditions:
self.current_conditions = self.retrieve_weather_info()['current_observation']['temp_f']
return self.current_conditions
def check_forecast(self):
if not self.forecast:
self.forecast = self.retrieve_weather_info()['forecast']['simpleforecast']['forecastday']
return self.forecast
def check_sunrise_sunset(self):
if not self.sunrise:
self.sunrise = self.weather_info['sun_phase']['sunrise']
if not self.sunset:
self.sunset = self.weather_info['sun_phase']['sunset']
return self.sunrise, self.sunset
def check_weather_alerts(self):
try:
if not self.weather_alert:
self.weather_alert = self.weather_info['alerts'][0]['type']
except:
pass
return self.weather_alert
def check_for_hurricanes(self):
if not self.hurricane_alert:
self.hurricane_alert = self.weather_info['currenthurricane']
if not self.hurricane_alert:
print("No hurricanes to report...FOR NOW.")
return self.hurricane_alert