Created
August 21, 2012 00:32
-
-
Save evanwalsh/3409862 to your computer and use it in GitHub Desktop.
Download an imgur.com gallery with Ruby and Nokogiri
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
#!/usr/bin/env ruby | |
# Download imgur.com galleries easily | |
# | |
# Requirements: | |
# gem install nokogiri | |
# | |
# Usage: | |
# ruby imgur.rb [url of gallery] [directory to download into] [namespace for files] | |
# | |
# Example: | |
# ruby imgur.rb http://imgur.com/a/npV94 battlefield3 scenery | |
# All files will be downloaded into battlefield3 with filenames in the pattern of | |
# scenery-[number].jpg | |
require 'nokogiri' | |
require 'open-uri' | |
url = URI.parse ARGV[0] | |
directory = ( defined?(ARGV[1]) ? ARGV[1] : File.basename(url.to_s) ) | |
namespace = ( defined?(ARGV[2]) ? ARGV[2] : File.basename(url.to_s) ) | |
page = Nokogiri::HTML(open url) | |
images = [] | |
page.search('.thumb-title').each do |image| | |
images << image.attributes['data-src'].value.gsub(/s.jpg/, '.jpg') | |
end | |
unless FileTest.directory? directory | |
puts "Creating #{directory}" | |
Dir.mkdir directory | |
end | |
puts "Downloading images into #{directory}...\n\n" | |
images.each_with_index do |image, i| | |
puts "Downloading #{image} as #{namespace}-#{i}.jpg..." | |
file = open(image) | |
File.open("#{directory}/#{namespace}-#{i}.jpg", 'w') do |f| | |
f.write file.read | |
end | |
end | |
puts "\nDone." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
you can download imgur image from link if you appended '.jpg' to the end of the link,
so you can use the following snippit