irongit
87 lines2.8 KBBourne Again Shell (bash)
1#!/bin/bash
2# /* ---- 💫 https://github.com/JaKooLit 💫 ---- */ ##
3# weather info from wttr. https://github.com/chubin/wttr.in
4# Remember to add city
5
6city=
7cachedir="~/.cache/rbn"
8cachefile=${0##*/}-$1
9
10if [ ! -d $cachedir ]; then
11 mkdir -p $cachedir
12fi
13
14if [ ! -f $cachedir/$cachefile ]; then
15 touch $cachedir/$cachefile
16fi
17
18# Save current IFS
19SAVEIFS=$IFS
20# Change IFS to new line.
21IFS=$'\n'
22
23cacheage=$(($(date +%s) - $(stat -c '%Y' "$cachedir/$cachefile")))
24if [ $cacheage -gt 1740 ] || [ ! -s $cachedir/$cachefile ]; then
25 data=($(curl -s "https://en.wttr.in/${city}${1}\?0qnT&u" 2>&1))
26 echo ${data[0]} | cut -f1 -d, >$cachedir/$cachefile
27 echo ${data[1]} | sed -E 's/^.{15}//' >>$cachedir/$cachefile
28 echo ${data[2]} | sed -E 's/^.{15}//' >>$cachedir/$cachefile
29fi
30
31weather=($(cat $cachedir/$cachefile))
32
33# Restore IFSClear
34IFS=$SAVEIFS
35
36temperature=$(echo ${weather[2]} | sed -E 's/([[:digit:]]+)\.\./\1 to /g')
37
38#echo ${weather[1]##*,}
39
40# https://fontawesome.com/icons?s=solid&c=weather
41case $(echo ${weather[1]##*,} | tr '[:upper:]' '[:lower:]') in
42"clear" | "sunny")
43 condition=""
44 ;;
45"partly cloudy")
46 condition="󰖕"
47 ;;
48"cloudy")
49 condition=""
50 ;;
51"overcast")
52 condition=""
53 ;;
54"fog" | "freezing fog")
55 condition=""
56 ;;
57"patchy rain possible" | "patchy light drizzle" | "light drizzle" | "patchy light rain" | "light rain" | "light rain shower" | "mist" | "rain")
58 condition="󰼳"
59 ;;
60"moderate rain at times" | "moderate rain" | "heavy rain at times" | "heavy rain" | "moderate or heavy rain shower" | "torrential rain shower" | "rain shower")
61 condition=""
62 ;;
63"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")
64 condition="󰼴"
65 ;;
66"blowing snow" | "moderate or heavy sleet" | "patchy light snow" | "light snow" | "light snow showers")
67 condition="󰙿"
68 ;;
69"blizzard" | "patchy moderate snow" | "moderate snow" | "patchy heavy snow" | "heavy snow" | "moderate or heavy snow with thunder" | "moderate or heavy snow showers")
70 condition=""
71 ;;
72"thundery outbreaks possible" | "patchy light rain with thunder" | "moderate or heavy rain with thunder" | "patchy light snow with thunder")
73 condition=""
74 ;;
75*)
76 condition=""
77 echo -e "{\"text\":\""$condition"\", \"alt\":\""${weather[0]}"\", \"tooltip\":\""${weather[0]}: $temperature ${weather[1]}"\"}"
78 ;;
79esac
80
81#echo $temp $condition
82
83echo -e "{\"text\":\""$temperature $condition"\", \"alt\":\""${weather[0]}"\", \"tooltip\":\""${weather[0]}: $temperature ${weather[1]}"\"}"
84
85cached_weather="$temperature \n$condition ${weather[1]}"
86
87echo -e $cached_weather >"~/.cache/.weather_cache"