require 'net/http' require 'pp' require 'rubygems' require 'xmlsimple' # desktop_width = 1280 desktop_height = 1024 num_desktops = 6 # target_ratio = desktop_width.to_f / desktop_height target_range = (target_ratio.floor)..(target_ratio.ceil) Photo = Struct.new(:farm, :sid, :id, :oid, :secret, :ext, :title, :width, :height, :ratio, :host, :path, :x_diff) params = [ [:api_key, '6efcde4b429a5569196d2a99a2669097'], [:method, 'flickr.interestingness.getList'], [:extras, 'o_dims,original_format,original_secret'], [:per_page, '250'] ] path = "/services/rest/?#{params.collect { |p| p.join("=") }.join("&")}" body = Net::HTTP.get("api.flickr.com", path) result = XmlSimple.xml_in(body)['photos'].first photos = result['photo'].collect do |photo| farm, sid, id, oid, secret, ext, title, w, h = photo.values_at(*[ 'farm', 'server', 'id', 'owner', 'originalsecret', 'originalformat', 'title', 'o_width', 'o_height' ]) p = Photo.new(farm, sid, id, oid, secret, ext, title, w, h) if p.width && p.height p.width = p.width.to_i p.height = p.height.to_i p.ratio = p.width.to_f / p.height p.x_diff = p.ratio != target_ratio ? (p.width - (target_ratio * p.height)).round : 0 end p end.select do |photo| if photo.width && photo.height && target_range.include?(photo.ratio) && (photo.width - photo.x_diff) > desktop_width photo.host = "farm#{photo.farm}.static.flickr.com" photo.path = "/#{photo.sid}/#{photo.id}_#{photo.secret}_o.#{photo.ext}" true else false end end index = 0 Dir.mkdir("images") unless File.exist?("images") num_desktops.times do |i| index = 0 if photos.length == index photo = photos[index] index += 1 # save image fn = File.expand_path("images/#{photo.id}.#{photo.ext}") puts "Downloading #{photo.title}..." File.open(fn, "w") { |f| f.write(Net::HTTP.get(photo.host, photo.path)) } if target_ratio != photo.ratio # crop image; x - 1.25y = w - 1.25h h = photo.height w = photo.width - photo.x_diff x_offset = photo.x_diff / 2 y_offset = 0 puts " Cropping..." cmd = "mogrify -crop #{w}x#{h}+#{x_offset}+#{y_offset} #{fn}" system(cmd) end # scale puts " Scaling..." `mogrify -scale #{desktop_width}x#{desktop_height} #{fn}` # set desktop puts " Setting to desktop #{i+1}..." `dcop kdesktop KBackgroundIface setWallpaper #{i+1} #{fn} 1` end