You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
203 lines
5.6 KiB
203 lines
5.6 KiB
#!/bin/bash
|
|
|
|
# background-weather.sh
|
|
# Gets a background and sets it depending on the time of day and season.
|
|
|
|
# Get the true path to the wallpapers directory
|
|
if [ -L "$0" ] ; then
|
|
true_file=$(readlink -f "$0");
|
|
base_dir=$( cd "$(dirname "$true_file")" ; pwd -P );
|
|
else
|
|
base_dir=$( cd "$(dirname "$0")" ; pwd -P );
|
|
fi
|
|
|
|
# Defaults
|
|
WEATHER="clear";
|
|
test=false;
|
|
LAT=false;
|
|
LNG=false;
|
|
OW_API_KEY="";
|
|
CITY="";
|
|
CONFIG="";
|
|
IMG_FILE="";
|
|
wallpapers_dir="$base_dir/wallpapers";
|
|
|
|
# Import config file
|
|
declare -a configs=(
|
|
"/etc/background-weather.conf"
|
|
"$base_dir/background-weather.conf"
|
|
"/home/$USER/.config/background-weather/background-weather.conf"
|
|
)
|
|
|
|
for config_file in "${configs[@]}"; do
|
|
if [ -f "$config_file" ]; then
|
|
CONFIG="$config_file"
|
|
break;
|
|
fi
|
|
done
|
|
|
|
if [ "$CONFIG" != "" ]; then
|
|
set -a # export all variables from config
|
|
source "$CONFIG"
|
|
set +a # stop exporting
|
|
fi
|
|
|
|
# Get options (override config)
|
|
options=$@
|
|
arguments=($options)
|
|
index=0
|
|
for argument in $options ; do
|
|
index=`expr $index + 1`
|
|
case $argument in
|
|
"--weather"|"-w")
|
|
WEATHER="${arguments[index]}"
|
|
;;
|
|
"--api-key")
|
|
OW_API_KEY="${arguments[index]}"
|
|
;;
|
|
"--lat")
|
|
LAT="${arguments[index]}"
|
|
;;
|
|
"--lng")
|
|
LNG="${arguments[index]}"
|
|
;;
|
|
"--config")
|
|
CONFIG="${arguments[index]}"
|
|
;;
|
|
"--img")
|
|
IMG_FILE="${arguments[index]}"
|
|
;;
|
|
"--test"|"-t")
|
|
test="${arguments[index]}"
|
|
;;
|
|
|
|
esac
|
|
done
|
|
|
|
if [ "$LAT" != false ] && [ "$LNG" != false ] && [ "$OW_API_KEY" != "" ]; then
|
|
OW_API_URL="https://api.openweathermap.org/data/2.5/weather?lat=$LAT&lon=$LNG&appid=$OW_API_KEY"
|
|
|
|
# Get __WEATHER__ from JSON response:
|
|
# e.g {"coord":{"lon":12.345,"lat":12.345},"weather":[{"id":800,"main":"__WEATHER__", ...
|
|
if [ -x "$(command -v jq)" ]; then
|
|
WEATHER=$(curl --silent $OW_API_URL | jq -r '.weather[0].main')
|
|
else
|
|
WEATHER=$(curl --silent "$OW_API_URL" --stderr - \
|
|
| grep -E -o ',"main":"[0-9a-zA-Z]*",' \
|
|
| sed 's/,"main":"//g' \
|
|
| head -c -3)
|
|
fi
|
|
|
|
# To lowercase
|
|
WEATHER=$(echo "$WEATHER" | sed -e 's/\(.*\)/\L\1/')
|
|
fi
|
|
|
|
# DOY = Current Day of Year (without leading zeroes)
|
|
DOY=$(date '+%-j');
|
|
|
|
# The current hours from 0 to 23.
|
|
HR=$(date '+%-H');
|
|
|
|
if ((DOY >= 79 && DOY < 172)) ; then
|
|
# March 20 - June 21
|
|
season="spring";
|
|
elif ((DOY >= 172 && DOY < 265)) ; then
|
|
# June 21 - September 22
|
|
season="summer";
|
|
elif ((DOY >= 265 && DOY < 355)) ; then
|
|
# September 22 - December 21
|
|
season="fall";
|
|
else
|
|
# December 21 - March 20
|
|
season="winter";
|
|
fi
|
|
|
|
# TODO: To adjust light according to latitude to modify hours
|
|
set -a # export all variables created next
|
|
|
|
# Get file by hour time
|
|
if [ "$IMG_FILE" = "" ]; then
|
|
case "$HR" in
|
|
0) IMG_FILE="00-mid-night.png" ;;
|
|
1) IMG_FILE="01-late-night.png" ;;
|
|
2) IMG_FILE="02-early-vigil.png" ;;
|
|
3) IMG_FILE="03-mid-vigil.png" ;;
|
|
4) IMG_FILE="04-late-vigil.png" ;;
|
|
5) IMG_FILE="05-dawn.png" ;;
|
|
6) IMG_FILE="06-early-morning.png" ;;
|
|
7) IMG_FILE="07-mid-morning.png" ;;
|
|
8) IMG_FILE="08-late-morning.png" ;;
|
|
9) IMG_FILE="09-early-forenoon.png" ;;
|
|
10) IMG_FILE="10-mid-forenoon.png" ;;
|
|
11) IMG_FILE="11-late-forenoon.png" ;;
|
|
12) IMG_FILE="12-noon.png" ;;
|
|
13) IMG_FILE="13-early-afternoon.png" ;;
|
|
14) IMG_FILE="14-mid-afternoon.png" ;;
|
|
15) IMG_FILE="15-late-afternoon.png" ;;
|
|
16) IMG_FILE="16-early-evening.png" ;;
|
|
17) IMG_FILE="17-mid-evening.png" ;;
|
|
18) IMG_FILE="18-late-evening.png" ;;
|
|
19) IMG_FILE="19-early-dusk.png" ;;
|
|
20) IMG_FILE="20-mid-dusk.png" ;;
|
|
21) IMG_FILE="21-late-dusk.png" ;;
|
|
22) IMG_FILE="22-sunset.png" ;;
|
|
23) IMG_FILE="23-early-night.png" ;;
|
|
esac
|
|
fi
|
|
|
|
# Put path together
|
|
if [ -f "$wallpapers_dir/$season/$WEATHER/$IMG_FILE" ]; then
|
|
# ./wallpapers/spring/clear/00-filename.png
|
|
img_path="$wallpapers_dir/$season/$WEATHER/$IMG_FILE"
|
|
|
|
elif [ -f "$wallpapers_dir/$season/$IMG_FILE" ]; then
|
|
# ./wallpapers/spring/00-filename.png
|
|
img_path="$wallpapers_dir/$season/$IMG_FILE"
|
|
|
|
else
|
|
# ./wallpapers/00-filename.png
|
|
img_path="$wallpapers_dir/$IMG_FILE"
|
|
fi
|
|
|
|
# Check for file
|
|
if [ ! -f "$img_path" ]; then
|
|
echo "File dos not exist:"
|
|
echo "$img_path"
|
|
exit 1;
|
|
fi
|
|
|
|
# Check for test mode
|
|
if ! [ "$test" = false ] ; then
|
|
echo "Path: $img_path";
|
|
exit;
|
|
fi
|
|
|
|
# Set the background link:
|
|
|
|
# feh --bg-fill "$img_path"
|
|
# ls -n /etc/alternatives/desktop-background "$img_path"
|
|
|
|
# (do x11 mambo to set a wallpaper, ugh)
|
|
export DISPLAY=:0
|
|
export XAUTHORITY="/home/$USER/.Xauthority"
|
|
export XDG_RUNTIME_DIR="/run/user/$UID"
|
|
|
|
if [ -x "$(command -v pcmanfm)" ]; then
|
|
# For Raspbian
|
|
pcmanfm --wallpaper-mode=stretch --set-wallpaper="$img_path"
|
|
elif [ -x "$(command -v gsettings)" ]; then
|
|
# Gnome 3.22.2
|
|
gsettings set org.gnome.desktop.background picture-uri "file://$img_path"
|
|
elif [ -x "$(command -v xfconf-query)" ]; then
|
|
# XFCE
|
|
xfconf-query --channel xfce4-desktop --property /backdrop/screen0/monitor0/image-path --set $img_path
|
|
xfdesktop --reload
|
|
#xfconf-query --channel xfce4-desktop --list # lists all related properties, in case screen0/monitor0 isn't the one.
|
|
elif [ -x "$(command -v feh)" ]; then
|
|
# feh, used sometimes by i3 and AwesomeWM
|
|
feh --bg-scale $img_path
|
|
else
|
|
echo "No suitable background manager found."
|
|
echo "File found:"
|
|
echo "$img_path"
|
|
fi
|
|
|