diff --git a/src/muse-dl.cr b/src/muse-dl.cr index 07dcefa..14c3f7d 100644 --- a/src/muse-dl.cr +++ b/src/muse-dl.cr @@ -9,9 +9,10 @@ module Muse::Dl VERSION = "0.1.0" class Main - def self.run(args : Array(String)) - parser = Parser.new(args) - thing = Fetch.get_info(parser.url) + def self.dl(parser : Parser) + url = parser.url + thing = Fetch.get_info(url) if url + return unless thing if thing.is_a? Muse::Dl::Book # Will have no effect if parser has a custom title @@ -44,6 +45,20 @@ module Muse::Dl puts "Saved final output to #{parser.output}" end end + + def self.run(args : Array(String)) + parser = Parser.new(args) + + input_list = parser.input_list + if input_list + File.each_line input_list do |url| + parser.url = url.strip + Main.dl parser + end + elsif parser.url + Main.dl parser + end + end end end diff --git a/src/parser.cr b/src/parser.cr index d2c2374..a41020e 100644 --- a/src/parser.cr +++ b/src/parser.cr @@ -7,13 +7,15 @@ module Muse::Dl @tmp : String @cleanup = true @output = DEFAULT_FILE_NAME - @url = "INVALID_URL" + @url : String | Nil @input_pdf : String | Nil @clobber = false + @input_list : String | Nil DEFAULT_FILE_NAME = "tempfilename.pdf" - getter :bookmarks, :tmp, :cleanup, :output, :url, :input_pdf, :clobber + getter :bookmarks, :tmp, :cleanup, :output, :url, :input_pdf, :clobber, :input_list + setter :url # Update the output filename unless we have a custom one passed def output=(output_file : String) @@ -34,13 +36,19 @@ module Muse::Dl @input_pdf = nil parser = OptionParser.new - parser.banner = "Usage: muse-dl [--flags] URL" + parser.banner = <<-EOT + Usage: muse-dl [--flags] [URL|INPUT_FILE] + + URL: A link to a book on the Project MUSE website, eg https://muse.jhu.edu/book/875 + INPUT_FILE: Path to a file containing a list of links + + EOT parser.on(long_flag = "--no-cleanup", description = "Don't cleanup temporary files") { @cleanup = false } parser.on(long_flag = "--tmp-dir PATH", description = "Temporary Directory to use") { |path| @tmp = path } parser.on(long_flag = "--output FILE", description = "Output Filename") { |file| @output = file } parser.on(long_flag = "--no-bookmarks", description = "Don't add bookmarks in the PDF") { @bookmarks = false } parser.on(long_flag = "--input-pdf INPUT", description = "Input Stitched PDF. Will not download anything") { |input| @input_pdf = input } - parser.on(long_flag = "--clobber", description = "Overwrite the output file, if it already exists") { @clobber = true } + parser.on(long_flag = "--clobber", description = "Overwrite the output file, if it already exists. Not compatible with input-pdf") { @clobber = true } parser.on("-h", "--help", "Show this help") { puts parser } parser.unknown_args do |args| @@ -48,7 +56,12 @@ module Muse::Dl puts parser exit 1 end - @url = args[0] + if File.exists? args[0] + @input_list = args[0] + @input_pdf = nil + else + @url = args[0] + end end parser.invalid_option do |flag|