demondownload

Update Ubuntu wallpaper based on Tweet content

Jul 31st, 2015
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 1.88 KB | None | 0 0
  1. class Wallpaper
  2.   # This sets the specified image as the wallpaper and, by default, tiles it. Use
  3.   #   $ gsettings range org.gnome.desktop.background picture-options
  4.   # to see other options for centring/stretching the image. You can also specify a different source directory
  5.   def self.update(filename,display="wallpaper",dir="file:///path/to/wallpaper")
  6.     `gsettings set org.gnome.desktop.background picture-uri #{dir}/#{filename} && gsettings set org.gnome.desktop.background picture-options "#{display}"`
  7.   end
  8. end
  9.  
  10. # This is where the fun bit starts
  11. require "rubygems"
  12. require "twitter" # https://github.com/sferik/twitter
  13.  
  14. # Visit http://apps.twitter.com to set up your API keys
  15. client = Twitter::Streaming::Client.new do |config|
  16.   config.consumer_key        = "YOUR_CONSUMER_KEY"
  17.   config.consumer_secret     = "YOUR_CONSUMER_SECRET"
  18.   config.access_token        = "YOUR_ACCESS_TOKEN"
  19.   config.access_token_secret = "YOUR_ACCESS_SECRET"
  20. end
  21.  
  22. users = ["19455489"] # Just Matt, for the purposes of this script
  23. attempts = 0
  24.  
  25. begin
  26.   attempts = 1
  27.   # Get the live stream for the relevant users
  28.   client.filter(follow: users.join(",")) do |object|
  29.     # We only care about tweets
  30.     if object.is_a?(Twitter::Tweet)
  31.       # This is so we don't trigger on replies/mentions etc.
  32.       if object.user.screen_name.downcase == "jam_sponge"
  33.         # Now we want to make sure this is one of the trigger phrases
  34.         if object.text.include?("GOOD MORNING")
  35.           Wallpaper.update("morning.png")
  36.         elsif object.text.include?("GOOD NIGHT")
  37.           Wallpaper.update("night.jpg")
  38.         end
  39.       end
  40.     end
  41.   end
  42. rescue Twitter::Error::TooManyRequests => error
  43.   puts "#{Time.now.strftime("%H:%M:%S")} -- Attempt #{attempts} failed"
  44.   if attempts % 3 == 0
  45.     puts "#{Time.now.strftime("%H:%M:%S")} -- Waiting 15 minutes..."
  46.     sleep(15*60)
  47.   end
  48.   retry
  49. end
Advertisement
Add Comment
Please, Sign In to add comment