在Linux没找到合适的壁纸软件,于是估摸着自己简单实现了一个。

  • 功能由三部分实现:

  • 效果
  • 源码如下 (相当之简陋 勉强能用)

    #!/usr/bin/env ruby
    # encoding:utf-8
    # -*- coding: UTF-8 -*-
    
    require 'fileutils'
    
    # 壁纸更换间隔
    DURA = 30 * 60
    IMGS = 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}`.chomp
    puts ">>> bg [#{file}] fetch by #{script[1]}"
    return file
    end
    
    def 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 = nName
    end
    return img_path
    end
    
    def get_background_image(fetch_type)
    VALS[:COUNT_FETCH] += 1
    fsca = 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_type
    when 'RANDOM'
    fsca[ rand fsca.size ]
    when 'INDEX'
    VALS[:INDEX_FETCH] += 1
    fsca[ VALS[:INDEX_FETCH] % fsca.size ]
    else
    fsca[ rand fsca.size ]
    end, IMG, VALS[:COUNT_FETCH]
    end
    
    def 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 do
    loop 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 1
    end
    end
    
    # 间隔时间后 拉取新壁纸
    loop do
    sleep 7 if Time.now.sec == 00
    file = get_background_image FETCH
    FileUtils.copy_file file, CBG if not file.nil?
    next if not File.exist? CBG
    
    sleep DURA
    end
  • 时间脚本

    #!/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" $2
    convert $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.jpg
    echo $1.jpg

    或者

    #!/usr/bin/env zsh
    # $1 存放路径
    # $2 当前脚本的调用次数
    
    echo "/home/user/Pictures/background.png"
  • 优点

  • 已知问题

03-05 15:41