Closed fbcoder closed 6 years ago
Dear FBCODER and OAHONG:
Maybe this code helps.
Cheers,
Dani
#!/usr/bin/env bash
# Author: Marguerite Su , dzmanto
# License: GPL-3.0
# Description: Download Bing Wallpaper of the Day and set it as your Linux Desktop.
# https://github.com/marguerite/linux-bing-wallpaper
function contains() {
local n=$#
local value=${!n}
for (( i=1; i < $#; i++ ))
do
if [ "${!i}" == "${value}" ]; then
echo "y"
return 0
fi
done
echo "n"
return 1
}
function detectDE() {
# see https://bugs.freedesktop.org/show_bug.cgi?id=34164
unset GREP_OPTIONS
uname -a | grep -i darwin 2>&1 >/dev/null && DE="mac";
if [ -n "${XDG_CURRENT_DESKTOP}" ]; then
case "${XDG_CURRENT_DESKTOP}" in
'GNOME')
DE="gnome"
;;
'KDE')
DE="kde"
;;
'LXDE')
DE="lxde"
;;
'LXQt')
DE="lxqt"
;;
'MATE')
DE="mate"
;;
'XFCE')
DE="xfce"
;;
'X-Cinnamon')
DE="cinnamon"
;;
esac
fi
if [ x"$DE" = x"" ]; then
# classic fallbacks
if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
elif [ x"$MATE_DESKTOP_SESSION_ID" != x"" ]; then DE=mate;
elif `dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager > /dev/null 2>&1` ; then DE=gnome;
elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
elif xprop -root 2> /dev/null | grep -i '^xfce_desktop_window' >/dev/null 2>&1; then DE=xfce
fi
fi
if [ x"$DE" = x"" ]; then
# fallback to checking $DESKTOP_SESSION
case "$DESKTOP_SESSION" in
'gnome')
DE="gnome"
;;
'LXDE'|'Lubuntu')
DE="lxde"
;;
'MATE')
DE="mate"
;;
'xfce'|'xfce4'|'Xfce Session')
DE="xfce"
;;
esac
fi
if [ x"$DE" = x"gnome" ]; then
# gnome-default-applications-properties is only available in GNOME 2.x
# but not in GNOME 3.x
which gnome-default-applications-properties > /dev/null 2>&1 || DE="gnome3"
fi
echo $DE
}
if [ $# -eq 0 ]; then
# The mkt parameter determines which Bing market you would like to
# obtain your images from.
mkt="zh-CN"
# Try and guess language
ML=$(echo $LANG | cut -f 1 -d .)
case $ML in
'en_US')
mkt="en-US"
;;
'zh_CN')
mkt="zh-CN"
;;
'ja_JP')
mkt="ja-JP"
;;
'en_AU')
mkt="en-AU"
;;
'en_UK')
mkt="en-UK"
;;
'de_CH')
mkt="de-DE"
;;
'de_DE')
mkt="de-DE"
;;
'en_NZ')
mkt="en-NZ"
;;
'en_CA')
mkt="en-CA"
;;
esac
exitAfterRunning=false
elif [ $# -eq 2 ]; then
# Valid values are:
declare -a list=("en-US" "zh-CN" "ja-JP" "en-AU" "en-UK" "de-DE" "en-NZ" "en-CA")
firstpar=$1
# inhibit code injection
firstpar=${firstpar//[^a-zA-Z-]/}
if [ $(contains "${list[@]}" $firstpar) == "y" ]; then
mkt=$firstpar
else
echo "mkt must be one of the following:"
printf '%s\n' "${list[@]}"
exit 1
fi
if [ "$2" = true ]; then
exitAfterRunning=true
else
exitAfterRunning=false
fi
else
echo "Usage: `basename $0` mkt[en-US,zh-CN,ja-JP,en-AU,en-UK,de-DE,en-NZ,en-CA] exitAfterRunning[true,false]"
exit 1
fi
# $bing is needed to form the fully qualified URL for
# the Bing pic of the day
bing="www.bing.com"
# The idx parameter determines where to start from. 0 is the current day,
# 1 the previous day, etc.
idx="0"
# $xmlURL is needed to get the xml data from which
# the relative URL for the Bing pic of the day is extracted
xmlURL="http://www.bing.com/HPImageArchive.aspx?format=xml&idx=$idx&n=1&mkt=$mkt"
# $saveDir is used to set the location where Bing pics of the day
# are stored. $HOME holds the path of the current user's home directory
saveDir=$HOME'/Pictures/Bing/'
# Create saveDir if it does not already exist
mkdir -p $saveDir
# Set picture options
# Valid options are: none,wallpaper,centered,scaled,stretched,zoom,spanned
picOpts="stretched"
# The file extension for the Bing pic
picExt=".jpg"
# Download the highest resolution
while true; do
for picRes in _1920x1200 _1366x768 _1280x720 _1024x768; do
# Extract the relative URL of the Bing pic of the day from
# the XML data retrieved from xmlURL, form the fully qualified
# URL for the pic of the day, and store it in $picURL
picURL=$bing$(echo $(curl -s $xmlURL) | egrep -o "(.*) " | cut -d ">" -f 2 | cut -d "<" -f 1)$picRes$picExt
# $picName contains the filename of the Bing pic of the day
# picName=${picURL##*/}
picName="bing_wallpaper_$$$picExt"
# Download the Bing pic of the day
curl -s -o $saveDir$picName -L "$picURL"
# Test if download was successful.
downloadResult=$?
if [ $downloadResult -ge 1 ]; then
rm -rf $saveDir$picName && continue
fi
if [ -x "/usr/bin/convert" ]; then
title=$(echo $(curl -s $xmlURL) | egrep -o "(.*) " | cut -d ">" -f 2 | cut -d "<" -f 1 )
convert -background "#00000080" -fill white -gravity center -size 1024 -font "Droid Sans" -pointsize 22 caption:"${title}" $saveDir$picName +swap -gravity south -composite $saveDir$picName
fi
# Test if it's a pic
file --mime-type -b $saveDir$picName | grep "^image/" > /dev/null && break
rm -rf $saveDir$picName
done
if [ $downloadResult -ge 1 ]; then
echo "Failed to download any picture."
echo "Try again in 60 seconds."
sleep 60
continue
fi
DE=$(detectDE)
if [ "$DE" = "cinnamon" ]; then
# Set the Cinnamon wallpaper
DISPLAY=:0 GSETTINGS_BACKEND=dconf gsettings set org.cinnamon.desktop.background picture-uri '"file://'$saveDir$picName'"'
# Set the Cinnamon wallpaper picture options
DISPLAY=:0 GSETTINGS_BACKEND=dconf gsettings set org.cinnamon.desktop.background picture-options $picOpts
elif [ "$DE" = "gnome" ]; then
# Set the GNOME 2 wallpaper
gconftool-2 -s -t string /desktop/gnome/background/picture_filename "$saveDir$picName"
# Set the GNOME 2 wallpaper picture options
gconftool-2 -s -t string /desktop/gnome/background/picture_options "$picOpts"
elif [ "$DE" = "gnome3" ]; then
# Set the GNOME3 wallpaper
DISPLAY=:0 GSETTINGS_BACKEND=dconf gsettings set org.gnome.desktop.background picture-uri '"file://'$saveDir$picName'"'
# Set the GNOME 3 wallpaper picture options
DISPLAY=:0 GSETTINGS_BACKEND=dconf gsettings set org.gnome.desktop.background picture-options $picOpts
gsettings set org.gnome.desktop.background picture-uri '"file://'$saveDir$picName'"'
elif [ "$DE" = "kde" ]; then
test -e /usr/bin/xdotool || sudo zypper --no-refresh install xdotool
test -e /usr/bin/gettext || sudo zypper --no-refresh install gettext-runtime
./kde4_set_wallpaper.sh $saveDir$picName
elif [ "$DE" = "lxqt" ] ; then
pcmanfm-qt -w $saveDir$picName
elif [ "$DE" = "xfce" ]; then
./xfce4_set_wallpaper.sh $saveDir$picName
elif [ "$DE" = "mac" ]; then
fn=$saveDir$picName
osascript -e 'tell application "Finder" to set desktop picture to POSIX file "'"$fn"'"'
osaResult=$?
sqlite3 ~/Library/Application\ Support/Dock/desktoppicture.db "update data set value = '$fn'" && killall Dock
sqliteResult=$?
if [ $osaResult -ge 1 -a $sqliteResult -ge 1 ]; then
echo "Failed to refresh desktop image."
echo "Try again in 60 seconds."
sleep 60
continue
fi
fi
if [ "$exitAfterRunning" = true ] ; then
# Exit the script
exit 0
fi
# sleep for half a day
DIFF_TIME=0
LAST_RUN=$(date +%s)
while [ $DIFF_TIME -lt 43199 ]; do
NOW=$(date +%s)
DIFF_TIME=$(expr $NOW - $LAST_RUN)
sleep 60
done
done
In Ubuntu /bin/bash links to dash. For me the scripts only work if I change the first line to /bin/bash. If one runs the script as sh in Ubuntu there are errors on some specific bash syntax like the double square brackets for conditional statements.