Skip to content

Instantly share code, notes, and snippets.

@ruslanpa
Last active March 6, 2016 00:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruslanpa/9239365 to your computer and use it in GitHub Desktop.
Save ruslanpa/9239365 to your computer and use it in GitHub Desktop.
Simple download manager for mangareader.net
require 'open-uri'
require 'nokogiri'
module Mangareader
module_function
BASE_URL = "http://mangareader.net"
# Example:
# => Mangareader.download "bleach", 552
# => Mangareader.download "bleach", 552, test_directory
#
# Arguments:
# => name: (String)
# => chapter: (Integer)
# => dir: (String)
def download(name, chapter, dir = [name, chapter].join('_'))
puts "Runnig..."
document = open_ [BASE_URL, name, chapter].join(File::SEPARATOR)
if document
document.css('#pageMenu option').each do |page|
current_page = open_ BASE_URL + page['value']
current_page.css('table div a img').each do |image|
puts "Saving ---> #{image['src']}"
save image['src'], dir
end
end
end
puts "Complete!"
end
def open_(url)
document = nil
begin
document = Nokogiri::HTML(open(url))
rescue Exception => message
puts message
end
document
end
def save(src, dir)
create_destination_folder dir
File.open(File.join(dir, File.basename(src)), 'wb') do |file|
file.write(open(src).read)
end
end
def create_destination_folder(dir)
FileUtils.makedirs dir unless File.exist?(dir)
end
private :open_, :save, :create_destination_folder
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment