muse-dl/src/muse-dl.cr

149 lines
5.0 KiB
Crystal
Raw Normal View History

2020-03-28 19:29:47 +00:00
require "./parser.cr"
require "./pdftk.cr"
require "./fetch.cr"
2020-03-28 19:51:36 +00:00
require "./book.cr"
2020-03-28 22:22:57 +00:00
require "./journal.cr"
2020-03-29 14:04:51 +00:00
require "./util.cr"
require "file_utils"
2020-03-28 21:07:14 +00:00
2020-03-28 19:29:47 +00:00
module Muse::Dl
2020-04-05 13:28:28 +00:00
VERSION = "1.1.2"
2020-03-28 19:29:47 +00:00
2020-03-28 21:07:14 +00:00
class Main
2020-03-29 19:21:09 +00:00
def self.dl(parser : Parser)
url = parser.url
thing = Fetch.get_info(url) if url
return unless thing
2020-03-28 22:22:57 +00:00
if thing.is_a? Muse::Dl::Book
2020-03-31 20:05:59 +00:00
unless thing.formats.includes? :pdf
STDERR.puts "Book not available in PDF format, skipping: #{url}"
return
end
2020-03-29 14:04:51 +00:00
# Will have no effect if parser has a custom title
parser.output = Util.slug_filename "#{thing.title}.pdf"
2020-03-29 18:38:10 +00:00
# If file exists and we can't clobber
if File.exists?(parser.output) && parser.clobber == false
2020-04-05 13:27:24 +00:00
STDERR.puts "Skipping #{url}, File already exists: #{parser.output}"
return
2020-03-28 22:22:57 +00:00
end
2020-03-29 18:38:10 +00:00
temp_stitched_file = nil
2020-03-29 12:21:01 +00:00
pdf_builder = Pdftk.new(parser.tmp)
2020-03-29 18:38:10 +00:00
# Save each chapter
thing.chapters.each do |chapter|
begin
Fetch.save_chapter(parser.tmp, chapter[0], chapter[1], parser.cookie, parser.bookmarks, parser.strip_first)
rescue e : Muse::Dl::Errors::MuseCorruptPDF
STDERR.puts "Got a 'Unable to construct chapter PDF' error from MUSE, skipping: #{url}"
return
2020-03-29 18:38:10 +00:00
end
end
chapter_ids = thing.chapters.map { |c| c[0] }
# Stitch the PDFs together
temp_stitched_file = pdf_builder.stitch chapter_ids
pdf_builder.add_metadata(temp_stitched_file, parser.output, thing)
2020-03-29 18:38:10 +00:00
temp_stitched_file.delete if temp_stitched_file
2020-06-30 08:38:28 +00:00
puts "--dont-strip-first-page was on. Please validate PDF file for any errors." if parser.strip_first
2020-04-05 13:28:02 +00:00
puts "DL: #{url}. Saved final output to #{parser.output}"
2020-04-03 19:47:31 +00:00
# Cleanup the chapter files
if parser.cleanup
thing.chapters.each do |c|
Fetch.cleanup(parser.tmp, c[0])
end
end
elsif thing.is_a? Muse::Dl::Article
# No bookmarks are needed since this is just a single article PDF
begin
Fetch.save_article(parser.tmp, thing.id, parser.cookie, nil, parser.strip_first)
rescue e : Muse::Dl::Errors::MuseCorruptPDF
STDERR.puts "Got a 'Unable to construct chapter PDF' error from MUSE, skipping: #{url}"
return
end
# TODO: Move this code elsewhere
source = Fetch.article_file_name(thing.id, parser.tmp)
destination = "article-#{thing.id}.pdf"
# Needed because of https://github.com/crystal-lang/crystal/issues/7777
FileUtils.cp source, destination
FileUtils.rm source if parser.cleanup
elsif thing.is_a? Muse::Dl::Issue
# Will have no effect if parser has a custom title
parser.output = Util.slug_filename "#{thing.title}.pdf"
# If file exists and we can't clobber
if File.exists?(parser.output) && parser.clobber == false
STDERR.puts "Skipping #{url}, File already exists: #{parser.output}"
return
end
temp_stitched_file = nil
pdf_builder = Pdftk.new(parser.tmp)
# ## TODO till 111
thing.issues.each do |issue|
begin
Fetch.save_issue(parser.tmp, chapter[0], chapter[1], parser.cookie, parser.bookmarks, parser.strip_first)
rescue e : Muse::Dl::Errors::MuseCorruptPDF
STDERR.puts "Got a 'Unable to construct chapter PDF' error from MUSE, skipping: #{url}"
return
end
end
chapter_ids = thing.chapters.map { |c| c[0] }
# Stitch the PDFs together
temp_stitched_file = pdf_builder.stitch chapter_ids
pdf_builder.add_metadata(temp_stitched_file, parser.output, thing)
temp_stitched_file.delete if temp_stitched_file
puts "--dont-strip-first-page was on. Please validate PDF file for any errors." if parser.strip_first
puts "DL: #{url}. Saved final output to #{parser.output}"
# Cleanup the chapter files
if parser.cleanup
thing.chapters.each do |c|
Fetch.cleanup(parser.tmp, c[0])
end
end
####
2020-03-28 22:22:57 +00:00
end
2020-03-28 21:07:14 +00:00
end
2020-03-29 19:21:09 +00:00
def self.run(args : Array(String))
parser = Parser.new(args)
delay_secs = 1
2020-03-29 19:21:09 +00:00
input_list = parser.input_list
if input_list
File.each_line input_list do |url|
begin
# TODO: Change this to nil
parser.reset_output_file
parser.url = url.strip
# Ask the download process to not quit the process, and return instead
Main.dl parser
if delay_secs >= 2
delay_secs /= 2
end
rescue ex
puts ex.message
puts ex.backtrace.join("\n ")
puts "Error. Skipping book: #{url}. Waiting for #{delay_secs} seconds before continuing."
sleep(delay_secs)
if delay_secs < 256
delay_secs *= 2
end
end
2020-03-29 19:21:09 +00:00
end
elsif parser.url
Main.dl parser
end
end
2020-03-28 21:07:14 +00:00
end
2020-03-28 19:29:47 +00:00
end
2020-03-28 21:14:48 +00:00
Muse::Dl::Main.run(ARGV)