class MangaDownloadr::Workflow

Public Class Methods

download_images(images, config) click to toggle source
# File lib/manga-downloadr/workflow.rb, line 53
def self.download_images(images, config)
  puts "Downloading each image ..."
  reactor = Concurrency.new(ImageDownloader, config, false)
  reactor.fetch(images) do |image, _|
    image_file = File.join(config.download_directory, image.filename)
    unless File.exists?(image_file)
      ImageDownloader.new(image.host).fetch(image.path, image_file)
    end
    [ image_file ]
  end
end
fetch_chapters(config) click to toggle source
# File lib/manga-downloadr/workflow.rb, line 30
def self.fetch_chapters(config)
  puts "Fetching chapters ..."
  chapters = Chapters.new(config.domain, config.root_uri, config.cache_http).fetch
  puts "Number of Chapters: #{chapters&.size}"
  chapters
end
fetch_images(pages, config) click to toggle source
# File lib/manga-downloadr/workflow.rb, line 45
def self.fetch_images(pages, config)
  puts "Feching the Image URLs from each Page ..."
  reactor = Concurrency.new(PageImage, config)
  reactor.fetch(pages) do |link, engine|
    [ engine&.fetch(link) ]
  end
end
fetch_pages(chapters, config) click to toggle source
# File lib/manga-downloadr/workflow.rb, line 37
def self.fetch_pages(chapters, config)
  puts "Fetching pages from all chapters ..."
  reactor = Concurrency.new(Pages, config)
  reactor.fetch(chapters) do |link, engine|
    engine&.fetch(link)
  end
end
optimize_images(downloads, config) click to toggle source
# File lib/manga-downloadr/workflow.rb, line 65
def self.optimize_images(downloads, config)
  puts "Running mogrify to convert all images down to Kindle supported size (600x800)"
  `mogrify -resize #{config.image_dimensions} #{config.download_directory}/*.jpg`
  downloads
end
prepare_volumes(downloads, config) click to toggle source
# File lib/manga-downloadr/workflow.rb, line 71
def self.prepare_volumes(downloads, config)
  manga_name = config.download_directory.split("/")&.last
  index = 1
  volumes = []
  downloads.each_slice(config.pages_per_volume) do |batch|
    volume_directory = "#{config.download_directory}/#{manga_name}_#{index}"
    volume_file      = "#{volume_directory}.pdf"
    volumes << volume_file
    FileUtils.mkdir_p volume_directory

    puts "Moving images to #{volume_directory} ..."
    batch.each do |file|
      destination_file = file.split("/").last
      `mv #{file} #{volume_directory}/#{destination_file}`
    end

    puts "Generating #{volume_file} ..."
    `convert #{volume_directory}/*.jpg #{volume_file}`

    index += 1
  end
  volumes
end
run(config = Config.new) click to toggle source
# File lib/manga-downloadr/workflow.rb, line 3
def self.run(config = Config.new)
  FileUtils.mkdir_p config.download_directory

  CM(config, Workflow)
    .fetch_chapters
    .fetch_pages(config)
    .fetch_images(config)
    .download_images(config)
    .optimize_images(config)
    .prepare_volumes(config)
    .unwrap

  puts "Done!"
end
run_tests(config = Config.new) click to toggle source
# File lib/manga-downloadr/workflow.rb, line 18
def self.run_tests(config = Config.new)
  FileUtils.mkdir_p "/tmp/manga-downloadr-cache"

  CM(Workflow, config)
    .fetch_chapters
    .fetch_pages(config)
    .fetch_images(config)
    .unwrap

  puts "Done!"
end