Files

136 lines
3.8 KiB
Bash
Executable File

#!/usr/bin/env bash
# fetch_wallpaper.sh
# Requires: wl-paste, curl, jq, move-wallpaper, next-wallpaper
set -euo pipefail
URL="$(wl-paste --no-newline || true)"
if [ -z "$URL" ]; then
echo "No URL in clipboard" >&2
exit 1
fi
# extract id (last path segment, strip trailing slash and query)
WALLPAPER_ID="$(printf '%s' "$URL" | sed -E 's#^.*/##; s/[/?].*$//')"
if [ -z "$WALLPAPER_ID" ]; then
echo "Could not extract id from URL: $URL" >&2
exit 1
fi
# Configuration
TEMP_DIR="/tmp/wallhaven_downloads"
API_KEY="$(pass api/wallhaven)" # Can set API key as environment variable
TARGET_RESOLUTION="2560x1600"
# Validate input
if [[ -z "$WALLPAPER_ID" ]]; then
echo "Error: No content in clipboard"
exit 1
fi
if [[ ! "$WALLPAPER_ID" =~ ^[a-zA-Z0-9]{4,8}$ ]]; then
echo "Error: Invalid wallhaven ID format: $WALLPAPER_ID"
exit 1
fi
# Create temp directory
mkdir -p "$TEMP_DIR"
# Build API URL
API_URL="https://wallhaven.cc/api/v1/w/$WALLPAPER_ID"
if [[ -n "$API_KEY" ]]; then
API_URL="$API_URL?apikey=$API_KEY"
fi
echo "Fetching wallpaper info for ID: $WALLPAPER_ID"
# Fetch wallpaper data from API
response=$(curl -s -f "$API_URL")
curl_exit_code=$?
if [[ $curl_exit_code -ne 0 ]]; then
echo "Error: Failed to fetch data from wallhaven API (curl exit: $curl_exit_code)"
exit 1
fi
# Check if jq is available for proper JSON parsing
if ! command -v jq &> /dev/null; then
echo "Error: jq is required for JSON parsing. Please install jq."
echo "Ubuntu/Debian: sudo apt install jq"
echo "Fedora: sudo dnf install jq"
echo "macOS: brew install jq"
exit 1
fi
# Parse JSON response
if echo "$response" | jq -e '.error' > /dev/null; then
error_msg=$(echo "$response" | jq -r '.error.message')
echo "API Error: $error_msg"
exit 1
fi
# Extract wallpaper information
wallpaper_data=$(echo "$response" | jq -r '.data')
if [[ "$wallpaper_data" == "null" || -z "$wallpaper_data" ]]; then
echo "Error: No wallpaper data found in API response"
exit 1
fi
# Get image URL and details
image_url=$(echo "$response" | jq -r '.data.path')
resolution=$(echo "$response" | jq -r '.data.resolution')
file_type=$(echo "$response" | jq -r '.data.file_type')
file_size=$(echo "$response" | jq -r '.data.file_size')
purity=$(echo "$response" | jq -r '.data.purity')
# Validate extracted data
if [[ -z "$image_url" || "$image_url" == "null" ]]; then
echo "Error: Could not extract image URL from API response"
exit 1
fi
echo "Wallpaper details:"
echo " Resolution: $resolution"
echo " File type: $file_type"
echo " File size: $((file_size / 1024)) KB"
echo " Purity: $purity"
echo " URL: $image_url"
# Check if resolution matches target
if [[ "$resolution" != "$TARGET_RESOLUTION" ]]; then
echo "Warning: Wallpaper resolution is $resolution (target: $TARGET_RESOLUTION)"
fi
# Download the image
filename=$(basename "$image_url")
full_path="$TEMP_DIR/$filename"
echo "Downloading wallpaper to: $full_path"
if curl -s -f -o "$full_path" "$image_url"; then
# Verify download
if [[ -f "$full_path" && -s "$full_path" ]]; then
downloaded_size=$(stat -c%s "$full_path" 2>/dev/null || stat -f%z "$full_path" 2>/dev/null)
echo "Download successful: $full_path ($((downloaded_size / 1024)) KB)"
# Execute move_wallpaper command
echo "Executing: move_wallpaper -r \"$full_path\""
if move_wallpaper -r "$full_path" 2>/dev/null; then
echo "Wallpaper processing completed successfully"
else
echo "move_wallpaper command completed (exit status ignored as requested)"
fi
zenity --notification --text "Wallpaper downloaded!"
else
echo "Error: Downloaded file is empty or missing"
exit 1
fi
else
echo "Error: Failed to download the wallpaper from: $image_url"
exit 1
fi