Skip to content

Instantly share code, notes, and snippets.

@youthlin
Created September 26, 2018 02:25
Show Gist options
  • Save youthlin/bdf3f9751dc5c554031048b383157687 to your computer and use it in GitHub Desktop.
Save youthlin/bdf3f9751dc5c554031048b383157687 to your computer and use it in GitHub Desktop.
BingWallpaper-xfce4
#!/bin/bash
#by youthlin.chen https://youthlin.com
dir=`pwd`
file=`curl -I "https://cn.bing.com/ImageResolution.aspx?w=1920&h=1080" 2>/dev/null | grep Location | awk '{print $2}'`
file=${file%_*}
file="${file}_1920x1080.jpg"
url="https://cn.bing.com$file"
file=${file##*/}
today=`date +%Y%m%d`
file="${today}_${file}"
curl $url >$file 2>/dev/null
/usr/bin/xfconf-query --channel xfce4-desktop --property /backdrop/screen0/monitor0/workspace0/last-image -s $dir/$file
/usr/bin/xfconf-query --channel xfce4-desktop --property /backdrop/screen0/monitor1/workspace0/last-image -s $dir/$file
notify-send -i $dir/bing-logo.png '壁纸已设置' $dir/$file
#bing-logo.png: http://pluspng.com/img-png/logo-bing-png-bing-icon-1600.png
@youthlin
Copy link
Author

youthlin commented Sep 28, 2018

bing-kde.sh

#!/bin/bash
logfile=/tmp/bing.tmp.log
log(){
  echo "`date +'%Y-%m-%d %H:%M:%S'`" $* >> $logfile
}

#https://medium.com/@krish.raghuram/change-the-desktop-background-based-on-battery-status-9662b587c786
export DISPLAY=:0
export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1001/bus

export PATH="/usr/local/bin:$PATH"

env >> $logfile
log "start bing-kde..."
log "path=$PATH"  "curl=`which curl`" "notify-send=`which notify-send`" "dbus-send=`which dbus-send`"
log "DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS"

dir=$(cd `dirname $0`; pwd)

log "dir=$dir"
today=`date +%Y%m%d`
file="${dir}/${today}_1920x1080.jpg"

log '执行:curl -L "https://cn.bing.com/ImageResolution.aspx?w=1920&h=1080" >$file 2>/dev/null'
log "save file: " $file

curl -L "https://cn.bing.com/ImageResolution.aspx?w=1920&h=1080" >$file 2>/dev/null

dbus-send --session --dest=org.kde.plasmashell --type=method_call /PlasmaShell org.kde.PlasmaShell.evaluateScript 'string:
var Desktops = desktops();                                                       
for (i=0;i<Desktops.length;i++) {
        d = Desktops[i];
        d.wallpaperPlugin = "org.kde.image";
        d.currentConfigGroup = Array("Wallpaper", "org.kde.image", "General");
        d.writeConfig("Image", "file://'"$file"'");
}'

log '... bing-kde end'
notify-send -i $dir/bing-logo.png '壁纸已设置' $file

部件被锁定时不能更改!!
Can NOT set wallpaper when widgets are locked!!!

https://github.com/pashazz/ksetwallpaper/blob/master/ksetwallpaper.py
https://www.reddit.com/r/kde/comments/65pmhj/change_wallpaper_from_terminal/
curl -L 302 http://ju.outofmemory.cn/entry/95924

@youthlin
Copy link
Author

KDE 其实还可以 在桌面右键-配置桌面-壁纸-壁纸类型-每日一图-Bing

@youthlin
Copy link
Author

youthlin commented Oct 17, 2018

export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/`id -u`/bus

@youthlin
Copy link
Author

youthlin commented Aug 28, 2020

#!/bin/bash

PWD=$(
  cd "$(dirname "$0")" || exit
  pwd
)

# echo "$PWD"
mkdir -p "${PWD}/1920x1080"
mkdir -p "${PWD}/1080x1920"

# 最多可以获取的图片数量
MAX_COUNT=8

# 使用 curl 获取 json 数据
# curl -s 静默模式,结果是 json 给 python3 处理
json=$(/usr/bin/curl -s "https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=8")

# 检查每个图片
idx=0
while ((idx < MAX_COUNT)); do
  # 使用 python3 的 json 库,解析出图片地址
  url=$(echo "$json" | /usr/local/bin/python3 -c "import sys, json; print(json.load(sys.stdin)['images'][${idx}]['url'])")
  # url=/th?id=OHR.SailingStone_ROW7078458305_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp
  # %% 表示从尾部去掉所有匹配正则的部分,正则 &* 表示符号&后跟着任意字符串
  name=${url%%&*}

  # 解析出日期
  date=$(echo "$json" | /usr/local/bin/python3 -c "import sys, json; print(json.load(sys.stdin)['images'][${idx}]['startdate'])")

  # 判断改日期图片是否已下载
  if [ ! -e "${PWD}/1920x1080/${date}.jpg" ]; then
    # name=/th?id=OHR.SailingStone_ROW7078458305_1920x1080.jpg
    # echo "$date $name"
    /usr/bin/curl -s -o "${PWD}/1920x1080/${date}.jpg" "https://bing.com$name"
  fi

  # 判断竖版图片是否存在
  if [ ! -e "${PWD}/1080x1920/${date}.jpg" ]; then
    # echo "竖版: $date ${name/1920x1080/1080x1920}"
    /usr/bin/curl -s -o "${PWD}/1080x1920/${date}.jpg" "https://bing.com${name/1920x1080/1080x1920}"
  fi

  idx=$((idx + 1))
done

save as ~/Pictures/bing/bing.sh

EDITOR=vim crontab -e
 
输入并保存:
# Min Hour Day Month Week Cmd
0 8 * * * bash ~/Pictures/bing/bing.sh

https://youthlin.com/wallpaper#comment-2152

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment