Skip to content

Instantly share code, notes, and snippets.

@kyktommy
Created November 9, 2013 11:38
Show Gist options
  • Save kyktommy/7384565 to your computer and use it in GitHub Desktop.
Save kyktommy/7384565 to your computer and use it in GitHub Desktop.
Behance Project Images downloader `ruby app.rb http://www.behance.net/gallery/Web-SPIT-Mx/10331815 ~/Desktop`
require 'nokogiri'
require 'open-uri'
class ImageFetcher
EXCLUDES = [
"http://a2.behance.net/img/site/grey.png"
]
def self.get_all_img_src(url)
doc = Nokogiri::HTML(open(url))
imgs = doc.css('.module img').map do |img|
img.attributes['src'].value
end
return imgs - EXCLUDES
end
end
## Begin Main App
url = ARGV[0]
dir_path = ARGV[1] || "projects"
puts "Begin Download"
imgs = ImageFetcher.get_all_img_src(url)
puts "Create Folder"
project_path = File.join [dir_path, url.split('/')[-2]]
[dir_path, project_path].each do |dir|
Dir.mkdir(dir) unless File.exists?(dir)
end
puts "Downloading Images"
imgs.each do |img|
file_name = img.split('/').last
file_path = File.join([project_path, file_name])
content = open(img).read
File.write(file_path, content)
end