Last active
March 6, 2016 00:39
-
-
Save ruslanpa/9239365 to your computer and use it in GitHub Desktop.
Simple download manager for mangareader.net
This file contains 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 '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