Skip to content

Instantly share code, notes, and snippets.

@falsefalse
Last active February 3, 2023 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save falsefalse/882022 to your computer and use it in GitHub Desktop.
Save falsefalse/882022 to your computer and use it in GitHub Desktop.
http://simpledesktops.com/ wallpaper downloader
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'
@bopm
Copy link

bopm commented Mar 23, 2011

path = "#{page_number}/#{File.basename href}" == File.join(page_number, File.basename(href)) # crossplatform

@falsefalse
Copy link
Author

@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

@zbroyar
Copy link

zbroyar commented Mar 23, 2011

excellent... and how can I use this downloader?

@falsefalse
Copy link
Author

falsefalse commented Mar 23, 2011

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

@zbroyar
Copy link

zbroyar commented Mar 23, 2011

You propose to setup a whole infrastructure to use just one little downloader :-)

@falsefalse
Copy link
Author

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?

@falsefalse
Copy link
Author

falsefalse commented Feb 2, 2023

^_^

going fast

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment