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' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
^_^