From 70d268cb0bc336f027a09e6286615ca699be5dd2 Mon Sep 17 00:00:00 2001 From: Gardouille Date: Mon, 23 Apr 2018 08:28:14 +0200 Subject: [PATCH] Download scan from mangareader.net MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From : https://github.com/lukaszkorecki/mdl/blob/master/mdl.rb --- mangareader.net_download.rb | 62 +++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100755 mangareader.net_download.rb diff --git a/mangareader.net_download.rb b/mangareader.net_download.rb new file mode 100755 index 0000000..2f9f3ca --- /dev/null +++ b/mangareader.net_download.rb @@ -0,0 +1,62 @@ +#!/usr/bin/env ruby + +require 'rubygems' +require 'mechanize' +require 'fileutils' + +BROWSER = Mechanize.new { |agent| agent.user_agent_alias = "Mac Safari"} + +def cleanup! dir_name + include FileUtils + files = Dir["#{dir_name}/*.jpg"] + chapters = files.map { |f| f.split('/').last.split('Page').first.split('-').first.sub(/_$/,'') }.uniq + + puts "Moving pages into chapters\n #{chapters.join("\n\t")}" + + chapters.each do |chapter| + dir = "#{dir_name}/#{chapter}" + mkdir dir unless File.exists? dir + files.select { |file| file =~ /#{chapter}_/ }.each do |_file| + file = _file.split('/').last + mv _file, "#{dir_name}/#{chapter}/#{file}" + end + + end + +end +def getit! dir_name, target_url + puts ">>>> #{target_url}" + + begin + BROWSER.get(target_url) do |page| + next_url = "http://" + next_url << page.uri.host + next_url << page.search('.//div[@id="imgholder"]//a').attr('href').to_s + + url = page.search('.//div[@id="imgholder"]//img').attr('src').to_s + + name = (page.title.gsub(/[\ \/:]/, '_')+"."+url.split('.').last).gsub /[:\/]/,'' + + name = name =~ /Page\_\d\.jpg/ ? name.sub('Page_', 'Page_0') : name + + puts ">>>> Downloading #{name} ( #{url} )" + + BROWSER.get(url).save "#{dir_name}/#{name}" + + getit! dir_name, next_url + end + rescue => e + puts "Failed at #{target_url} - probably we reached the end!" + puts e.inspect unless ENV['DEBUG'].nil? + puts 'cleaning up' + cleanup! dir_name + exit 1 + end +end + +if ENV['CLEANUP'].nil? + puts "usage: \nruby mdl.rb ''" and exit 1 if ARGV.length < 2 + getit! ARGV[0], ARGV[1] +else + cleanup! ARGV[0] +end