Random wallpaper from socwall.com 1

Posted by Brad Mon, 01 Sep 2008 08:10:00 GMT

I quite like http://socwall.com. They have a rss feed for recent posts, but I wrote a small script to download a random selection instead.

On OSX I could then set my desktop background to be a random picture from a folder. In my case that folder was /Users/brad/Pictures/Wallpapers/ but that could be easily changed.

The code is after the jump.

#!/usr/bin/ruby

require 'rubygems'
require 'hpricot'
require 'open-uri'
require 'Pathname'
 
URL = 'http://socwall.com/browse/index.php?wpSortby=8'
DEST = "/Users/brad/Pictures/Wallpapers/"


def clear_old_images
  Pathname.new(DEST).children.each { |f| f.delete }
end

def download_images(count)
  count = count.to_i
  
  while count > 0
    doc = Hpricot(open(URL))
    
    doc.search("div.wpThumbnail").each do |thumb|
      next if count == 0
      
      thumbnail = thumb.at("img")['src']
      image = thumbnail.gsub(/\/tb_/, '/')
      image = image.gsub(" ", "%20")
      
      remote_image = open(image, "User-Agent" => "Ruby/#{RUBY_VERSION}")
      local_image = open("#{ DEST }/#{ count }.png", 'w')
      local_image.write(remote_image.read)
      
      remote_image.close
      local_image.close
      
      count -= 1
    end
  end
end

count = ARGV[0] || 10

clear_old_images
download_images(count)

Trackbacks

Use the following link to trackback from your own site:
http://blog.lucky-dip.net/articles/trackback/13

Comments

Leave a response

  1. Monkey about 1 month later:
    Hi Brad, This code is awesome and I am loving my new random wallpaper. My favourite is this one of camels. Have you seen that? Man, I hope you love camels too. I have been a fan of lucky-dip.net since 2003. Throughout this time, I have found it funny, fresh, exciting and informative all at once. Could you please post on your blog more often? Holding my breath between each post is getting harder and harder. Thanks, Monkey
Comments