-
-
Save douglasdrumond/503034 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env ruby | |
# | |
# Put this script in your PATH and download from onemanga.com like this: | |
# onemanga_downloader.rb Bleach [chapter number] | |
# | |
# You will find the downloaded chapters under $HOME/Documents/OneManga/Bleach | |
# | |
# If you run this script without arguments, it will check your local manga downloads | |
# and check if there are any new chapters | |
# | |
# Updates | |
# 05/24 - taking into account redirection to 1000manga.com and age verification cookie | |
# 08/01 - (dfd) retry on error (sometimes a timeout occurs) | |
require 'rubygems' | |
require 'mechanize' | |
require 'nokogiri' | |
require 'open-uri' | |
manga_root = "http://www.onemanga.com/" | |
manga_download_folder = File.join(ENV['HOME'],"/Documents/OneManga/") | |
agent = Mechanize.new { |agent| agent.user_agent_alias = 'Mac Safari' } | |
if ARGV.size == 0 | |
# no args means just to check for new chapters | |
mangas = Dir.glob(File.join(manga_download_folder, "*")).map do |f| | |
f.gsub(manga_download_folder, '') | |
end | |
mangas.each do |manga_name| | |
downloaded_chapters = Dir.glob(File.join(manga_download_folder, manga_name, "*")).map do |f| | |
f.gsub(File.join(manga_download_folder, manga_name, "/"), "").to_i | |
end.sort | |
last_chapter = downloaded_chapters.last | |
# index page | |
agent.get(manga_root + manga_name) | |
# find chapter | |
chapters = agent.page.links.map do |l| | |
$1.to_i if l.href =~ /#{manga_name}\/(\d+)/ | |
end.compact.sort | |
most_recent_chapter = chapters.last | |
puts "#{last_chapter}/#{most_recent_chapter} - #{manga_name}" | |
end | |
exit 0 # go away | |
end | |
manga_name = ARGV.first || "Bakuman" | |
start_from_chapter = ARGV.size > 1 ? ARGV[1] : nil | |
manga_folder = File.join(manga_download_folder, manga_name) | |
puts "Creating #{manga_folder}" | |
FileUtils.mkdir_p(manga_folder) | |
# index page | |
agent.get(manga_root + manga_name) | |
# find chapter | |
chapter_link = agent.page.links.select do |l| | |
if start_from_chapter | |
l.href =~ /#{manga_name}\/#{start_from_chapter}\// | |
else | |
l.href =~ /#{manga_name}\/\d+/ | |
end | |
end.reverse.first | |
# click the chapter link in the index page | |
agent.click chapter_link | |
another_site = agent.page.link_with(:text => "Read this series at 1000manga.com") | |
if another_site | |
agent.get :url => another_site.href, :referer => agent.page, :headers => { "cookie" => "age_verified=30" } | |
end | |
# first time in a chapter starts with "Begin reading ..." | |
agent.get :url => agent.page.links.select { |l| l.text =~ /Begin/ }.first.href, :referer => agent.page, :headers => { "cookie" => "age_verified=30" } | |
chapter_number = nil | |
chapter_folder = "" | |
# go all the way. the navigation stop in the last chapter with a bookmark link | |
while (agent.page / "#id_bookmark_click").empty? | |
break if agent.page.forms.empty? | |
# create the chapter folder if it changes | |
current_chapter_number = agent.page.uri.to_s.split("/")[-2] # /[manga]/[chapter]/[page] | |
if chapter_number != current_chapter_number | |
chapter_number = current_chapter_number | |
chapter_folder = File.join(manga_download_folder, manga_name, chapter_number) | |
puts "Creating #{chapter_folder}" | |
FileUtils.mkdir_p(chapter_folder) | |
end | |
# download image file | |
img_uri = agent.page.search("//img[@class='manga-page']").first["src"] | |
image_file = File.join(chapter_folder, img_uri.split("/").last) | |
begin | |
open(image_file, 'wb') do |file| | |
puts "Downloading #{img_uri} to #{image_file}" | |
file.write(open(img_uri).read) | |
end | |
rescue | |
puts "Error downloading #{img_uri} to #{image_file}, retrying after 1 second..." | |
sleep 1 | |
retry | |
end | |
# next page | |
agent.get :url => agent.page.links.select { |link| link.text.strip.empty? }.first.href, :referer => agent.page, :headers => { "cookie" => "age_verified=30" } | |
end |
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
#!/usr/bin/env ruby | |
# Generate PDFs for the OneManga downloaded images | |
# Use like this: | |
# ruby onemanga_pdf.rb Bakuman 5 10 50 | |
# | |
# Meaning: | |
# Manga: Bakuman (name of the folder) | |
# Number of volumes: 5 (number of generated PDF files) | |
# Starting chapter: 10 (default is 0) | |
# Last chapter: 50 (default is -1, which is everything) | |
# | |
require 'rubygems' | |
require 'enumerator' | |
require 'prawn' | |
if ARGV.size < 1 | |
puts "Pass a manga name.\nonemanga_pdf.rb manga_name [# volumes] [# start chapter] [# last chapter]" | |
exit(1) | |
end | |
manga_name = ARGV[0] | |
volumes = ARGV[1].to_i || 1 # defaults to just one volume | |
start_chapter = ARGV[2].to_i || 0 | |
last_chapter = ARGV[3].to_i > 0 ? ARGV[3].to_i : -1 # defaults to all chapters | |
manga_download_folder = File.join(ENV['HOME'],"/Documents/OneManga/") | |
manga_current_folder = File.join(manga_download_folder, manga_name) | |
folders = Dir.glob(File.join(manga_current_folder, "*")) # fetch all chapters | |
# correctly sort directories names based o the chapter number at the end | |
folders.sort! do |a, b| | |
a =~ /\/(\d+)$/ | |
x = $1.to_i | |
b =~ /\/(\d+)$/ | |
y = $1.to_i | |
x <=> y | |
end | |
folders = folders[start_chapter..last_chapter] # limit chapters to process | |
current_volume = 1 | |
folders.each_slice(volumes) do |chapter_folders| | |
manga_file = File.join(manga_download_folder, manga_name + "_#{current_volume}.pdf") | |
File.delete(manga_file) if File.exists?(manga_file) | |
print "\nPrinting volume #{manga_file} ..." | |
Prawn::Document.generate(manga_file, :margin => 0, :page_size => "A4") do | |
chapter_folders.each do |chapter| | |
Dir.glob(File.join(chapter, "*")).each do |page| | |
next if page =~ /credit/ | |
image page, :fit => [595.28,841.89] | |
start_new_page | |
print "." | |
end | |
end | |
end | |
current_volume += 1 | |
end | |
puts "Finished printing out #{volumes} volumes for #{manga_name}." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment