Bash · 2637 bytes Raw Blame History
1 #!/bin/bash
2
3 cachedir=~/.cache/rbn
4 cachefile=${0##*/}-$1
5
6 if [ ! -d $cachedir ]; then
7 mkdir -p $cachedir
8 fi
9
10 if [ ! -f $cachedir/$cachefile ]; then
11 touch $cachedir/$cachefile
12 fi
13
14 # Save current IFS
15 SAVEIFS=$IFS
16 # Change IFS to new line.
17 IFS=$'\n'
18
19 cacheage=$(($(date +%s) - $(stat -c '%Y' "$cachedir/$cachefile")))
20 if [ $cacheage -gt 1740 ] || [ ! -s $cachedir/$cachefile ]; then
21 data=($(curl -s https://en.wttr.in/$1\?0qnT 2>&1))
22 echo ${data[0]} | cut -f1 -d, > $cachedir/$cachefile
23 echo ${data[1]} | sed -E 's/^.{15}//' >> $cachedir/$cachefile
24 echo ${data[2]} | sed -E 's/^.{15}//' >> $cachedir/$cachefile
25 fi
26
27 weather=($(cat $cachedir/$cachefile))
28
29 # Restore IFSClear
30 IFS=$SAVEIFS
31
32 temperature=$(echo ${weather[2]} | sed -E 's/([[:digit:]])+\.\./\1 to /g')
33
34 #echo ${weather[1]##*,}
35
36 # https://fontawesome.com/icons?s=solid&c=weather
37 case $(echo ${weather[1]##*,} | tr '[:upper:]' '[:lower:]') in
38 "clear" | "sunny")
39 condition=""
40 ;;
41 "partly cloudy")
42 condition="杖"
43 ;;
44 "cloudy")
45 condition=""
46 ;;
47 "overcast")
48 condition=""
49 ;;
50 "mist" | "fog" | "freezing fog")
51 condition=""
52 ;;
53 "patchy rain possible" | "patchy light drizzle" | "light drizzle" | "patchy light rain" | "light rain" | "light rain shower" | "rain")
54 condition=""
55 ;;
56 "moderate rain at times" | "moderate rain" | "heavy rain at times" | "heavy rain" | "moderate or heavy rain shower" | "torrential rain shower" | "rain shower")
57 condition=""
58 ;;
59 "patchy snow possible" | "patchy sleet possible" | "patchy freezing drizzle possible" | "freezing drizzle" | "heavy freezing drizzle" | "light freezing rain" | "moderate or heavy freezing rain" | "light sleet" | "ice pellets" | "light sleet showers" | "moderate or heavy sleet showers")
60 condition="ﭽ"
61 ;;
62 "blowing snow" | "moderate or heavy sleet" | "patchy light snow" | "light snow" | "light snow showers")
63 condition="流"
64 ;;
65 "blizzard" | "patchy moderate snow" | "moderate snow" | "patchy heavy snow" | "heavy snow" | "moderate or heavy snow with thunder" | "moderate or heavy snow showers")
66 condition="ﰕ"
67 ;;
68 "thundery outbreaks possible" | "patchy light rain with thunder" | "moderate or heavy rain with thunder" | "patchy light snow with thunder")
69 condition=""
70 ;;
71 *)
72 condition=""
73 echo -e "{\"text\":\""$condition"\", \"alt\":\""${weather[0]}"\", \"tooltip\":\""${weather[0]}: $temperature ${weather[1]}"\"}"
74 ;;
75 esac
76
77 #echo $temp $condition
78
79 echo -e "{\"text\":\""$temperature $condition"\", \"alt\":\""${weather[0]}"\", \"tooltip\":\""${weather[0]}: $temperature ${weather[1]}"\"}"