|
#!/bin/bash |
|
|
|
API_URL="https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1" |
|
BASE_DIR="$HOME/Pictures/Bing" |
|
UHD_DIR="$BASE_DIR/UHD" |
|
PORTRAIT_DIR="$BASE_DIR/Portrait" |
|
INFO_FILE="$BASE_DIR/info.txt" |
|
TEMP_DIR="/tmp/bing_wallpaper_$$" |
|
|
|
RED='\033[0;31m' |
|
NC='\033[0m' |
|
|
|
set -e |
|
trap 'rm -rf "$TEMP_DIR"' EXIT |
|
|
|
mkdir -p "$UHD_DIR" "$PORTRAIT_DIR" "$TEMP_DIR" |
|
|
|
json_data=$(curl -s "$API_URL") |
|
|
|
urlbase=$(echo "$json_data" | grep -o '"urlbase":"[^"]*"' | head -1 | cut -d'"' -f4) |
|
copyright=$(echo "$json_data" | grep -o '"copyright":"[^"]*"' | head -1 | cut -d'"' -f4) |
|
|
|
filename=$(echo "$urlbase" | sed -n 's/.*OHR\.\(.*\)_DE-.*/\1/p') |
|
|
|
if [ -z "$filename" ]; then |
|
echo -e "${RED}✗ Error: Could not extract filename${NC}" |
|
exit 1 |
|
fi |
|
|
|
formats=("UHD" "1080x1920") |
|
destinations=("$UHD_DIR" "$PORTRAIT_DIR") |
|
output_files=() |
|
|
|
for i in "${!formats[@]}"; do |
|
format="${formats[$i]}" |
|
dest_dir="${destinations[$i]}" |
|
|
|
download_url="http://www.bing.com${urlbase}_${format}.jpg" |
|
temp_jpg="$TEMP_DIR/${filename}_${format}.jpg" |
|
output_avif="$dest_dir/${filename}.avif" |
|
|
|
if curl -f -s -o "$temp_jpg" "$download_url"; then |
|
/opt/homebrew/bin/magick "$temp_jpg" "$output_avif" 2>/dev/null |
|
output_files+=("$output_avif") |
|
else |
|
echo -e "${RED}✗ Download failed: $download_url${NC}" |
|
exit 1 |
|
fi |
|
done |
|
|
|
echo "$copyright" > "$INFO_FILE" |
|
|
|
osascript <<EOF >/dev/null 2>&1 |
|
tell application "System Events" |
|
set desktopCount to count of desktops |
|
|
|
if desktopCount >= 1 then |
|
tell desktop 1 |
|
set picture to "${output_files[1]}" |
|
end tell |
|
end if |
|
|
|
if desktopCount >= 2 then |
|
tell desktop 2 |
|
set picture to "${output_files[0]}" |
|
end tell |
|
end if |
|
end tell |
|
EOF |