manga_fetch 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 870395ce4d4c86cedf154f07a78ba30ed2adc37c
4
- data.tar.gz: f16ff576f1ae7e24645ebd86a5d9599c79d0bb7c
3
+ metadata.gz: b7548ec57ffa2092d910e848cf1a1f05949d9b57
4
+ data.tar.gz: 37c6122db7107bc6d4715482d72302dc0753e5b5
5
5
  SHA512:
6
- metadata.gz: fa7be02326bc288750835ab04115464a6d5cf8fd5420dbda45281b79a149ae7b738e021d515f32b4bd0951c0e7883c30a25229c07ff3e3ca99ce5d7c8142eeb0
7
- data.tar.gz: 3c6aff3e36cd2e026f29b9aafe61fb91470e0a24d01657eed18ccf9c9973f94cf9402c0d481dcba17aa4a93dbe4db9c804e5c507fe75dc585338af6c84faa416
6
+ metadata.gz: 812d5ad2a4dd2bca5f45e9cdafef83716b71f5dcea29ba9967872aada10523eb05dfb563ccdcb57a01f0767b152f5914adbbc6fc5c128d99b3a5e75894e3bd27
7
+ data.tar.gz: 0e78afcf03633e425b21e9b12a6b3a804608d7227fa5fcaea9ef1368c3b69da646b77778b217f6df9ac7afd3928521ac1a082ff98f1f028179d808d5abdf991e
checksums.yaml.gz.sig CHANGED
Binary file
data/bin/manga_fetch CHANGED
@@ -1,14 +1,56 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require_relative "../lib/manga_fetch"
3
+ require 'optparse'
4
4
 
5
- $verbose = true
5
+ argv = OptionParser.new do |opts|
6
+ opts.banner = "Usage: manga_scrap <name> <min..max>"
7
+ $threads = 8
8
+ $verbose = true
9
+ $debug = false
10
+ $verbose = false if ENV["VERBOSE"] == "false"
11
+ $debug = true if ENV["DEBUG"] == "true"
12
+ $dir = "/tmp"
6
13
 
7
- m = ARGV[1].match(/\A(\d+)\.\.(\d+)\Z/)
8
- nums = m[1].to_i .. m[2].to_i
14
+ opts.on("--threads=N", "Number of threads to use (default: 1)") do |v|
15
+ $threads = Integer(v)
16
+ end
9
17
 
10
- list = MangaFetch.fetch(name: ARGV[0], num: nums).flatten.sort
18
+ opts.on("--dir=path", "--output=path", "Output directory (default: /tmp)") do |v|
19
+ $dir = v
20
+ end
11
21
 
12
- puts
13
- puts list
14
- list.each{|image| MangaFetch.download_image(src: image)}
22
+ opts.on("--verbose", "Display infos on stderr (default)") do
23
+ $verbose = true
24
+ end
25
+
26
+ opts.on("--no-verbose", "Display infos on stderr") do
27
+ $verbose = true
28
+ end
29
+
30
+ opts.on("--debug", "-d", "Developper mode") do
31
+ $debug = true
32
+ end
33
+ end.parse!
34
+
35
+ if $debug
36
+ require_relative "../lib/manga_fetch"
37
+ else
38
+ require "manga_fetch"
39
+ end
40
+
41
+ m = argv[1].match(/\A(\d+)\.\.(\d+)\Z/)
42
+ nums = (m[1].to_i .. m[2].to_i).to_a
43
+
44
+ nb_elem_by_list = (nums.size.to_f / $threads).ceil
45
+ lists = nums.each_slice(nb_elem_by_list).to_a
46
+
47
+ STDERR.puts "Starting #{$threads} threads..." if $verbose
48
+ lists.map.with_index do |current_nums, idx|
49
+ t = Thread.new do
50
+ STDERR.puts "Started #{idx+1} / #{$threads} threads" if $verbose
51
+ bot = MangaFetch::Fetcher.new
52
+ list = bot.fetch(name: argv[0], num: current_nums, download: true, dir: $dir).flatten.sort
53
+ end
54
+ sleep 0.5
55
+ t
56
+ end.map(&:join)
@@ -1,12 +1,12 @@
1
1
  module MangaFetch::Downloader
2
- extend self
3
-
4
- def download_image(src: nil, prefix: "/tmp")
5
- image = @agent.get src
2
+ # Download an image defined with `src` in the directory `prefix`. It uses `@agent` by default
3
+ def download_image(src: nil, prefix: "/tmp", agent: nil)
4
+ prefix ||= "/tmp"
5
+ image = (agent || @agent).get src
6
6
  path = File.expand_path image.uri.to_s.split("/")[-3..-1].join("/"), prefix
7
7
  Dir.mkdir File.expand_path image.uri.to_s.split("/")[-3..-3].join("/"), prefix rescue nil
8
8
  Dir.mkdir File.expand_path image.uri.to_s.split("/")[-3..-2].join("/"), prefix rescue nil
9
9
  File.write path, image.body
10
- STDERR.puts "File [".blue + path.yellow + "] saved"
10
+ STDERR.puts "File [".blue + path.yellow + "] saved (".blue + path.yellow + ")".blue
11
11
  end
12
12
  end
@@ -1,24 +1,27 @@
1
- module MangaFetch::Fetcher
2
- extend self
1
+ require_relative "downloader"
2
+
3
+ class MangaFetch::Fetcher
4
+ include MangaFetch::Downloader
3
5
 
4
6
  # Fetch some pages form the manga named like `name`
5
- def fetch(name: "berserk", num: nil)
6
- init_agent("http://www.mangareader.net/#{name}")
7
+ def fetch(name: "berserk", num: nil, download: false, dir: nil)
8
+ init_agent(url: "http://www.mangareader.net/#{name}")
7
9
 
8
10
  images = if num.is_a? Fixnum
9
- [ fetch_one(num) ]
11
+ [ fetch_one(num: num, download: download, dir: dir) ]
10
12
  elsif num.is_a? Enumerable
11
- fetch_list(num)
13
+ fetch_list(nums: num, download: download, dir: dir)
12
14
  end
13
15
  end
14
16
 
15
- def init_agent(url)
17
+ private def init_agent(url: nil)
16
18
  @agent = Mechanize.new
17
19
  @agent.get url
18
20
  @main = @agent.page
19
21
  end
20
22
 
21
- def fetch_one(num: 1)
23
+ # fetch every images of the tome `num` loaded in `@main`
24
+ def fetch_one(num: 1, download: false, dir: nil)
22
25
  link = "#{@main.uri.to_s}/#{num}"
23
26
  STDERR.puts "Fetching Tome [".blue + "#{num}".yellow + "]".blue if $verbose
24
27
  first_page = @agent.get(link)
@@ -28,15 +31,16 @@ module MangaFetch::Fetcher
28
31
  STDERR.puts "Fetching Tome [".blue + "#{num}".yellow + "] Image [".blue + "#{i} / #{last_page_num}".yellow + "]".blue if $verbose
29
32
  current_page = @agent.get "#{first_page.uri.to_s}/#{i}"
30
33
  image = current_page.at("#img")[:src]
31
- STDERR.puts "-> ".blue + image.red if $verbose
34
+ STDERR.puts "[#{download ? "D" : "S"}] -> ".blue + image.red if $verbose
35
+ download_image(src: image, prefix: dir) if download
32
36
  image
33
37
  end
34
-
35
38
  end
36
39
 
37
- def fetch_list(nums)
40
+ # fetch every images of the tomes `nums` loaded in `@main`
41
+ def fetch_list(nums: nil, download: false, dir: nil)
38
42
  ones = []
39
- nums.map{|num| ones << fetch_one(num: 1) } # , threads: 4
43
+ nums.map{|num| ones << fetch_one(num: num, download: download, dir: dir) }
40
44
  ones
41
45
  end
42
46
  end
data/lib/manga_fetch.rb CHANGED
@@ -4,11 +4,4 @@ require "colorize"
4
4
  module MangaFetch
5
5
  end
6
6
 
7
- require_relative "manga_fetch/downloader"
8
7
  require_relative "manga_fetch/fetcher"
9
-
10
- module MangaFetch
11
- include Downloader
12
- include Fetcher
13
- extend self
14
- end
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: manga_fetch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arthur Poulet
@@ -31,7 +31,7 @@ cert_chain:
31
31
  tcYkgfqUJPitIJx1RvWZpIyH5uJhRUYK3+vU9nMOxez5WbIlC1TtpByKAPMX+sht
32
32
  gib3AoIT8jh/2w==
33
33
  -----END CERTIFICATE-----
34
- date: 2016-08-26 00:00:00.000000000 Z
34
+ date: 2016-08-27 00:00:00.000000000 Z
35
35
  dependencies:
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: nomorebeer
@@ -93,7 +93,7 @@ dependencies:
93
93
  - - ">="
94
94
  - !ruby/object:Gem::Version
95
95
  version: 2.7.5
96
- description: add multi-thread and advanced features (download, opt parsing, ...)
96
+ description:
97
97
  email: arthur.poulet@mailoo.org
98
98
  executables:
99
99
  - manga_fetch
metadata.gz.sig CHANGED
Binary file