Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #--------------------#
- # REQUIREMENTS
- # * Ubuntu (or any GNOME 3 desktop environment?)
- # * ImageMagick
- # * Ruby
- # * Gems
- # * rmagick
- # * twitter
- #
- # Best run in screen
- #--------------------#
- #--------------------#
- # wallpaper.rb
- #--------------------#
- require "open-uri"
- require 'rmagick' # https://github.com/rmagick/rmagick
- include Magick
- class Wallpaper
- # Set the wallpaper directory
- @directory = "#{Dir.home}/Pictures/wallpaper"
- # This sets the specified image as the wallpaper and, by default, tiles it. Use
- # $ gsettings range org.gnome.desktop.background picture-options
- # to see other options for centring/stretching the image. You can also specify a different source directory
- def self.update(filename,display="centered")
- color = average_color(filename)
- `gsettings set org.gnome.desktop.background picture-uri file://#{filename} && gsettings set org.gnome.desktop.background picture-options '#{display}' && gsettings set org.gnome.desktop.background primary-color '#{color}'`
- end
- # Download an image and annotate it with the Twitter handle, if provided
- def self.download(url,username=nil)
- # Get the file extension
- ext = url.to_s.split(".").last
- # Always call the file the same thing, so we overwrite it rather than having thousands of images saved
- target_location = "#{@directory}/mattpaper.#{ext}"
- begin
- # Download the source image to the specified destination
- open(url){|image|
- File.open(target_location,"wb") do |file|
- IO.copy_stream(image,file)
- end
- }
- # Put the user's name on the image as a watermark
- unless username.nil?
- img = ImageList.new(target_location)
- txt = Draw.new
- txt.annotate(img, 0,0,0,0, username){
- self.gravity = Magick::SouthEastGravity
- self.pointsize = 12
- self.fill = "#FFFFFF"
- self.fill_opacity(0.75)
- }
- img = img.write(target_location)
- end
- return target_location
- rescue Exception => e
- puts e.message
- end
- return false
- end
- # Calculate the average color of an image
- # http://blog.dcxn.com/2011/07/05/averaging-image-color-using-rmagick/
- def self.average_color(img)
- begin
- image = ImageList.new(img)
- total = 0
- avg = { :r => 0.0, :g => 0.0, :b => 0.0 }
- image.quantize.color_histogram.each do |c,n|
- avg[:r] += n * c.red
- avg[:g] += n * c.green
- avg[:b] += n * c.blue
- total += n
- end
- avg.each_key do |c|
- avg[c] /= total
- avg[c] = (avg[c] / QuantumRange * 255).to_i
- end
- return "##{avg[:r].to_s(16)}#{avg[:g].to_s(16)}#{avg[:b].to_s(16)}"
- rescue Exception => e
- puts e.message
- return "#000000"
- end
- end
- end
- #--------------------#
- # run.rb
- #--------------------#
- require "rubygems"
- require "twitter" # https://github.com/sferik/twitter
- load "./wallpaper.rb"
- # Visit http://apps.twitter.com to set up your API keys
- client = Twitter::Streaming::Client.new do |config|
- config.consumer_key = "YOUR_CONSUMER_KEY"
- config.consumer_secret = "YOUR_CONSUMER_SECRET"
- config.access_token = "YOUR_ACCESS_TOKEN"
- config.access_token_secret = "YOUR_ACCESS_TOKEN_SECRET"
- end
- attempts = 0
- begin
- attempts += 1
- # Get the live stream
- client.user do |object|
- # We only care about Tweets
- if object.is_a?(Twitter::Tweet)
- # Does this tweet contain an image?
- if object.media.any?
- # Get the image URL
- image = object.media[0].media_url
- # Download the image
- paper = Wallpaper.download(image,object.user.screen_name)
- # Update the wallpaper, if it saved correctly
- if paper
- puts "#{Time.now.strftime("%Y-%m-%d %H:%M:%S")} -- #{image} from #{object.user.screen_name}"
- Wallpaper.update(paper)
- end
- end
- end
- end
- rescue Twitter::Error::TooManyRequests => error
- # This bit should only happen if the Access Tokens above are already in use
- puts "#{Time.now.strftime("%H:%M:%S")} -- Attempt #{attempts} failed"
- if attempts % 3 == 0
- puts "#{Time.now.strftime("%H:%M:%S")} -- Waiting 15 minutes..."
- sleep(15*60)
- end
- retry
- end
Advertisement
Add Comment
Please, Sign In to add comment