-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather_main.py
106 lines (88 loc) · 2.7 KB
/
weather_main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# weather_main.py
import urllib2
import json
import requests
class Weather():
def __init__(self, location):
self.location = location
self.city = None
self.state = None
self.master_json = None
self.get_weather()
self.get_location()
def get_weather(self):
url = 'http://api.wunderground.com/api/1a6f295e825c2af1/geolookup/conditions/forecast10day/q/{}.json'.format(self.location)
response = requests.get(url)
response_json = response.json()
self.master_json = response_json
print(url)
def get_location(self):
self.city = self.master_json['location']['city']
self.state = self.master_json['location']['state']
def get_full_location(self):
print '{}, {}'.format(self.city,self.state)
class CurrentConditions(Weather):
def __init__(self, location):
Weather.__init__(self, location)
self.current = None
self.weather = None
self.temp = None
self.temp = None
self.feelslike = None
self.wind = None
self.gust = None
self.get_current_conditions()
self.get_full_location()
self.__str__()
def get_current_conditions(self):
self.current = self.master_json['current_observation']
self.weather = self.current['weather']
self.temp = self.current['temp_f']
self.feelslike = self.current['feelslike_f']
self.wind = self.current['wind_mph']
self.gust = self.current['wind_gust_mph']
self.precip = self.current['precip_today_in']
def __str__(self):
self.get_full_location
print 'Weather: %s' % self.weather
print 'Temp: %sF' % self.temp
print 'Wind: %smph' % self.wind
print 'Gust: %smph' %self.gust
print 'Feels Like: %sF' % self.feelslike
print 'Precip: %s"' % self.precip
class Forecast(Weather):
def __init__(self, location, duration):
Weather.__init__(self, location)
self.duration = duration
self.ten_day = None
self.get_ten_day()
self.__str__()
def get_ten_day(self):
self.ten_day = self.master_json['forecast']['simpleforecast']['forecastday']
def get_date(self, period):
day = self.ten_day[period]
dates = day['date']
date = dates['pretty']
return date
def __str__(self):
for day in range(self.duration):
day_weather = self.ten_day[day]
date = self.get_date(day)
conditions = day_weather['conditions']
high = day_weather['high']['fahrenheit']
low = day_weather['low']['fahrenheit']
snow = day_weather['snow_allday']['in']
snow_day = day_weather['snow_day']['in']
snow_night = day_weather['snow_night']['in']
self.get_full_location
self.forecast_out = {'day':day
,'day_weather':day_weather
,'date':date
,'conditions':conditions
,'high':high
,'low':low
,'snow':snow
,'snow_day':snow_day
,'snow_night':snow_night
}
return self.forecast_out