-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathforecast.sh
executable file
·333 lines (305 loc) · 13 KB
/
forecast.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
#!/bin/bash
# TODO: Add in sunrise/sunset calcuations
########################################################################
# Setting Defaults
########################################################################
apiKey=""
defaultLocation=""
OpenBox="False"
Conky="False"
Terminal="False"
HTML="False"
degreeCharacter="c"
data=0
lastUpdateTime=0
FeelsLike=0
dynamicUpdates=0
UseIcons="True"
colors="False"
########################################################################
# Reading in rc
########################################################################
ConfigFile="$HOME/.config/weather_sh.rc"
if [ "$1" == "-r" ];then
shift
ConfigFile="$1"
shift
fi
if [ -f "$ConfigFile" ];then
readarray -t line < "$ConfigFile"
apiKey=${line[0]}
defaultLocation=${line[1]}
degreeCharacter=${line[2]}
UseIcons=${line[3]}
temp=${line[4]}
if [ "$temp" = "True" ];then
if [ -f "$HOME/.bashcolors" ];then
source "$HOME/.bashcolors"
colors="True"
else
colors=""
fi
else
colors=""
fi
fi
########################################################################
# Reading in options
########################################################################
while [ $# -gt 0 ]; do
option="$1"
case $option
in
-k) apiKey="$2"
shift
shift ;;
-l) defaultLocation="$2"
shift
shift ;;
-d) dynamicUpdates=1
shift ;;
-t) Terminal="True"
shift ;;
-h) HTML="True"
shift ;;
-o) OpenBox="True"
shift ;;
-y) Conky="True"
shift ;;
-f) degreeCharacter="f"
shift ;;
-n) UseIcons="False"
shift ;;
-p) CachePath="$2"
shift
shift ;;
-c)
if [ -f "$HOME/.bashcolors" ];then
source "$HOME/.bashcolors"
colors="True"
fi
shift ;;
esac
done
if [ -z $apiKey ];then
echo "No API Key specified in rc, script, or command line."
exit
fi
#Is it City ID or a string?
case $defaultLocation in
''|*[!0-9]*) CityID="False" ;;
*) CityID="True" ;;
esac
########################################################################
# Do we need a new datafile? If so, get it.
########################################################################
if [ -z "${CachePath}" ];then
dataPath="/tmp/fore-$defaultLocation.json"
else
dataPath="${CachePath}/fore-$defaultLocation.json"
fi
if [ ! -e $dataPath ];then
touch $dataPath
if [ "$CityID" = "True" ];then
data=$(curl -s "http://api.openweathermap.org/data/2.5/forecast?id=$defaultLocation&units=metric&appid=$apiKey")
else
data=$(curl -s "http://api.openweathermap.org/data/2.5/forecast?q=$defaultLocation&units=metric&appid=$apiKey")
fi
echo $data > $dataPath
else
data=$(cat $dataPath)
fi
check=$(echo "$data" | grep -c -e '"cod":"40')
check2=$(echo "$data" | grep -c -e '"cod":"30')
sum=$(( $check + $check2 ))
if [ $sum -gt 0 ];then
exit 99
fi
lastUpdateTime=$(($(date +%s) -600))
while true; do
lastfileupdate=$(date -r $dataPath +%s)
if [ $(($(date +%s)-$lastfileupdate)) -ge 600 ];then
if [ "$CityID" = "True" ];then
data=$(curl -s "http://api.openweathermap.org/data/2.5/forecast?id=$defaultLocation&units=metric&appid=$apiKey")
else
data=$(curl -s "http://api.openweathermap.org/data/2.5/forecast?q=$defaultLocation&units=metric&appid=$apiKey")
fi
echo $data > $dataPath
else
if [ "$Conky" != "True" ];then
echo "Cache age: $(($(date +%s)-$lastfileupdate)) seconds."
fi
fi
if [ $(($(date +%s)-$lastUpdateTime)) -ge 600 ]; then
lastUpdateTime=$(date +%s)
########################################################################
# Location Data
########################################################################
Station=$(echo $data | jq -r .city.name)
#Lat=$(echo $data | jq -r .coord.lat)
#Long=$(echo $data | jq -r .coord.lon)
#Country=$(echo $data | jq -r .sys.country)
NumEntries=$(echo $data |jq -r .cnt)
let i=0
while [ $i -lt $NumEntries ]; do
# Get the date...unix format
NixDate[$i]=$(echo $data | jq -r .list[$i].dt | tr '\n' ' ')
####################################################################
# Current conditions (and icon)
####################################################################
if [ "$UseIcons" = "True" ];then
icons[$i]=$(echo $data | jq -r .list[$i].weather[] | jq -r .icon | tr '\n' ' ')
iconval=${icons[$i]%?}
case $iconval in
01*) icon[$i]="☀️";;
02*) icon[$i]="🌤";;
03*) icon[$i]="🌥";;
04*) icon[$i]="☁";;
09*) icon[$i]="🌧";;
10*) icon[$i]="🌦";;
11*) icon[$i]="🌩";;
13*) icon[$i]="🌨";;
50*) icon[$i]="🌫";;
esac
else
icon[$i]=""
fi
ShortWeather[$i]=$(echo $data | jq -r .list[$i].weather[] | jq -r .main | tr '\n' ' '| awk '{$1=$1};1' )
LongWeather[$i]=$(echo $data | jq -r .list[$i].weather[] | jq -r .description | sed -E 's/\S+/\u&/g' | tr '\n' ' '| awk '{$1=$1};1' )
Humidity[$i]=$(echo $data | jq -r .list[$i].main.humidity | tr '\n' ' '| awk '{$1=$1};1' )
CloudCover[$i]=$(echo $data | jq -r .list[$i].clouds.all | tr '\n' ' '| awk '{$1=$1};1' )
####################################################################
# Parse Wind Info
####################################################################
WindSpeed[$i]=$(echo $data | jq -r .list[$i].wind.speed | tr '\n' ' ' | awk '{$1=$1};1' )
#Conversion
if [ "$degreeCharacter" = "f" ]; then
WindSpeed[$i]=$(echo "scale=2; ${WindSpeed[$i]}*0.6213712" | bc | xargs printf "%.2f" | awk '{$1=$1};1' )
windunit="mph"
else
windunit="kph"
fi
####################################################################
# Temperature
####################################################################
tempinc[$i]=$(echo $data | jq -r .list[$i].main.temp | tr '\n' ' ')
temperature[$i]=$tempinc[$i]
if [ "$degreeCharacter" = "f" ]; then
temperature[$i]=$(echo "scale=2; 32+1.8*${tempinc[$i]}" | bc)
fi
i=$((i + 1))
done
fi
AsOf=$(date +"%Y-%m-%d %R" -d @$lastfileupdate)
TomorrowDate=$(date -d '+1 day' +"%s")
NowHour=$(date +"%-H")
NowLow=$((NowHour + 1))
NowHigh=$((NowHour - 1))
if [ "$OpenBox" = "False" ];then
if [ "$HTML" = "False" ];then
if [ "$Conky" = "False" ];then
Terminal="True"
fi
fi
fi
if [ "$Conky" = "True" ]; then
if [ "$colors" = "True" ]; then
let i=0
bob=""
while [ $i -lt 5 ]; do
CastDate=$(date +"%s" -d @${NixDate[$i]})
if [ $CastDate -le $TomorrowDate ]; then
ShortDate=$(date +"%R" -d @${NixDate[$i]})
bob=$(printf "%s %-4s%-2s %-4s |" "$bob" "$ShortDate:" "${ShortWeather[$i]}" "${temperature[$i]}°${degreeCharacter^^}")
fi
i=$((i + 1))
done
else
let i=0
bob=""
while [ $i -lt 5 ]; do
CastDate=$(date +"%s" -d @${NixDate[$i]})
if [ $CastDate -le $TomorrowDate ]; then
ShortDate=$(date +"%R" -d @${NixDate[$i]})
bob=$(printf "%s %-5s %-6s %-4s |" "$bob" "$ShortDate:" "${ShortWeather[$i]}" "${temperature[$i]}°${degreeCharacter^^}")
fi
i=$((i + 1))
done
fi
#bob=$(echo "$icon $ShortWeather $temperature°${degreeCharacter^^}")
#bob
echo "$bob"
fi
if [ "$Terminal" = "True" ];then
if [ "$colors" = "True" ]; then
echo "Forecast for $Station as of: ${YELLOW}$AsOf${RESTORE} "
else
echo "Forecast for $Station as of: $AsOf "
fi
let i=0
while [ $i -lt 40 ]; do
CastDate=$(date +"%s" -d @${NixDate[$i]})
if [ $CastDate -le $TomorrowDate ]; then
ShortDate=$(date +"%m/%d@%R" -d @${NixDate[$i]})
if [ "$colors" = "True" ]; then
printf "${YELLOW}%-11s${RESTORE}: ${CYAN}%-2s%-16s${RESTORE} Temp:${CYAN}%-6s${RESTORE} Wind:${MAGENTA}%-6s${RESTORE} Humidity:${GREEN}%-4s${RESTORE} Clouds:${GREEN}%-4s${RESTORE}\n" "$ShortDate" "${icon[$i]} " "${LongWeather[$i]}" "${temperature[$i]}°${degreeCharacter^^}" "${WindSpeed[$i]}$windunit" "${Humidity[$i]}%" "${CloudCover[$i]}%"
else
printf "%-12s %-2s%-20s %-15s %-14s %-14s %-14s\n" "$ShortDate:" "${icon[$i]} " "${LongWeather[$i]}" "Temp:${temperature[$i]}°${degreeCharacter^^}" "Wind:${WindSpeed[$i]}$windunit" "Humidity:${Humidity[$i]}%" "Cloud Cover:${CloudCover[$i]}%"
fi
else
CastHour=$(date +"%-H" -d @${NixDate[$i]})
if [ "$CastHour" -ge "$NowHigh" ] && [ "$CastHour" -le "$NowLow" ]; then
ShortDate=$(date +"%m/%d@%R" -d @${NixDate[$i]})
if [ "$colors" = "True" ]; then
printf "${RED}%-11s${RESTORE}: ${CYAN}%-2s%-16s${RESTORE} Temp:${CYAN}%-6s${RESTORE} Wind:${MAGENTA}%-6s${RESTORE} Humidity:${GREEN}%-4s${RESTORE} Clouds:${GREEN}%-4s${RESTORE}\n" "$ShortDate" "${icon[$i]} " "${LongWeather[$i]}" "${temperature[$i]}°${degreeCharacter^^}" "${WindSpeed[$i]}$windunit" "${Humidity[$i]}%" "${CloudCover[$i]}%"
else
printf "%-12s %-2s%-20s %-15s %-14s %-14s %-14s\n" "$ShortDate:" "${icon[$i]} " "${LongWeather[$i]}" "Temp:${temperature[$i]}°${degreeCharacter^^}" "Wind:${WindSpeed[$i]}$windunit" "Humidity:${Humidity[$i]}%" "Cloud Cover:${CloudCover[$i]}%"
fi
fi
fi
i=$((i + 1))
done
fi
if [ "$OpenBox" = "True" ]; then
echo '<openbox_pipe_menu>'
echo '<separator label="Forecast" />'
printf '<item label="Forecast for %s as of %s" />\n' "$Station" "$AsOf"
let i=0
while [ $i -lt 40 ]; do
CastDate=$(date +"%s" -d @${NixDate[$i]})
if [ $CastDate -le $TomorrowDate ]; then
ShortDate=$(date +"%m/%d@%R" -d @${NixDate[$i]})
printf '<item label="%-12s %-2s%-20s %-15s %-14s %-14s %-14s/>\n' "$ShortDate:" "${icon[$i]} " "${LongWeather[$i]}" "Temp:${temperature[$i]}°${degreeCharacter^^}" "Wind:${WindSpeed[$i]}$windunit" "Humidity:${Humidity[$i]}%" "Cloud Cover:${CloudCover[$i]}%"
else
CastHour=$(date +"%-H" -d @${NixDate[$i]})
if [ "$CastHour" -ge "$NowHigh" ] && [ "$CastHour" -le "$NowLow" ]; then
ShortDate=$(date +"%m/%d@%R" -d @${NixDate[$i]})
printf '<item label="%-12s %-2s%-20s %-15s %-14s %-14s %-14s/>\n' "$ShortDate:" "${icon[$i]} " "${LongWeather[$i]}" "Temp:${temperature[$i]}°${degreeCharacter^^}" "Wind:${WindSpeed[$i]}$windunit" "Humidity:${Humidity[$i]}%" "Cloud Cover:${CloudCover[$i]}%"
fi
fi
i=$((i + 1))
done
echo '</openbox_pipe_menu>'
fi
if [ "$HTML" = "True" ];then
echo "Forecast for $Station as of: $AsOf <br />"
let i=0
while [ $i -lt 40 ]; do
CastDate=$(date +"%s" -d @${NixDate[$i]})
if [ $CastDate -le $TomorrowDate ]; then
ShortDate=$(date +"%m/%d@%R" -d @${NixDate[$i]})
printf "%-12s %-2s%-20s %-15s %-14s %-14s %-14s<br />\n" "$ShortDate:" "${icon[$i]} " "${LongWeather[$i]}" "Temp:${temperature[$i]}°${degreeCharacter^^}" "Wind:${WindSpeed[$i]}$windunit" "Humidity:${Humidity[$i]}%" "Cloud Cover:${CloudCover[$i]}%"
else
CastHour=$(date +"%-H" -d @${NixDate[$i]})
if [ $CastHour -ge $NowHigh ] && [ $CastHour -le $NowLow ]; then
ShortDate=$(date +"%m/%d@%R" -d @${NixDate[$i]})
printf "%-12s %-2s%-20s %-15s %-14s %-14s %-14s<br />\n" "$ShortDate:" "${icon[$i]} " "${LongWeather[$i]}" "Temp:${temperature[$i]}°${degreeCharacter^^}" "Wind:${WindSpeed[$i]}$windunit" "Humidity:${Humidity[$i]}%" "Cloud Cover:${CloudCover[$i]}%"
fi
fi
i=$((i + 1))
done
fi
if [ $dynamicUpdates -eq 0 ];then
break
fi
done