Skip to content

Instantly share code, notes, and snippets.

@evanwalsh
Created Aug 21, 2012
Embed
What would you like to do?
Download an imgur.com gallery with Ruby and Nokogiri
#!/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."
@emad-elsaid

This comment has been minimized.

Copy link

@emad-elsaid emad-elsaid commented May 9, 2014

you can download imgur image from link if you appended '.jpg' to the end of the link,
so you can use the following snippit

def download_imgur( url, filename )
  image = open( "#{url}.jpg" ).read
  File.write("#{filename}.jpg", image)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment