在Linux没找到合适的壁纸软件,于是估摸着自己简单实现了一个。功能由三部分实现:bgwall 软件主体,由 ruby 编写的脚本。调度图片获取脚本,以及壁纸更换。./fethch_script/ 存放获取图片的脚本。./post_script/ 存放对获取后的图片进行后期加工的脚本。效果源码如下 (相当之简陋 勉强能用)#!/usr/bin/env ruby# encoding:utf-8# -*- coding: UTF-8 -*-require 'fileutils'# 壁纸更换间隔DURA = 30 * 60IMGS = 2# 图片脚本调用方式 INDEX 顺序; RANDOM 随机FETCH = 'INDEX'VALS = { :INDEX_FETCH => 0, :COUNT_FETCH => 0 }# 图片临时存放路径IMG = '/tmp/bgwall/'FSC = './fetch_script/'PSC = './post_script/'CBG = "#{IMG}/bgwall_current.png"NBG = "#{IMG}/bgwall_next.png"def call_bg_script(script, img_dir, count)return nil if script.nil?file = `#{script[0]} #{img_dir}/#{script[1]}_#{count} #{count}`.chompputs ">>> bg [#{file}] fetch by #{script[1]}"return fileenddef call_post_script(img_path)Dir::open(PSC).filter{|f| f.start_with? /\d+/ and File.executable? PSC + f }.sort.map{|v| [PSC + v, v] }.each_with_index do |script,index| nName = "#{IMG}#{index}_#{File.basename(img_path)}" FileUtils.copy_file img_path, nName system("#{script[0]} #{img_path} #{nName}") img_path = nNameendreturn img_pathenddef get_background_image(fetch_type)VALS[:COUNT_FETCH] += 1fsca = Dir::open(FSC).filter{|f| f.start_with? /\d+/ and File.executable? FSC + f }.sort.map{|v| [FSC + v, v] }return nil if fsca.empty?call_bg_script case fetch_typewhen 'RANDOM'fsca[ rand fsca.size ]when 'INDEX'VALS[:INDEX_FETCH] += 1fsca[ VALS[:INDEX_FETCH] % fsca.size ]elsefsca[ rand fsca.size ]end, IMG, VALS[:COUNT_FETCH]enddef set_background_image(img_path)# 根据不同的环境 使用不同的 工具#system("hsetroot -fill #{img_path}")puts ">>> set background #{img_path} [#{Time.now}]\n\n"system("feh --bg-scale #{img_path}")end`[[ -d #{IMG} ]] || mkdir #{IMG}; echo $(date) > #{PSC}/start && echo $(date) > #{FSC}/start; sleep 2`# 每分钟刷新一次桌面背景 主要更新图片处理进本的操作,比如刷新显示时间Thread::new doloop do if Time.now.sec == 00 next if not File.exist? CBG if File.exist? NBG set_background_image NBG else set_background_image CBG end post_bg = call_post_script CBG FileUtils.copy_file post_bg, NBG end sleep 1endend# 间隔时间后 拉取新壁纸loop dosleep 7 if Time.now.sec == 00file = get_background_image FETCHFileUtils.copy_file file, CBG if not file.nil?next if not File.exist? CBGsleep DURAend时间脚本#!/usr/bin/env zsh# $1 源图片路径# $2 存放路径_dz=("子" "丑" "寅" "卯" "辰" "巳" "午" "未" "申" "酉" "戌" "亥")now=$(date "+%H")dz=$_dz[$[ ($now + 1) / 2 % 12 + 1]]" 時"now=$now":"$[ $(date "+%M") + 1]convert $1 -font Noteworthy-Bold.ttf -gravity SouthEast -pointsize 200 -fill white -annotate +100+100 "$now" $2convert $2 -font /usr/share/fonts/wps-office/FZLSK.TTF -gravity SouthEast -pointsize 200 -fill white -annotate +100+500 "$dz" $2拉取脚本#!/usr/bin/env zsh# $1 存放路径# $2 当前脚本的调用次数base_page="https://wallhere.com/zh/wallpapers?q=狗&page=$2&pageSize=1&format=json"data_id=$(curl $base_page | jq .data | sed -e 's/\\r\\n//g' -e 's/\\//g' | grep -Po '(?<=data-id=")(\d+?)(?=")' | head -n1)img_url="https://wallhere.com/zh/wallpaper/$data_id"img_src=$(curl $img_url | grep current-page-photo | grep -Po '(?<=href\=")(.*?)(?=")')wget $img_src -O $1.jpgecho $1.jpg或者#!/usr/bin/env zsh# $1 存放路径# $2 当前脚本的调用次数echo "/home/user/Pictures/background.png"优点图片来源容易扩展,只要会一门语言,简单爬取就行已知问题刚运行时 不会设置图片,在得到图片后的下一分钟更新壁纸时才有。
