-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathmodels.py
132 lines (100 loc) · 3.62 KB
/
models.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
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
from forecastio.utils import UnicodeMixin, PropertyUnavailable
import datetime
import requests
class Forecast(UnicodeMixin):
def __init__(self, data, response, headers):
self.response = response
self.http_headers = headers
self.json = data
self._alerts = []
for alertJSON in self.json.get('alerts', []):
self._alerts.append(Alert(alertJSON))
def update(self):
r = requests.get(self.response.url)
self.json = r.json()
self.response = r
def currently(self):
return self._forcastio_data('currently')
def minutely(self):
return self._forcastio_data('minutely')
def hourly(self):
return self._forcastio_data('hourly')
def daily(self):
return self._forcastio_data('daily')
def timezone(self):
return self.json['timezone']
def offset(self):
return self.json['offset']
def alerts(self):
return self._alerts
def _forcastio_data(self, key):
keys = ['minutely', 'currently', 'hourly', 'daily']
try:
if key not in self.json:
keys.remove(key)
url = "%s&exclude=%s%s" % (self.response.url.split('&')[0],
','.join(keys), ',alerts,flags')
response = requests.get(url).json()
self.json[key] = response[key]
if key == 'currently':
return ForecastioDataPoint(self.json[key])
else:
return ForecastioDataBlock(self.json[key])
except:
if key == 'currently':
return ForecastioDataPoint()
else:
return ForecastioDataBlock()
class ForecastioDataBlock(UnicodeMixin):
def __init__(self, d=None):
d = d or {}
self.summary = d.get('summary')
self.icon = d.get('icon')
self.data = [ForecastioDataPoint(datapoint)
for datapoint in d.get('data', [])]
def __unicode__(self):
return '<ForecastioDataBlock instance: ' \
'%s with %d ForecastioDataPoints>' % (self.summary,
len(self.data),)
class ForecastioDataPoint(UnicodeMixin):
def __init__(self, d={}):
self.d = d
try:
self.time = datetime.datetime.utcfromtimestamp(int(d['time']))
self.utime = d['time']
except:
pass
try:
sr_time = int(d['sunriseTime'])
self.sunriseTime = datetime.datetime.utcfromtimestamp(sr_time)
except:
self.sunriseTime = None
try:
ss_time = int(d['sunsetTime'])
self.sunsetTime = datetime.datetime.utcfromtimestamp(ss_time)
except:
self.sunsetTime = None
def __getattr__(self, name):
try:
return self.d[name]
except KeyError:
raise PropertyUnavailable(
"Property '{}' is not valid"
" or is not available for this forecast".format(name)
)
def __unicode__(self):
return '<ForecastioDataPoint instance: ' \
'%s at %s>' % (self.summary, self.time,)
class Alert(UnicodeMixin):
def __init__(self, json):
self.json = json
def __getattr__(self, name):
try:
return self.json[name]
except KeyError:
raise PropertyUnavailable(
"Property '{}' is not valid"
" or is not available for this forecast".format(name)
)
def __unicode__(self):
return '<Alert instance: {0} at {1}>'.format(self.title, self.time)