Liège 360
Published © LGPL

Bash script is animated, self-updating satellite wallpaper.

This Bash script is animated, self-updating Raspberry Pi wallpaper using the latest infrared satellite images of Europe from the RMI.

BeginnerShowcase (no instructions)16
Bash script is animated, self-updating satellite wallpaper.

Story

Read more

Code

wallpaper.sh

BatchFile
#!/bin/bash
WORKDIR="$HOME"
IMGDIR="$WORKDIR/sat_images"
URL="https://www.meteo.be/en/weather/observations/satellite-images/infrared-europe"

mkdir -p "$IMGDIR"

set_wallpaper() {
    local img_path="$1"
    echo "Setting wallpaper: $img_path"
    if command -v pcmanfm &> /dev/null; then
        pcmanfm --set-wallpaper="$img_path"
    elif command -v wayfiremsg &> /dev/null; then
        wayfiremsg set-output-config "wallpaper $img_path"
    elif command -v labwc &> /dev/null; then
        wpaperd -p "$img_path" &> /dev/null || pcmanfm-qt --set-wallpaper="$img_path"
    fi
}

while true; do
    echo "Fetching the 4 latest satellite images... $(date)"

    IMAGE_URLS=$(curl -s "$URL" | grep -oE 'https://[^"]+\.jpg' | sort -u | tail -n 4)
    NUM_URLS=$(echo "$IMAGE_URLS" | tr -d '\r' | grep -v '^$' | wc -l)

    if [ "$NUM_URLS" -gt 0 ]; then
        rm -f "$IMGDIR"/wall_*.png
        rm -f "$IMGDIR"/temp_*.jpg

        for num in $(seq 1 "$NUM_URLS"); do
            IMG_URL=$(echo "$IMAGE_URLS" | awk "NR==$num")
            if [ -n "$IMG_URL" ]; then
                curl -s "$IMG_URL" -o "$IMGDIR/temp_${num}.jpg"
                if [ -s "$IMGDIR/temp_${num}.jpg" ]; then
                    convert "$IMGDIR/temp_${num}.jpg" "$IMGDIR/wall_${num}.png" 2>/dev/null
                fi
            fi
        done

        echo "Starting 5-minute loop (switching every 5s)..."

        current_wall=1
        for i in {1..60}; do
            if [ -f "$IMGDIR/wall_${current_wall}.png" ]; then
                set_wallpaper "$IMGDIR/wall_${current_wall}.png"
            else
                if [ -f "$IMGDIR/wall_1.png" ]; then
                    set_wallpaper "$IMGDIR/wall_1.png"
                    current_wall=1
                fi
            fi
            
            sleep 5

            current_wall=$((current_wall + 1))
            if [ $current_wall -gt "$NUM_URLS" ] || [ $current_wall -gt 4 ]; then
                current_wall=1
            fi
        done
    else
        echo "No images found, retrying in 5 seconds."
        sleep 5
    fi
done

Credits

Liège 360
23 projects • 0 followers

Comments