Created
April 25, 2013 09:15
-
-
Save clooth/5458540 to your computer and use it in GitHub Desktop.
mobile-patterns.com 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 'nokogiri' | |
require 'open-uri' | |
require 'net/http' | |
# Patterns | |
patterns = {} | |
# Load first page | |
doc = Nokogiri::HTML(open('http://www.mobile-patterns.com/')) | |
# Get all page links for later reference | |
pages_urls = [] | |
all_pages = doc.css('#sidebar li.sidebar-item a').each do |link| | |
link = link["href"] | |
pages_urls << "http://www.mobile-patterns.com#{link}" | |
end | |
pages_urls.each do |url| | |
doc = Nokogiri::HTML(open(url)) | |
doc.css('div.pattern').each do |pattern| | |
unless pattern.css('.tags a')[1].nil? | |
category = pattern.css('.tags a')[1].content | |
puts category | |
else | |
category = 'misc' | |
end | |
target_dir = Dir::pwd + "/" + category + "/" | |
unless patterns.has_key? category | |
patterns[category] = [] | |
Dir::mkdir target_dir unless File.exists?(target_dir) | |
end | |
image_url = pattern.at_css('a[rel=facebox]')["href"] | |
Thread.new { | |
open(image_url) do |f| | |
File.open(target_dir + File.basename(image_url), "wb") {|file| | |
file.puts f.read | |
u["content"] = true | |
puts "got #{image_url}" | |
if urls.all? {|u| u.has_key?("content") } | |
puts "Got all" | |
exit | |
end | |
} | |
end | |
} | |
end | |
end | |
sleep |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment