Forked from emad-elsaid/gtk2_live_wallpaper_from_reddit.rb
Last active
August 29, 2015 14:01
-
-
Save otruffer/5b44ac660de10e9f5a57 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# OP | |
# Author : Emad Elsaid (https://github.com/blazeeboy) | |
# Added backup. | |
# Author : Oskar Truffer (https://github.com/otruffer) | |
# | |
# autochanging wallpaper from reddit images | |
# you have to point the script to your existing | |
# file you set it as a wallpaper and it will override | |
# it with a new image every 5 minutes. | |
# | |
# this script works on centos 6 with GNOME/GTK2 interface | |
require 'open-uri' # we'll need to download image with that | |
require 'ruby_reddit_api' # gem install ruby_reddit_api | |
# class will be initialized with | |
# a subreddit to monitor and a | |
# destination file to write the | |
# downloaded image to it | |
class RedditWallpaper | |
def initialize( subreddit, destination_file = 'img/bg.jpg', backup_folder = '' ) | |
@subreddit = subreddit | |
@path = destination_file | |
@downloaded = [] | |
@not_downloaded = [] | |
@backup_folder = backup_folder | |
end | |
# download the url to destination | |
def download( url ) | |
image = open( "#{url}.jpg" ).read | |
File.write @path, image | |
#if a backup folder is specified we save a backup of the wallpaper | |
unless @backup_folder == '' | |
backup image | |
end | |
end | |
# add a copy of the image in the backup folder | |
def backup( image ) | |
md5 = Digest::MD5.hexdigest(image) | |
backup_file = File.join(@backup_folder, md5.to_s + '.jpg') | |
File.open(backup_file, "w+") do |f| | |
f.write(image) | |
end | |
end | |
# update wallpaper and update images cache | |
def update | |
# make me a reddit client please | |
r = Reddit::Api.new | |
# update earth | |
posts = r.browse @subreddit | |
posts.each do |r| | |
@not_downloaded << r.url if r.url.include?('imgur') and !@downloaded.include?(r.url) | |
end | |
image = @not_downloaded.shift | |
download image | |
end | |
end | |
# i'll get images from earthporn | |
# they have lots of great images of | |
# nature places | |
# and then i'll update it every 5 minutes | |
downloader = RedditWallpaper.new 'earthPorn', '/home/vagrant/ruby/wallpaperchanger/img/bg.jpg', '/home/vagrant/Pictures/Wallpapers' | |
loop do | |
downloader.update | |
sleep 5*60 # wait for 5 minutes | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment