feat: smarter weather script

This commit is contained in:
Abhishek Keshri 2024-03-17 02:39:45 +05:30 committed by Abhishek Keshri
parent d12a912d28
commit 37c495f15d
2 changed files with 50 additions and 82 deletions

View file

@ -1,44 +0,0 @@
#!/usr/bin/env bash
# setting the locale, some users have issues with different locales, this forces the correct one
export LC_ALL=en_US.UTF-8
#wrapper script for running weather on interval
fahrenheit=$1
location=$2
fixedlocation=$3
LOCKFILE=/tmp/.tmux2k-tmux-weather.lock
DATAFILE=/tmp/.tmux2k-tmux-data
ensure_single_process() {
# check for another running instance of this script and terminate it if found
[ -f $LOCKFILE ] && ps -p "$(cat $LOCKFILE)" -o cmd= | grep -F " ${BASH_SOURCE[0]}" && kill "$(cat $LOCKFILE)"
echo $$ >$LOCKFILE
}
main() {
ensure_single_process
current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [ ! -f $DATAFILE ]; then
printf "Loading..." >$DATAFILE
fi
"$current_dir"/weather.sh >$DATAFILE
while tmux has-session &>/dev/null; do
"$current_dir"/weather.sh "$fahrenheit" "$location" "$fixedlocation" >$DATAFILE
if tmux has-session &>/dev/null; then
sleep 1200
else
break
fi
done
rm $LOCKFILE
}
#run main driver function
main

View file

@ -1,15 +1,37 @@
#!/usr/bin/env bash #!/usr/bin/env bash
# setting the locale, some users have issues with different locales, this forces the correct one
export LC_ALL=en_US.UTF-8 export LC_ALL=en_US.UTF-8
fahrenheit=$1 current_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
location=$2 source "$current_dir"/utils.sh
fixedlocation=$3
show_fahrenheit=$(get_tmux_option "@tmux2k-show-fahrenheit" false)
show_location=$(get_tmux_option "@tmux2k-show-location" false)
fixed_location=$(get_tmux_option "@tmux2k-fixed-location" "Rampurhat")
declare -A weather_icons=(
["Clear"]="󰖙"
["Cloud"]=""
["Drizzle"]="󰖗"
["Fog"]=""
["Haze"]="󰼰"
["Mist"]=""
["Overcast"]=""
["Rain"]=""
["Sand"]=""
["Shower"]=""
["Smoke"]=""
["Snow"]=""
["Sunny"]="󰖙"
["Thunderstorm"]=""
["Tornado"]="󰼸"
["Windy"]="󰖝"
)
display_location() { display_location() {
if $location && [[ -n "$fixedlocation" ]]; then if [[ -n "$fixed_location" ]]; then
echo " $fixedlocation" echo "$fixed_location"
elif $location; then elif $show_location; then
city=$(curl -s https://ipinfo.io/city 2>/dev/null) city=$(curl -s https://ipinfo.io/city 2>/dev/null)
region=$(curl -s https://ipinfo.io/region 2>/dev/null) region=$(curl -s https://ipinfo.io/region 2>/dev/null)
echo "$city, $region" echo "$city, $region"
@ -19,43 +41,34 @@ display_location() {
} }
fetch_weather_information() { fetch_weather_information() {
display_weather=$1 if $show_fahrenheit; then
# it gets the weather condition textual name (%C), and the temperature (%t) display_weather='&u'
curl -sL wttr.in/"$fixedlocation"\?format="%C+%t$display_weather"
}
#get weather display
display_weather() {
if $fahrenheit; then
display_weather='&u' # for USA system
else else
display_weather='&m' # for metric system display_weather='&m'
fi fi
weather_information=$(fetch_weather_information $display_weather) curl -sL "wttr.in/$fixed_location?format=%C+%t$display_weather"
weather_condition=$(echo "$weather_information" | rev | cut -d ' ' -f2- | rev) # Sunny, Snow, etc
temperature=$(echo "$weather_information" | rev | cut -d ' ' -f 1 | rev) # +31°C, -3°F, etc
unicode=$(forecast_unicode "$weather_condition")
echo "$unicode${temperature/+/}" # remove the plus sign to the temperature
} }
forecast_unicode() { forecast_unicode() {
weather_condition=$(echo "$weather_condition" | awk '{print tolower($0)}') local condition=$1
weather_icon="${weather_icons[$condition]}"
if [[ $weather_condition =~ 'snow' ]]; then if [[ -n $weather_icon ]]; then
echo '❄ ' echo "$weather_icon "
elif [[ (($weather_condition =~ 'rain') || ($weather_condition =~ 'shower')) ]]; then
echo '☂ '
elif [[ (($weather_condition =~ 'overcast') || ($weather_condition =~ 'cloud')) ]]; then
echo '☁ '
elif [[ $weather_condition = 'NA' ]]; then
echo ''
else else
echo ' ' echo ' '
fi fi
} }
display_weather() {
weather_information=$(fetch_weather_information "$display_weather")
condition=$(echo "$weather_information" | rev | cut -d ' ' -f2- | tr -d '[:space:]' | rev)
temperature=$(echo "$weather_information" | rev | cut -d ' ' -f 1 | rev)
unicode=$(forecast_unicode "$condition")
echo "$unicode${temperature/+/} $condition"
}
main() { main() {
# process should be cancelled when session is killed # process should be cancelled when session is killed
if ping -q -c 1 -W 1 ipinfo.io &>/dev/null; then if ping -q -c 1 -W 1 ipinfo.io &>/dev/null; then
@ -65,5 +78,4 @@ main() {
fi fi
} }
#run main driver program
main main