Last active
February 3, 2023 12:39
-
-
Save falsefalse/882022 to your computer and use it in GitHub Desktop.
http://simpledesktops.com/ wallpaper downloader
This file contains hidden or 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
require 'hpricot' | |
require 'open-uri' | |
require 'active_support/core_ext/numeric/conversions' | |
@base = "http://simpledesktops.com" | |
def scrape_page(url, page_number = 0) | |
Dir.mkdir "#{page_number}" unless File.exists? "#{page_number}" | |
doc = Hpricot( open( url ) ) | |
hrefs = ( doc/".desktop > a" ).map { |a| a.attributes["href"] } | |
puts "Scraping #{url} ..." | |
total_size = 0 | |
total = hrefs.size | |
done = 0 | |
threads = hrefs.map do |href| | |
Thread.new do | |
name = "#{File.basename href}.png" | |
path = File.join(page_number.to_s, name) | |
if (File.exists?(path)) | |
done += 1 | |
next | |
end | |
( Hpricot( open( @base + href ) )/".desktop > a" ).each do |a| | |
open( @base + a.attributes["href"] ) do |image| | |
File.open( path, "wb" ) do |f| | |
f.write image.read | |
done += 1 | |
total_size += image.size | |
print " %2d/%2d %5.1f%% %8s \r" % [done, total, done.to_f / total.to_f * 100.0, total_size.to_s(:human_size)] | |
end | |
end | |
end | |
end | |
end | |
threads.map &:join | |
puts "" | |
next_page = ( doc/".back" ) | |
return if next_page.size === 0 | |
next_page_url = @base + next_page[0].attributes["href"] | |
page_number += 1 | |
scrape_page next_page_url, page_number | |
end | |
puts "Starting..." | |
scrape_page @base + '/browse' |
@Bomp right, but I think it should work on Mac OS already. I might try it inside VM, but this is sorta hassle now :) I would be glad if someone try to run it on Mac OS
excellent... and how can I use this downloader?
Check that you have ruby, rubygems and hpricot gem installed, if something is missing install it.
Then
$ git clone git://gist.github.com/882022.git simpled
$ cd simpled
$ ruby simpledesktops_scraper.rb
You propose to setup a whole infrastructure to use just one little downloader :-)
Why, yes I am. Don't like it, don't buy it, it's small utility thing after all, everybody can write their own for theirs infra, amirite?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
path = "#{page_number}/#{File.basename href}" == File.join(page_number, File.basename(href)) # crossplatform