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 | #!/bin/bash
#
# Copyright (c) 2013 Krytarik Raido
#
# Homepage: http://www.tuxgarage.com/
#
# This script queries the current weather conditions and forecast
# using the Yahoo Weather API and prints them.
#
########################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
usage () {
printf "\nUsage:\n"
printf "\t weather [options]\n\n"
printf "Options:\n\n"
printf "\t -m only metric units; if combined with \"-b\", primary units\n"
printf "\t -i only imperial units; if combined with \"-b\", primary units\n"
printf "\t -b also include the respective other units\n"
printf "\t -f also include forecast\n"
printf "\t -e also include extended forecast\n"
printf "\t -c only current conditions\n"
printf "\t -v verbose current conditions\n"
printf "\t -t also include observation time\n"
printf "\t -l also include location\n"
printf "\t -p no coloring\n"
printf "\t -h print this help text\n\n"
}
### Defaults
WOEID="638242" # get location ID by searching on <http://weather.yahoo.com/>
UNITS="c" # "c" = metric units
# "f" = imperial units
UNITSCONV="no" # "yes" = also include the respective other units
FORECASTS="yes" # "yes" = also include forecast
FCSTDAYS="2" # number of forecast days to include (max. 5)
CONDVERB="no" # "yes" = verbose current conditions
OBSTIME="yes" # "yes" = also include observation time
LOCATION="no" # "yes" = also include location
if [ -t 0 ]; then
CLON="\e[1;34m"
CLOFF="\e[0m"
else
CLON="\x0312"
CLOFF="\x03"
fi
### Options
while getopts ":imbcfevtlph" OPT; do
case $OPT in
i)
UNITS="f"
UNITSCONV="no"
;;
m)
UNITS="c"
UNITSCONV="no"
;;
b)
UNITSCONV="yes"
;;
c)
FORECASTS="no"
;;
f)
FORECASTS="yes"
if (( $FCSTDAYS < 2 )); then
FCSTDAYS="2"
fi
;;
e)
FORECASTS="yes"
FCSTDAYS="5"
;;
v)
CONDVERB="yes"
;;
t)
OBSTIME="yes"
;;
l)
LOCATION="yes"
;;
p)
CLON=""
CLOFF=""
;;
h)
usage
exit 0
;;
\?)
echo "Invalid option \"-$OPTARG\"." >&2
exit 1
;;
esac
done
### Get data
getdata () {
FULLDATA=$(wget -t 5 -T 10 -qO - "http://weather.yahooapis.com/forecastrss?w=$WOEID&u=$UNITS" | grep '<yweather:')
if [ -z "$FULLDATA" ]; then
echo "Retrieval of weather data failed." >&2
exit 1
fi
UNITSTEMP="°"$(expr "$FULLDATA" : '.*<yweather:units[^>]* temperature="\([^"]*\)".*')
UNITSSPEED=$(expr "$FULLDATA" : '.*<yweather:units[^>]* speed="\([^"]*\)".*')
if [ "$FORECASTS" = "yes" ]; then
FCSTDATA=$(awk -v fcstdays="$FCSTDAYS" '/<yweather:forecast/ {print; fcstday++} fcstday==fcstdays {exit}' <<< "$FULLDATA")
ORIGIFS="$IFS"
IFS=$'\n'
FCSTWDAY=($(sed -n 's/.* day="\([^"]*\)".*/\1/p' <<< "$FCSTDATA"))
FCSTLOW=($(sed -n 's/.* low="\([^"]*\)".*/\1/p' <<< "$FCSTDATA"))
FCSTHIGH=($(sed -n 's/.* high="\([^"]*\)".*/\1/p' <<< "$FCSTDATA"))
FCSTTEXT=($(sed -n 's/.* text="\([^"]*\)".*/\1/p' <<< "$FCSTDATA"))
IFS="$ORIGIFS"
fi
if [ "$1" != "convert" ]; then
CONDTEXT=$(expr "$FULLDATA" : '.*<yweather:condition[^>]* text="\([^"]*\)".*')
ATMOHUMID=$(expr "$FULLDATA" : '.*<yweather:atmosphere[^>]* humidity="\([^"]*\)".*')"%"
if [ "$OBSTIME" = "yes" ]; then
OBSTMDATA=$(expr "$FULLDATA" : '.*<yweather:condition[^>]* date="\([^"]*\)".*')
OBSTMDAY=${OBSTMDATA%%,*}
OBSTMTIME=$(awk -F '[ ,:]+' '$7 == "pm" && $5 != "12" {conv = 12} {printf "%d:%d", $5 + conv, $6}' <<< $OBSTMDATA)
fi
if [ "$LOCATION" = "yes" ]; then
LOCATCITY=$(expr "$FULLDATA" : '.*<yweather:location[^>]* city="\([^"]*\)".*')
LOCATREGION=$(expr "$FULLDATA" : '.*<yweather:location[^>]* region="\([^"]*\)".*')
LOCATCOUNTRY=$(expr "$FULLDATA" : '.*<yweather:location[^>]* country="\([^"]*\)".*')
fi
CONDTEMP=$(expr "$FULLDATA" : '.*<yweather:condition[^>]* temp="\([^"]*\)".*')"$UNITSTEMP"
WINDSPEED=$(expr "$FULLDATA" : '.*<yweather:wind[^>]* speed="\([^"]*\)".*')" $UNITSSPEED"
if [ "$CONDVERB" = "yes" ]; then
UNITSDIST=$(expr "$FULLDATA" : '.*<yweather:units[^>]* distance="\([^"]*\)".*')
UNITSPRESS=$(expr "$FULLDATA" : '.*<yweather:units[^>]* pressure="\([^"]*\)".*')
ATMOVISIB=$(expr "$FULLDATA" : '.*<yweather:atmosphere[^>]* visibility="\([^"]*\)".*')" $UNITSDIST"
ATMOPRESS=$(expr "$FULLDATA" : '.*<yweather:atmosphere[^>]* pressure="\([^"]*\)".*')" $UNITSPRESS"
ATMOTRENDS=("→" "↑" "↓")
ATMOTREND=${ATMOTRENDS[$(expr "$FULLDATA" : '.*<yweather:atmosphere[^>]* rising="\([^"]*\)".*')]}
WINDDIRS=("N" "NNE" "NE" "ENE" "E" "ESE" "SE" "SSE" "S" "SSW" "SW" "WSW" "W" "WNW" "NW" "NNW")
WINDDIR=${WINDDIRS[($(expr "$FULLDATA" : '.*<yweather:wind[^>]* direction="\([^"]*\)".*') * 100 + 1125) / 2250 % 16]}
WINDCHILL=$(expr "$FULLDATA" : '.*<yweather:wind[^>]* chill="\([^"]*\)".*')"$UNITSTEMP"
fi
for FCSTDAY in "${!FCSTWDAY[@]}"; do
FCSTTEMPS[$FCSTDAY]="${FCSTLOW[$FCSTDAY]}/${FCSTHIGH[$FCSTDAY]}${UNITSTEMP}"
done
else
CONDTEMP="$CONDTEMP ("$(expr "$FULLDATA" : '.*<yweather:condition[^>]* temp="\([^"]*\)".*')"$UNITSTEMP)"
WINDSPEED="$WINDSPEED ("$(expr "$FULLDATA" : '.*<yweather:wind[^>]* speed="\([^"]*\)".*')" $UNITSSPEED)"
if [ "$CONDVERB" = "yes" ]; then
UNITSDIST=$(expr "$FULLDATA" : '.*<yweather:units[^>]* distance="\([^"]*\)".*')
UNITSPRESS=$(expr "$FULLDATA" : '.*<yweather:units[^>]* pressure="\([^"]*\)".*')
ATMOVISIB="$ATMOVISIB ("$(expr "$FULLDATA" : '.*<yweather:atmosphere[^>]* visibility="\([^"]*\)".*')" $UNITSDIST)"
ATMOPRESS="$ATMOPRESS ("$(expr "$FULLDATA" : '.*<yweather:atmosphere[^>]* pressure="\([^"]*\)".*')" $UNITSPRESS)"
WINDCHILL="$WINDCHILL ("$(expr "$FULLDATA" : '.*<yweather:wind[^>]* chill="\([^"]*\)".*')"$UNITSTEMP)"
fi
for FCSTDAY in "${!FCSTWDAY[@]}"; do
FCSTTEMPS[$FCSTDAY]="${FCSTTEMPS[$FCSTDAY]} (${FCSTLOW[$FCSTDAY]}/${FCSTHIGH[$FCSTDAY]}${UNITSTEMP})"
done
fi
}
### Main
getdata
if [ "$UNITSCONV" = "yes" ]; then
case $UNITS in
c)
UNITS="f"
;;
f)
UNITS="c"
;;
esac
getdata convert
fi
if [ "$LOCATION" = "yes" ]; then
OUTPUT="${CLON}Location:$CLOFF $LOCATCITY, $LOCATREGION, $LOCATCOUNTRY $CLON~$CLOFF "
fi
if [ "$CONDVERB" = "yes" ]; then
OUTPUT+="${CLON}Cond:$CLOFF $CONDTEMP, $CONDTEXT $CLON~ Atmo:$CLOFF $ATMOHUMID, $ATMOVISIB, $ATMOTREND $ATMOPRESS $CLON~ Wind:$CLOFF $WINDSPEED, $WINDDIR, $WINDCHILL"
else
OUTPUT+="${CLON}Temp:$CLOFF $CONDTEMP $CLON~ Cond:$CLOFF $CONDTEXT $CLON~ Humid:$CLOFF $ATMOHUMID $CLON~ Wind:$CLOFF $WINDSPEED"
fi
for FCSTDAY in "${!FCSTWDAY[@]}"; do
OUTPUT+=" $CLON~ ${FCSTWDAY[$FCSTDAY]}:$CLOFF ${FCSTTEMPS[$FCSTDAY]}, ${FCSTTEXT[$FCSTDAY]}"
done
if [ "$OBSTIME" = "yes" ]; then
OUTPUT+=" $CLON~ Time:$CLOFF "
if [ "$FORECASTS" = "yes" ]; then
OUTPUT+="$OBSTMDAY, "
fi
OUTPUT+="$OBSTMTIME"
fi
printf "%b\n" "$OUTPUT"
|