Skip to content

Instantly share code, notes, and snippets.

@mtrovo
Last active January 1, 2016 13:09
Show Gist options
  • Save mtrovo/8149805 to your computer and use it in GitHub Desktop.
Save mtrovo/8149805 to your computer and use it in GitHub Desktop.
Wallpaper downloader for Ubuntu to the last Macanudo comics from the website http://www.macanudo.com.ar. Tested in Ubuntu 13.04. Run with --help to see some more options.
#!/usr/bin/env ruby
require 'open-uri'
require 'nokogiri'
require 'RMagick'
include Nokogiri
include Magick
def latest_comic
day=Date.today
dom=nil
today_str=nil
loop do
today_str=day.strftime("%Y-%m-%d")
url="http://www.macanudo.com.ar/#{today_str}"
dom=Nokogiri.parse(open(url))
break if dom.css('.nohaymacanudo')
day=day - 1
end
build_wallpaper(dom, today_str)
end
def random_comic
first_date=Date.new(2008,06,28)
delta_days=(Date.today-first_date).to_i
day_str=nil
dom=nil
loop do
day=first_date + Random.rand(delta_days)
day_str=day.strftime("%Y-%m-%d")
url="http://www.macanudo.com.ar/#{day_str}"
dom=Nokogiri.parse(open(url))
break if dom.css('.nohaymacanudo')
end
puts "Setting wallpaper to macanudo from #{day_str}"
build_wallpaper(dom, day_str)
end
def from_date date
url="http://www.macanudo.com.ar/#{date}"
dom=Nokogiri.parse(open(url))
if dom.css('.nohaymacanudo')
puts 'There is no comic for this date :('
exit 1
end
end
def build_wallpaper dom, today_str
comic_src=dom.css('td[colspan~=\'2\']>img')[0]['src']
header_src=dom.css('a>img')[0]['src']
comic=ImageList.new(comic_src)
header=ImageList.new(header_src)
# get first screen size
scrx, scry = `xrandr`.scan(/connected (\d+)x(\d+)\+0\+0/).flatten.map{|s|s.to_i}
# compose wallpaper
wallpaper=ImageList.new
wallpaper.new_image(scrx, scry){ self.background_color = "white" }
wallpaper.composite!(comic, Magick::CenterGravity,Magick::AtopCompositeOp)
wallpaper.composite!(header, Magick::SouthEastGravity,Magick::AtopCompositeOp)
FileUtils.mkdir_p File.expand_path('~/.macanudos')
filename=File.expand_path("~/.macanudos/#{today_str}.png")
wallpaper.write filename
system("gsettings set org.gnome.desktop.background picture-uri 'file://#{filename}'")
end
require 'trollop'
opts = Trollop::options do
opt :random, "Random image"
opt :date, "Image from date (format: YYYY-MM-DD)", :type => :string
end
if opts[:random]
random_comic
elsif opts[:date]
from_date opts[:date]
else
latest_comic
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment