Intro

Great Mike, another webscraping blog…

Yeah, bite me.

Anyways, I got sick of looking at a boring wallpaper, and I got sick of downloading new wallpapers for my desktop. Bing has a great daily image that appears on their page… every day. It’s usually some nature thing, or something otherwise visually interesting. Today, as I write this, it’s some fanc looking birds taking off from a mossy rock. Cool.

Anyways, as you’ve probably guessed, I wrote up some automation with Ruby and Mechanize to download this file everyday and make it my wallpaper on my Linux desktop. I’m going to show you how to do it.

Setup The Environment

All you really need (besides a working Ruby installation), is the mechanize gem. You can install it as such:

gem install mechanize

Nice, that was easy.

The Script

# frozen_string_literal: true

require 'mechanize'
require 'date'

TODAY_IMAGE_FILEPATH = "/home/michael/BingImages/#{Date.today}.jpg"

unless File.exist?(TODAY_IMAGE_FILEPATH)
  m = Mechanize.new
  p = m.get('https://bing.com')
  url = p.css('html head meta[property="og:image"]').attribute('content').value.gsub('tmb', '1920x1080')
  w = m.get(url)
  w.save(TODAY_IMAGE_FILEPATH)
end
#`feh --bg-fill "#{TODAY_IMAGE_FILEPATH}"`

Other Stuff

Now just change your TODAY_IMAGE_FILEPATH variable and run it! It will save a file with the name of today’s date. If the file already exists, it won’t download it.

At the bottom I commented out the feh command I use to set it as my desktop background. Feel free to use that or whatever else.

Also consider hooking it up to cron! Maybe don’t run it too close to midnight, I don’t know exactly when Bing refreshes the image.