-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather.sh
executable file
·71 lines (65 loc) · 1.68 KB
/
weather.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
#!/bin/bash
function get {
echo $(xmllint --xpath "string($2)" - <<< "$1")
}
function get_icon {
CODE=$(get "$1" "//current/weather/@number")
case $CODE in
# Thunderstorm
200|201|202|230|231|232)
echo "<span font='Weather Icons 10' color='#9990b2'></span>"
;;
# Lightning
210|211|212|221)
echo "<span font='Weather Icons 10' color='#9990b2'></span>"
;;
# Rain
300|301|302|310|311|312|313|314|321|500|501|502|503|504|511|520|521|701)
echo "<span font='Weather Icons 10' color='#9990b2'></span>"
;;
# Snow
600|601|602|621|622|903)
echo "<span font='Weather Icons 10' color='#9990b2'></span>"
;;
# Fog
741)
echo "<span font='Weather Icons 10' color='#9990b2'></span>"
;;
# Clouds
771|801|802|803|804)
echo "<span font='Weather Icons 10' color='#9990b2'></span>"
;;
# Sunny
800|904)
echo "<span font='Weather Icons 10' color='#9990b2'></span>"
;;
*)
echo "FIXME"
;;
esac
}
function update {
WEATHER_INFO=$(curl -s "http://api.openweathermap.org/data/2.5/weather?q=$1&APPID=$2&mode=xml&units=metric")
if [ $? -ne 0 ]; then
exit 1
fi
echo "$WEATHER_INFO" >> /tmp/weather.xml
ICON=$(echo $(get_icon "$WEATHER_INFO"))
TEMP=$(echo $(get "$WEATHER_INFO" "//current/temperature/@value"))
STATUS="$TEMP°C "
rm -rf /tmp/weather_icon
rm -rf /tmp/weather_info
rm -rf /tmp/weather_last_updated
echo "$ICON" >> /tmp/weather_icon
echo "$STATUS" >> /tmp/weather_info
echo $(date +%s) >> /tmp/weather_last_updated
}
if [ -f /tmp/weather_last_updated ]; then
LAST_UPDATED=$(cat /tmp/weather_last_updated)
if [ `expr $(date +%s) - $LAST_UPDATED` -gt 900 ]; then
update $1 $2
fi
else
update $1 $2
fi
echo $(cat /tmp/weather_info)