demondownload

Ubuntu Twitter Wallpaper Updater (v3)

Aug 17th, 2015
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.25 KB | None | 0 0
  1. #--------------------#
  2. # REQUIREMENTS
  3. # * Ubuntu (or any GNOME 3 desktop environment?)
  4. # * ImageMagick
  5. # * Ruby
  6. # * Gems
  7. #   * rmagick
  8. #   * twitter
  9. #
  10. # Best run in screen
  11. #--------------------#
  12.  
  13. #--------------------#
  14. # wallpaper.rb
  15. #--------------------#
  16. require "open-uri"
  17. require 'rmagick' # https://github.com/rmagick/rmagick
  18. include Magick
  19.  
  20. class Wallpaper
  21.   # Set the wallpaper directory
  22.   @directory = "#{Dir.home}/Pictures/wallpaper"
  23.  
  24.   # This sets the specified image as the wallpaper and, by default, tiles it. Use
  25.   #   $ gsettings range org.gnome.desktop.background picture-options
  26.   # to see other options for centring/stretching the image. You can also specify a different source directory
  27.   def self.update(filename,display="centered")
  28.     color = average_color(filename)
  29.     `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}'`
  30.   end
  31.  
  32.   # Download an image and annotate it with the Twitter handle, if provided
  33.   def self.download(url,username=nil)
  34.     # Get the file extension
  35.     ext = url.to_s.split(".").last
  36.     # Always call the file the same thing, so we overwrite it rather than having thousands of images saved
  37.     target_location = "#{@directory}/mattpaper.#{ext}"
  38.  
  39.     begin
  40.       # Download the source image to the specified destination
  41.       open(url){|image|
  42.         File.open(target_location,"wb") do |file|
  43.           IO.copy_stream(image,file)
  44.         end
  45.       }
  46.  
  47.       # Put the user's name on the image as a watermark
  48.       unless username.nil?
  49.         img = ImageList.new(target_location)
  50.         txt = Draw.new
  51.         txt.annotate(img, 0,0,0,0, username){
  52.           self.gravity = Magick::SouthEastGravity
  53.           self.pointsize = 12
  54.           self.fill = "#FFFFFF"
  55.           self.fill_opacity(0.75)
  56.         }
  57.         img = img.write(target_location)
  58.       end
  59.  
  60.       return target_location
  61.     rescue Exception => e
  62.       puts e.message
  63.     end
  64.     return false
  65.   end
  66.  
  67.   # Calculate the average color of an image
  68.   # http://blog.dcxn.com/2011/07/05/averaging-image-color-using-rmagick/
  69.   def self.average_color(img)
  70.     begin
  71.       image = ImageList.new(img)
  72.       total = 0
  73.       avg = { :r => 0.0, :g => 0.0, :b => 0.0 }
  74.  
  75.       image.quantize.color_histogram.each do |c,n|
  76.         avg[:r] += n * c.red
  77.         avg[:g] += n * c.green
  78.         avg[:b] += n * c.blue
  79.         total += n
  80.       end
  81.  
  82.       avg.each_key do |c|
  83.         avg[c] /= total
  84.         avg[c] = (avg[c] / QuantumRange * 255).to_i
  85.       end
  86.  
  87.       return "##{avg[:r].to_s(16)}#{avg[:g].to_s(16)}#{avg[:b].to_s(16)}"
  88.     rescue Exception => e
  89.       puts e.message
  90.       return "#000000"
  91.     end
  92.   end
  93. end
  94.  
  95.  
  96. #--------------------#
  97. # run.rb
  98. #--------------------#
  99. require "rubygems"
  100. require "twitter" # https://github.com/sferik/twitter
  101. load "./wallpaper.rb"
  102.  
  103. # Visit http://apps.twitter.com to set up your API keys
  104. client = Twitter::Streaming::Client.new do |config|
  105.   config.consumer_key        = "YOUR_CONSUMER_KEY"
  106.   config.consumer_secret     = "YOUR_CONSUMER_SECRET"
  107.   config.access_token        = "YOUR_ACCESS_TOKEN"
  108.   config.access_token_secret = "YOUR_ACCESS_TOKEN_SECRET"
  109. end
  110.  
  111. attempts = 0
  112.  
  113. begin
  114.   attempts += 1
  115.   # Get the live stream
  116.   client.user do |object|
  117.     # We only care about Tweets
  118.     if object.is_a?(Twitter::Tweet)
  119.       # Does this tweet contain an image?
  120.       if object.media.any?
  121.         # Get the image URL
  122.         image = object.media[0].media_url
  123.         # Download the image
  124.         paper = Wallpaper.download(image,object.user.screen_name)
  125.         # Update the wallpaper, if it saved correctly
  126.         if paper
  127.           puts "#{Time.now.strftime("%Y-%m-%d %H:%M:%S")} -- #{image} from #{object.user.screen_name}"
  128.           Wallpaper.update(paper)
  129.         end
  130.       end
  131.     end
  132.   end
  133. rescue Twitter::Error::TooManyRequests => error
  134.   # This bit should only happen if the Access Tokens above are already in use
  135.   puts "#{Time.now.strftime("%H:%M:%S")} -- Attempt #{attempts} failed"
  136.   if attempts % 3 == 0
  137.     puts "#{Time.now.strftime("%H:%M:%S")} -- Waiting 15 minutes..."
  138.     sleep(15*60)
  139.   end
  140.   retry
  141. end
Advertisement
Add Comment
Please, Sign In to add comment