Skip to content

wallpaper

Every 5 minutes from wallhaven

Enable both the service and the timer.

Multiple files and systemd timers

The benefit of multiple files is better control. If you don't like the wallpaper you could just:

fetch-wallpaper.sh && set-wallpaper.sh

~/bin/fetch-wallpaper.sh
WALLHAVEN_API="https://wallhaven.cc/api/v1/search?q=nature&categories=100&purity=100&sorting=random&resolutions=3840x2160&page=1"
WALLPAPER="/tmp/wallpaper.jpg"

# Get response from Wallhaven API
APIRESP=$(curl -s "$WALLHAVEN_API")
# Extract the first image URL using jq
IMAGEURL=$(echo "$APIRESP" | jq -r '.data[].path' | shuf -n 1)

if [ -n "$IMAGEURL" ] && [ "$IMAGEURL" != "null" ]; then
  curl -s --max-time 10 "$IMAGEURL" -o "$WALLPAPER"
fi
~/bin/set-wallpaper.sh
#!/bin/bash

WALLPAPER="/tmp/wallpaper.jpg"
FALLBACK="$HOME/media/wallpapers/20250204.jpg"
DATE_TEXT=$(date '+%A, %B %d %Y%n%H:%M%n%Y-%m-%d')

if ! [[ -e $WALLPAPER ]]; then
  WALLPAPER="$FALLBACK"
fi

magick "$WALLPAPER" \
  \( -size 800x240 xc:'#000000' \
  -alpha set -channel A -evaluate set 70% +channel \
  -virtual-pixel transparent \
  -gaussian-blur 0x10 \) \
  -gravity southeast -geometry +20+20 -composite \
  \( -size 800x240 xc:none \
  -font "Iosevka-Term-SS12" -pointsize 48 \
  -fill white -gravity center \
  -annotate +0+0 "$DATE_TEXT" \
  -pointsize 36 \) \
  -gravity southeast -geometry +20+20 -composite \
  - | feh --bg-fill -
~/.config/systemd/user/fetch-wallpaper.service
[Unit]
Description=Update wallpaper with time

[Service]
Type=oneshot
Environment=DISPLAY=:1
Environment=XAUTHORITY=%h/.local/share/sx/xauthority
ExecStart=%h/bin/fetch-wallpaper.sh

[Install]
WantedBy=default.target
~/.config/systemd/user/fetch-wallpaper.timer
[Unit]
Description=Update wallpaper every minute

[Timer]
OnCalendar=*:0/5
AccuracySec=1s
Persistent=true

[Install]
WantedBy=timers.target
~/.config/systemd/user/update-wallpaper.service
[Unit]
Description=Update wallpaper with time

[Service]
Type=oneshot
Environment=DISPLAY=:1
Environment=XAUTHORITY=%h/.local/share/sx/xauthority
ExecStart=%h/bin/set-wallpaper.sh

[Install]
WantedBy=default.target

# ~/.config/systemd/user/wallpaper-update.timer
[Unit]
Description=Update wallpaper every minute

[Timer]
OnCalendar=*:0/1
AccuracySec=1s
Persistent=true

[Install]
WantedBy=timers.target
~/.config/systemd/user/update-wallpaper.timer
[Unit]
Description=Update wallpaper every minute

[Timer]
OnCalendar=*:0/1
AccuracySec=1s
Persistent=true

[Install]
WantedBy=timers.target

Single file and a systemd timer

#!/bin/bash
WALLHAVEN_API="https://wallhaven.cc/api/v1/search?q=nature&categories=100&purity=100&sorting=random&resolutions=3840x2160&page=1"
WALLPAPER="/tmp/wallpaper.jpg"
LAST_RUN_FILE="/tmp/wallpaper_last_run"

# Function to download new wallpaper
fetch_wallpaper() {
  local image_url
  local api_response
  # Get response from Wallhaven API
  api_response=$(curl -s "$WALLHAVEN_API")
  # Extract the first image URL using jq
  image_url=$(echo "$api_response" | jq -r '.data[0].path')
  # Extract title
  title=$(echo "$api_response" | jq -r '.data[0].id')
  if [ -n "$image_url" ] && [ "$image_url" != "null" ]; then
    curl -s --max-time 10 "$image_url" -o "$WALLPAPER"
    echo "$title" >"${WALLPAPER}.title"
  fi
  # Store current timestamp
  date +%s >"$LAST_RUN_FILE"
}

# Check if we need to fetch a new wallpaper
current_time=$(date +%s)
if [ -f "$LAST_RUN_FILE" ]; then
  last_run=$(cat "$LAST_RUN_FILE")
  time_diff=$((current_time - last_run))
  if [ $time_diff -ge 300 ]; then # 300 seconds = 5 minutes
    fetch_wallpaper
  fi
else
  # First run or last_run file missing
  fetch_wallpaper
fi

# Get the title from saved file or use a default
TITLE=$(cat "${WALLPAPER}.title" 2>/dev/null || echo "Daily Wallpaper")
# Get the date text
DATE_TEXT=$(date '+%A, %B %d %Y%n%H:%M%n%Y-%m-%d')

# Only proceed if wallpaper exists
if [ -f "$WALLPAPER" ]; then
  magick "$WALLPAPER" \
    \( -size 800x240 xc:'#000000' \
    -alpha set -channel A -evaluate set 70% +channel \
    -virtual-pixel transparent \
    -gaussian-blur 0x10 \) \
    -gravity southeast -geometry +20+20 -composite \
    \( -size 800x240 xc:none \
    -font "Iosevka-Term-SS12" -pointsize 48 \
    -fill white -gravity center \
    -annotate +0+0 "$DATE_TEXT" \
    -pointsize 36 \) \
    -gravity southeast -geometry +20+20 -composite \
    - | feh --bg-fill -
fi

Everyday from bing wallpaper of the day

$HOME/bin/wallpaper.sh
#!/bin/bash
BING_API="https://bing.biturl.top/?resolution=UHD&format=json&index=0&mkt=zh-CN"
LOCAL_WALLPAPER="$HOME/admin/dwm/wallpaper.jpg"
WALLPAPER_DIR="$HOME/media/wallpapers" # Directory to store daily wallpapers
TODAY_WALLPAPER="${WALLPAPER_DIR}/$(date '+%Y%m%d').jpg"

# Ensure wallpaper directory exists
mkdir -p "$WALLPAPER_DIR"

# Function to check if we need to update wallpaper
need_wallpaper_update() {
  [ ! -f "$TODAY_WALLPAPER" ]
}

# Function to download new wallpaper
fetch_wallpaper() {
  local image_url
  local api_response
  api_response=$(curl -s "$BING_API")
  image_url=$(echo "$api_response" | grep -o '"url":"[^"]*"' | cut -d'"' -f4)
  title=$(echo $image_url | awk -F 'id=' '{print $2}')
  if [ -n "$image_url" ] && curl -s --max-time 10 "$image_url" -o "$TODAY_WALLPAPER"; then
    echo "$title" >"${TODAY_WALLPAPER}.title"
    return 0
  fi
  cp "${LOCAL_WALLPAPER/#\~/$HOME}" "$TODAY_WALLPAPER" 2>/dev/null || return 1
}

# Update wallpaper if needed
if need_wallpaper_update; then
  fetch_wallpaper
fi

# Get the title from saved file or use a default
TITLE=$(cat "${TODAY_WALLPAPER}.title" 2>/dev/null || echo "Daily Wallpaper")

# Get the date text
DATE_TEXT=$(date '+%A, %B %d %Y%n%H:%M%n%Y-%m-%d')

magick "$TODAY_WALLPAPER" \
  \( -size 800x320 xc:'#000000' \
  -alpha set -channel A -evaluate set 70% +channel \
  -virtual-pixel transparent \
  -gaussian-blur 0x10 \) \
  -gravity southeast -geometry +20+20 -composite \
  \( -size 800x320 xc:none \
  -font "Iosevka-Term-SS12" -pointsize 48 \
  -fill white -gravity center \
  -annotate +0-40 "$DATE_TEXT" \
  -pointsize 36 \
  -annotate +0+100 "$TITLE" \) \
  -gravity southeast -geometry +20+20 -composite \
  - | feh --bg-fill -
$HOME/.config/systemd/user/update-wallpaper.service
[Unit]
Description=Update wallpaper with time

[Service]
Type=oneshot
Environment=DISPLAY=:1
Environment=XAUTHORITY=%h/.local/share/sx/xauthority
ExecStart=%h/bin/wallpaper.sh

[Install]
WantedBy=default.target

# ~/.config/systemd/user/wallpaper-update.timer
[Unit]
Description=Update wallpaper every minute

[Timer]
OnCalendar=*:0/1
AccuracySec=1s
Persistent=true

[Install]
WantedBy=timers.target
$HOME/.config/systemd/user/update-wallpaper.timer
[Unit]
Description=Update wallpaper with time

[Service]
Type=oneshot
Environment=DISPLAY=:1
Environment=XAUTHORITY=%h/.local/share/sx/xauthority
ExecStart=%h/bin/wallpaper.sh

[Install]
WantedBy=default.target

# ~/.config/systemd/user/wallpaper-update.timer
[Unit]
Description=Update wallpaper every minute

[Timer]
OnCalendar=*:0/1
AccuracySec=1s
Persistent=true

[Install]
WantedBy=timers.target

Comments