This repository has been archived by the owner on Nov 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.py
executable file
·208 lines (165 loc) · 6.36 KB
/
weather.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#! /usr/local/bin/python3
import datetime
import json
import requests
from urllib import parse as urlparse
from collections import namedtuple
import os
from prefs import prefs, keys
import misc
from formatter import extformat
# dict of runtime caches, to avoid double requests
cache = {}
def api_url(endpoint, location=prefs['weather']['location']):
return ('https://api.wunderground.com/api/'
+ keys['wunderground']
+ f'/{endpoint}/q/{location}.json')
def weather(endpoint, location=prefs['weather']['location']):
global cache
ret, cache = misc.request_json(api_url(endpoint, location), cache)
return ret
def hours(length=24, location=prefs['weather']['location']):
moments = []
forecast = weather('hourly', location)
Weather = namedtuple('Weather', ['temp', 'precip', 'time'])
for i, hour in enumerate(forecast['hourly_forecast']):
if i >= length:
break
moments.append(Weather(
temp=float(hour['temp']['english']),
precip=int(hour['pop']),
time=str(int(hour['FCTTIME']['hour']) % 12 + 1)
+ hour['FCTTIME']['ampm']
))
return moments
def graph(location=prefs['weather']['location']):
width = prefs['width']
height = prefs['weather']['height']
margin = 3
inner_width = width - 2 * margin
moments = hours(length=inner_width, location=location)
if len(moments) == 0:
return ''
def limit(seq, fn, key):
return getattr(fn(seq, key=lambda x: getattr(x, key)), key)
def limits(seq, key):
return (limit(seq, min, key),
limit(seq, max, key))
(temp_max, temp_min) = limits(moments, 'temp')
(precip_max, precip_min) = limits(moments, 'precip')
def place(val, x, y, align='left'):
nonlocal graph
field = graph
if y > len(field) or x > len(field[y]):
return
if align is 'left':
field[y] = field[y][0:x] + val + field[y][x + len(val):]
elif align is 'right':
field[y] = field[y][0:x - len(val)] + val + field[y][x:]
step = int((inner_width) / len(moments))
time_rows = 1
# time_rows = 3 * step
graph = [' ' * width for x in range(height + time_rows)]
chars = prefs['weather']['chars']
place('°F' + chars['temp'], 0, height)
place(chars['precip'] + '%p', width - margin, height)
for y in range(height):
# temp axis marker
place(str(int(misc.lerp(temp_min, temp_max, y / (height - 1)))),
0, y)
# left rule
place(prefs['vert'], margin - 1, y)
# precip marker
place(str(int(misc.lerp(precip_min, precip_max, y / (height - 1)))),
width, y, align='right')
# right rule
place(prefs['vert'], width - margin, y)
for i, moment in enumerate(moments):
# odd = i % time_rows
i_orig = i
i = int(i * step + margin)
time_num = int(moment.time[:-2])
if time_num % 3 == 0:
# write time at 3 6 9 12
place(str(time_num), i, height)
# vrule at 12am/pm
if time_num % 12 == 0:
for j in range(height):
place(prefs['vert_light'], i, j)
temp_y = int(misc.scale(
moment.temp, temp_min, temp_max, 0, height - 1))
place(chars['temp'], i, temp_y)
precip_y = int(misc.scale(
moment.precip, precip_min, precip_max, 0, height - 1))
if graph[precip_y][i] is chars['temp']:
place(chars['both'], i, precip_y)
else:
place(chars['precip'], i, precip_y)
return '\n'.join(graph)
def day_forecast(day=0, location=prefs['weather']['location']):
forecast = weather('forecast', location)['forecast']
day_data = forecast['simpleforecast']['forecastday'][day]
txt_data = forecast['txt_forecast']['forecastday'][day]
return extformat(prefs['weather']['forecast_format'],
forecast=forecast,
day_data=day_data,
txt_data=txt_data,
high =int(day_data['high'][prefs['weather']['temp']]),
low =int(day_data['low'][prefs['weather']['temp']]),
precip =int(day_data['pop']),
conds =day_data['conditions'],
summary =txt_data['fcttext'],
)
def conditions(day=0, location=prefs['weather']['location']):
return (weather('forecast', location)
['forecast']
['simpleforecast']
['forecastday']
[day]
['conditions'])
def tomorrow_conditions(location=prefs['weather']['location']):
return conditions(day=1, location=location)
def today_forecast(day=0, location=prefs['weather']['location']):
return day_forecast(day=day, location=location)
def tomorrow_forecast(day=1, location=prefs['weather']['location']):
return day_forecast(day=day, location=location)
def parsesuntime(t):
return datetime.datetime.strptime(
t['hour'].rjust(2, '0') + t['minute'], '%H%M')
def sunrise(location=prefs['weather']['location']):
times = weather('astronomy', location)['sun_phase']['sunrise']
return parsesuntime(times)
def sunset(location=prefs['weather']['location']):
times = weather('astronomy', location)['sun_phase']['sunset']
return parsesuntime(times)
def suntimes(location=prefs['weather']['location']):
risetime, settime = sunrise(location), sunset(location)
daylight = settime - risetime
risefmt = extformat(
prefs['weather']['sun']['rise_format'],
sunrise=misc.hoursminutes(risetime, fillchar=''))
setfmt = extformat(
prefs['weather']['sun']['set_format'],
sunset=misc.hoursminutes(settime, fillchar=''))
dayfmt = extformat(
prefs['weather']['sun']['daylight_format'],
daylight=misc.formatdelta(daylight))
return extformat(
prefs['weather']['sun']['format'],
sunrise =risefmt,
sunset =setfmt,
daylight=dayfmt,)
def moon(location=prefs['weather']['location']):
times = weather('astronomy', location)['moon_phase']
phase = times['phaseofMoon'].lower()
percent = times['percentIlluminated']
graphic = (prefs['weather']['moon']['bright_graphic']
if int(times['percentIlluminated']) > 50
else prefs['weather']['moon']['dark_graphic'])
return extformat(prefs['weather']['moon']['format'],
**locals())
def main():
print(today_forecast())
print(graph())
if __name__ == '__main__':
main()