-
Notifications
You must be signed in to change notification settings - Fork 1
/
weather_wttr.sh
359 lines (314 loc) · 12.1 KB
/
weather_wttr.sh
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
#!/usr/bin/env bash
trap 'rm -rf "${WORKDIR}"' EXIT
[[ -z "${WORKDIR}" || "${WORKDIR}" != "/tmp/"* || ! -d "${WORKDIR}" ]] && WORKDIR="$(mktemp -d)"
[[ -z "${CURRENT_DIR}" || ! -d "${CURRENT_DIR}" ]] && CURRENT_DIR=$(pwd)
# Load custom functions
if type 'colorEcho' 2>/dev/null | grep -q 'function'; then
:
else
if [[ -s "${MY_SHELL_SCRIPTS:-$HOME/.dotfiles}/custom_functions.sh" ]]; then
source "${MY_SHELL_SCRIPTS:-$HOME/.dotfiles}/custom_functions.sh"
else
echo "${MY_SHELL_SCRIPTS:-$HOME/.dotfiles}/custom_functions.sh does not exist!"
exit 0
fi
fi
[[ -z "${CURL_CHECK_OPTS[*]}" ]] && Get_Installer_CURL_Options
[[ -z "${AXEL_DOWNLOAD_OPTS[*]}" ]] && Get_Installer_AXEL_Options
# https://github.com/chubin/wttr.in/blob/master/lib/constants.py
declare -A WWO_CODE=(
["113"]="Sunny"
["116"]="PartlyCloudy"
["119"]="Cloudy"
["122"]="VeryCloudy"
["143"]="Fog"
["176"]="LightShowers"
["179"]="LightSleetShowers"
["182"]="LightSleet"
["185"]="LightSleet"
["200"]="ThunderyShowers"
["227"]="LightSnow"
["230"]="HeavySnow"
["248"]="Fog"
["260"]="Fog"
["263"]="LightShowers"
["266"]="LightRain"
["281"]="LightSleet"
["284"]="LightSleet"
["293"]="LightRain"
["296"]="LightRain"
["299"]="HeavyShowers"
["302"]="HeavyRain"
["305"]="HeavyShowers"
["308"]="HeavyRain"
["311"]="LightSleet"
["314"]="LightSleet"
["317"]="LightSleet"
["320"]="LightSnow"
["323"]="LightSnowShowers"
["326"]="LightSnowShowers"
["329"]="HeavySnow"
["332"]="HeavySnow"
["335"]="HeavySnowShowers"
["338"]="HeavySnow"
["350"]="LightSleet"
["353"]="LightShowers"
["356"]="HeavyShowers"
["359"]="HeavyRain"
["362"]="LightSleetShowers"
["365"]="LightSleetShowers"
["368"]="LightSnowShowers"
["371"]="HeavySnowShowers"
["374"]="LightSleetShowers"
["377"]="LightSleet"
["386"]="ThunderyShowers"
["389"]="ThunderyHeavyRain"
["392"]="ThunderySnowShowers"
["395"]="HeavySnowShowers"
)
declare -A WEATHER_SYMBOL=(
["Unknown"]="✨"
["Cloudy"]="☁️"
["Fog"]="🌫"
["HeavyRain"]="🌧"
["HeavyShowers"]="🌧"
["HeavySnow"]="❄️"
["HeavySnowShowers"]="❄️"
["LightRain"]="🌦"
["LightShowers"]="🌦"
["LightSleet"]="🌧"
["LightSleetShowers"]="🌧"
["LightSnow"]="🌨"
["LightSnowShowers"]="🌨"
["PartlyCloudy"]="⛅️"
["Sunny"]="☀️"
["ThunderyHeavyRain"]="🌩"
["ThunderyShowers"]="⛈"
["ThunderySnowShowers"]="⛈"
["VeryCloudy"]="☁️"
)
WIND_DIRECTION=("↓" "↙" "←" "↖" "↑" "↗" "→" "↘")
DATE_WEEKDAY=("星期日" "星期一" "星期二" "星期三" "星期四" "星期五" "星期六")
WEATHER_CITY=${1:-""}
WEATHER_PNG=${2:-"$HOME/.config/conky/hybrid/weather.png"}
WEATHER_MINI_PNG=${3:-"$HOME/.config/conky/hybrid/weather_mini.png"}
WEATHER_JSON="${WORKDIR}/weather.json"
WEATHER_HTML="${WORKDIR}/weather_wttr.html"
WEATHER_HTML_PNG="${WORKDIR}/weather_wttr.png"
if [[ -z "${WEATHER_CITY}" ]]; then
get_network_wan_geo_city
[[ -n "${NETWORK_WAN_NET_IP_CITY}" ]] && WEATHER_CITY="${NETWORK_WAN_NET_IP_CITY}"
fi
colorEcho "${BLUE}Getting ${FUCHSIA}weather ${ORANGE}JSON${BLUE} from ${FUCHSIA}wttr.in${BLUE}..."
curl "${CURL_DOWNLOAD_OPTS[@]}" \
--noproxy '*' -H "Accept-Language: zh-cn" --compressed \
"wttr.in/${WEATHER_CITY}?format=j1" \
-o "${WEATHER_JSON}"
curl_rtn_code=$?
if [[ ${curl_rtn_code} -gt 0 ]]; then
colorEcho "${RED}Error occurred while getting ${FUCHSIA}weather JSON data from ${FUCHSIA}wttr.in${RED}!"
exit 1
fi
colorEcho "${BLUE}Getting weather from ${FUCHSIA}wttr.in${BLUE}..."
curl "${CURL_DOWNLOAD_OPTS[@]}" \
--noproxy '*' -H "Accept-Language: zh-cn" --compressed \
"wttr.in/${WEATHER_CITY}.png" \
-o "${WORKDIR}/weather.png"
curl_rtn_code=$?
if [[ ${curl_rtn_code} -eq 0 ]]; then
convert -transparent black "${WORKDIR}/weather.png" "${WEATHER_PNG}"
else
colorEcho "${RED}Error occurred while getting ${FUCHSIA}weather PNG data from ${FUCHSIA}wttr.in${RED}!"
exit 1
fi
# curl "${CURL_DOWNLOAD_OPTS[@]}" \
# --noproxy '*' -H "Accept-Language: zh-cn" --compressed \
# "wttr.in/_Qtp_lang=zh_cn.png" \
# -o "${WORKDIR}/weather_mini.png" && \
# | convert -transparent black "${WORKDIR}/weather_mini.png" "${WEATHER_MINI_PNG}"
if [[ ! -s "${WEATHER_JSON}" ]]; then
colorEcho "${RED}Error occurred while getting ${FUCHSIA}weather data from ${FUCHSIA}wttr.in${RED}!"
exit 1
fi
colorEcho "${BLUE}Parsing ${ORANGE}JSON${BLUE} to ${FUCHSIA}HTML${BLUE}..."
tee "${WEATHER_HTML}" >/dev/null <<-'EOF'
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<title>天气预报</title>
<style type="text/css">
body {
background: black;
color: #bbbbbb;
}
table {
border-collapse: collapse;
border: 1px dashed rgb(200, 200, 200);
letter-spacing: 1px;
font-size: 0.8rem;
}
td,
th {
border: 1px dashed rgb(190, 190, 190);
padding: 2px 2px;
}
td {
text-align: center;
}
.ef118 { color: #87ff00; }
.ef220 { color: #ffd700; }
.ef226 { color: #ffff00; }
.weekday { color: #ab47bc; }
.date { color: #42a5f5; }
.colwidth { width: 90px; }
.bordersolid { border: 1px solid rgb(200, 200, 200); }
.fontbold { font-weight: bold; }
</style>
</head>
<body>
<table id="weather">
<colgroup>
<col class="colwidth" />
</colgroup>
EOF
for ((i=0; i<3; i++)); do
V_DATE=$(jq -r ".weather[$i] | .date" "${WEATHER_JSON}")
V_WEEKDAY=$(date --date="${V_DATE}" +%w)
WEATHER_DATE=$(cut -d"-" -f2- <<<"${V_DATE}")
WEATHER_WEEKDAY="${DATE_WEEKDAY[${V_WEEKDAY}]}"
WEATHER_TEMP_MIN=$(jq -r ".weather[$i] | .mintempC" "${WEATHER_JSON}")
WEATHER_TEMP_MAX=$(jq -r ".weather[$i] | .maxtempC" "${WEATHER_JSON}")
tee -a "${WEATHER_HTML}" >/dev/null <<-EOF
<tr class="bordersolid">
<td colspan="3" class="fontbold">
<span class="date">${WEATHER_DATE}</span>
<span class="weekday">${WEATHER_WEEKDAY}</span>
<span class="ef220">${WEATHER_TEMP_MIN}</span>(<span class="ef220">${WEATHER_TEMP_MAX}</span>)°C
</td>
</tr>
EOF
# 0000 0300 0600 0900 1200 1500 1800 2100
WEATHER_CNT=0
WEATHER_ICON=()
WEATHER_TEMP=()
WEATHER_TEMP_FEEL=()
WEATHER_DESC=()
WEATHER_WIND_DIRECTION=()
WEATHER_WIND_SPEED=()
WEATHER_WIND_GUSTSPEED=()
WEATHER_VISIBILITY=()
WEATHER_RAIN_MM=()
WEATHER_RAIN_CHANCE=()
for j in 2 4 6; do
WEATHER_CNT=$((WEATHER_CNT + 1))
V_CODE=$(jq -r ".weather[$i] | .hourly[$j] | .weatherCode" "${WEATHER_JSON}")
WEATHER_ICON+=("${WEATHER_SYMBOL[${WWO_CODE["${V_CODE}"]}]}")
V_TEMP=$(jq -r ".weather[$i] | .hourly[$j] | .tempC" "${WEATHER_JSON}")
V_TEMP_FEEL=$(jq -r ".weather[$i] | .hourly[$j] | .FeelsLikeC" "${WEATHER_JSON}")
WEATHER_TEMP+=("${V_TEMP}")
WEATHER_TEMP_FEEL+=("${V_TEMP_FEEL}")
V_DESC=$(jq -r ".weather[$i] | .hourly[$j] | .lang_zh[0] | .value" "${WEATHER_JSON}")
WEATHER_DESC+=("${V_DESC}")
V_WIND_DEGREE=$(jq -r ".weather[$i] | .hourly[$j] | .winddirDegree" "${WEATHER_JSON}")
V_WIND_DIRECTION=$(echo "(((${V_WIND_DEGREE}+22.5)%360)/45.0)" | bc)
V_WIND_SPEED=$(jq -r ".weather[$i] | .hourly[$j] | .windspeedKmph" "${WEATHER_JSON}")
V_WIND_GUSTSPEED=$(jq -r ".weather[$i] | .hourly[$j] | .WindGustKmph" "${WEATHER_JSON}")
WEATHER_WIND_DIRECTION+=("${WIND_DIRECTION[${V_WIND_DIRECTION}]}")
WEATHER_WIND_SPEED+=("${V_WIND_SPEED}")
WEATHER_WIND_GUSTSPEED+=("${V_WIND_GUSTSPEED}")
V_VISIBILITY=$(jq -r ".weather[$i] | .hourly[$j] | .visibility" "${WEATHER_JSON}")
WEATHER_VISIBILITY+=("${V_VISIBILITY}")
V_RAIN_MM=$(jq -r ".weather[$i] | .hourly[$j] | .precipMM" "${WEATHER_JSON}")
V_RAIN_CHANCE=$(jq -r ".weather[$i] | .hourly[$j] | .chanceofrain" "${WEATHER_JSON}")
WEATHER_RAIN_MM+=("${V_RAIN_MM}")
WEATHER_RAIN_CHANCE+=("${V_RAIN_CHANCE}")
done
tee -a "${WEATHER_HTML}" >/dev/null <<-EOF
<tr>
<td>${WEATHER_ICON[0]}<span class="ef220">${WEATHER_TEMP[0]}</span>(<span class="ef220">${WEATHER_TEMP_FEEL[0]}</span>)</td>
<td>${WEATHER_ICON[1]}<span class="ef220">${WEATHER_TEMP[1]}</span>(<span class="ef220">${WEATHER_TEMP_FEEL[1]}</span>)</td>
<td>${WEATHER_ICON[2]}<span class="ef220">${WEATHER_TEMP[2]}</span>(<span class="ef220">${WEATHER_TEMP_FEEL[2]}</span>)</td>
</tr>
<tr>
<td>${WEATHER_DESC[0]}</td>
<td>${WEATHER_DESC[1]}</td>
<td>${WEATHER_DESC[2]}</td>
</tr>
<tr>
<td><span class="ef118">${WEATHER_WIND_DIRECTION[0]}</span><span class="ef226">${WEATHER_WIND_SPEED[0]}</span>-<span class="ef220">${WEATHER_WIND_GUSTSPEED[0]}</span>km/h</td>
<td><span class="ef118">${WEATHER_WIND_DIRECTION[1]}</span><span class="ef226">${WEATHER_WIND_SPEED[1]}</span>-<span class="ef220">${WEATHER_WIND_GUSTSPEED[1]}</span>km/h</td>
<td><span class="ef118">${WEATHER_WIND_DIRECTION[2]}</span><span class="ef226">${WEATHER_WIND_SPEED[2]}</span>-<span class="ef220">${WEATHER_WIND_GUSTSPEED[2]}</span>km/h</td>
</tr>
<tr>
<td>${WEATHER_VISIBILITY[0]}km</td>
<td>${WEATHER_VISIBILITY[1]}km</td>
<td>${WEATHER_VISIBILITY[2]}km</td>
</tr>
<tr>
<td>${WEATHER_RAIN_MM[0]}mm ${WEATHER_RAIN_CHANCE[0]}%</td>
<td>${WEATHER_RAIN_MM[1]}mm ${WEATHER_RAIN_CHANCE[1]}%</td>
<td>${WEATHER_RAIN_MM[2]}mm ${WEATHER_RAIN_CHANCE[2]}%</td>
</tr>
EOF
done
tee -a "${WEATHER_HTML}" >/dev/null <<-'EOF'
</table>
</body>
</html>
EOF
colorEcho "${BLUE}Converting ${ORANGE} HTML ${BLUE}to${FUCHSIA} PNG${BLUE}..."
## Print output in table format
# echo -e '05-25\t05-26\t05-27\t\n🌦25(28)\t🌦29(34)\t🌦26(30)' | column -t -s $'\t' --table-empty-lines
## Save output to image file
## Imagemagick
# convert label:"$(ls)" result.png && display result.png
# ls | convert -fill red -background yellow label:@- result.png && display result.png
# convert label:@"scriptfile.sh" result.png && display result.png
## Render Unicode & emoji characters
## convert -list format | grep -i pango
# ls | convert pango:@- result.png && display result.png
## Convert html to image file
# convert pango:@"${WEATHER_HTML}" "${WEATHER_HTML_PNG}" && display "${WEATHER_HTML_PNG}"
## wkhtmltopdf
# yay --noconfirm --needed -S wkhtmltopdf
# wkhtmltopdf "${WEATHER_HTML}" "${WEATHER_HTML_PNG}" && display "${WEATHER_HTML_PNG}"
## Chorme headless
## https://developers.google.com/web/updates/2017/04/headless-chrome
# if [[ -x "/opt/google/chrome/google-chrome" ]]; then
# /opt/google/chrome/google-chrome --headless --disable-gpu --window-size=300,500 \
# --screenshot="${WEATHER_HTML_PNG}" "${WEATHER_HTML}" && \
# convert -transparent black "${WEATHER_HTML_PNG}" "${WEATHER_MINI_PNG}"
# fi
# Nodejs
if [[ ! -x "$(command -v npm)" ]]; then
# nvs
[[ -d "$HOME/.nvs" ]] && export NVS_HOME="$HOME/.nvs" && source "$NVS_HOME/nvs.sh"
# nvm
[[ -d "$HOME/.nvm" ]] && export NVM_DIR="$HOME/.nvm" && source "$NVM_DIR/nvm.sh"
fi
if [[ ! -x "$(command -v npm)" ]]; then
colorEcho "${RED}Please install ${FUCHSIA}nodejs & npm${RED} first!"
cd "${CURRENT_DIR}" && exit 1
fi
if [[ ! -d "${MY_SHELL_SCRIPTS:-$HOME/.dotfiles}/node_modules" ]]; then
colorEcho "${BLUE}Installing ${FUCHSIA}node modules${BLUE}..."
if [[ -x "$(command -v pnpm)" ]]; then
cd "${MY_SHELL_SCRIPTS:-$HOME/.dotfiles}" && pnpm install
elif [[ -x "$(command -v npm)" ]]; then
cd "${MY_SHELL_SCRIPTS:-$HOME/.dotfiles}" && npm install
fi
fi
if [[ ! -d "${MY_SHELL_SCRIPTS:-$HOME/.dotfiles}/node_modules" ]]; then
colorEcho "${FUCHSIA}node_modules${RED} not found, use \`${ORANGE}npm install${RED}\` to install first!"
cd "${CURRENT_DIR}" && exit 1
fi
# Puppeteer
cd "${MY_SHELL_SCRIPTS:-$HOME/.dotfiles}" && \
node ./nodejs/puppeteer_screenshot.js \
--url="file://${WEATHER_HTML}" \
--Element="#weather" \
--Output="${WEATHER_HTML_PNG}" && \
convert -transparent black "${WEATHER_HTML_PNG}" "${WEATHER_MINI_PNG}"
cd "${CURRENT_DIR}" || exit