Class: Mrmanga::Downloader

Inherits:
Object
  • Object
show all
Defined in:
lib/mrmanga/downloader.rb

Instance Method Summary collapse

Constructor Details

#initialize(manga, settings) ⇒ Downloader

Returns a new instance of Downloader.



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/mrmanga/downloader.rb', line 8

def initialize(manga, settings)
  @manga = manga

  @settings = settings

  @name = @manga.info[:info][:name]

  if File.directory?(@name)
    should_continue = agree("Folder '#{@name}' already exists, continue?") { |q| q.default = 'yes' }

    return unless should_continue
  end
end

Instance Method Details

#download_volume(volume) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mrmanga/downloader.rb', line 22

def download_volume(volume)
  if @settings[:volumes] != 'all'
    unless @settings[:volumes].include?(volume.to_s)
      puts "Skipping Vol. #{volume}"
      return false
    end
  end

  puts "Downloading volume #{volume}"

  prefix = "#{@name}/vol#{volume}"

  parser = Mrmanga::Parser.new

  FileUtils.mkdir_p(prefix)

  downloaded = {}

  @manga.volumes[volume].each do |volch|
    pages = parser.get_chapter_pages(@manga, volch[0], volch[1])

    progress = ProgressBar.create(title: "Downloading vol.#{volch[0]} ch.#{volch[1]}", total: pages.count)

    Parallel.each_with_index(pages, in_threads: @settings[:threads]) do |page, index|
      progress.increment

      timeout_retries = 0

      begin
        temp = Down.download(page[:link], open_timeout: 20, read_timeout: 20, 'Referer' => @manga.info[:info][:site])
      rescue Down::TimeoutError
        retry if timeout_retries += 1 < 15

        puts 'TimeoutError after many retries, debug info:'

        puts page[:link]
        puts @manga.info[:info]
        puts volch.inspect
        puts "Page number: #{index + 1}"
        puts @settings

        raise
      rescue
        puts 'Debug info:'

        puts page[:link]
        puts @manga.info[:info]
        puts volch.inspect
        puts "Page number: #{index + 1}"
        puts @settings

        raise
      end

      temp.close # Windows needs this

      ext = File.extname(temp.path)

      new_path = File.join(prefix, "#{volch[0]} - #{volch[1]} - #{index + 1}#{ext}")

      FileUtils.mv temp.path, new_path

      page[:file] = new_path
    end

    downloaded[volch[1]] = pages

    progress.finish
  end

  {
    volume: volume,
    pages: downloaded
  }
end