mangdown 0.13.4 → 0.14.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.
@@ -1,25 +1,54 @@
1
1
  module Mangdown
2
2
  class MDHash
3
3
  include Equality
4
+ include Properties
5
+
6
+ properties :name, :uri, :manga, :chapter, :site
4
7
 
5
8
  def initialize(options = {})
6
9
  uri = options.fetch(:uri)
7
10
  name = options[:name]
8
11
  site = options[:site]
9
- @properties = Properties.new(uri, site, nil, name)
12
+ @adapter = Mangdown.adapter!(uri, site, nil, name)
10
13
 
11
- @hash = {}
12
- @hash[:name] = name
13
- @hash[:uri] = Mangdown::Uri.new(uri)
14
- @hash[:site] = @properties.type
15
- @hash[:manga] = options[:manga]
16
- @hash[:chapter] = options[:chapter]
14
+ self.name = name
15
+ self.uri = Mangdown::Uri.new(uri)
16
+ self.site = @adapter.site
17
+ self.manga = options[:manga]
18
+ self.chapter = options[:chapter]
19
+ end
20
+
21
+ # explicit conversion to manga list
22
+ def to_manga_list
23
+ # Seems weird to do it this way, but this is to intentially make it
24
+ # more tedious to create a Mangadown::Manga object
25
+ # (outsite of this method)
26
+ #
27
+ if @adapter.is_manga_list?
28
+ list = MangaList.new
29
+ list.load_manga(uri)
30
+ list
31
+ else
32
+ raise NoMethodError, 'This is not a known manga list type'
33
+ end
17
34
  end
18
35
 
19
36
  # explicit conversion to manga
20
37
  def to_manga
21
- if @properties.is_manga?
22
- Manga.new(uri, name)
38
+ # Seems weird to do it this way, but this is to intentially make it
39
+ # more tedious to create a Mangadown::Manga object
40
+ # (outsite of this method)
41
+ #
42
+ if @adapter.is_manga?
43
+ if name.to_s.empty?
44
+ properties.merge!(@adapter.manga) { |_, before, after|
45
+ before || after }
46
+ end
47
+
48
+ manga = Manga.new(uri, name)
49
+ manga.adapter = @adapter
50
+ manga.load_chapters
51
+ manga
23
52
  else
24
53
  raise NoMethodError, 'This is not a known manga type'
25
54
  end
@@ -27,8 +56,22 @@ module Mangdown
27
56
 
28
57
  # explicit conversion to chapter
29
58
  def to_chapter
30
- if @properties.is_chapter?
31
- Chapter.new(uri, name, manga)
59
+ # Seems weird to do it this way, but this is to intentially make it
60
+ # more tedious to create a Mangadown::Chapter object
61
+ # (outsite of this method)
62
+ #
63
+ if @adapter.is_chapter?
64
+ if name.to_s.empty? || manga.to_s.empty? || chapter.to_s.empty?
65
+ properties.merge!(@adapter.chapter) { |_, before, after|
66
+ before || after }
67
+ end
68
+ # Get a local binding
69
+ number = chapter
70
+
71
+ chapter = Chapter.new(uri, name, manga, number)
72
+ chapter.adapter = @adapter
73
+ chapter.load_pages
74
+ chapter
32
75
  else
33
76
  raise NoMethodError, 'This is not a known chapter type'
34
77
  end
@@ -36,64 +79,11 @@ module Mangdown
36
79
 
37
80
  # explicit conversion to page
38
81
  def to_page
39
- if @properties.is_page?
82
+ if @adapter.is_page?
40
83
  Page.new(uri, name, manga, chapter)
41
84
  else
42
85
  raise NoMethodError, 'This is not a known page type'
43
86
  end
44
87
  end
45
-
46
- # name reader
47
- def name
48
- @hash[:name]
49
- end
50
-
51
- # uri reader
52
- def uri
53
- @hash[:uri]
54
- end
55
-
56
- def manga
57
- @hash[:manga]
58
- end
59
-
60
- def chapter
61
- @hash[:chapter ]
62
- end
63
-
64
- # name writer
65
- def name=(other)
66
- @hash[:name] = other
67
- end
68
-
69
- # uri writer
70
- def uri=(other)
71
- @hash[:uri] = other
72
- end
73
-
74
- def manga=(other)
75
- @hash[:manga] = other
76
- end
77
-
78
- def chapter=(other)
79
- @hash[:chapter] = other
80
- end
81
-
82
- def [](key)
83
- @hash[key]
84
- end
85
-
86
- def to_hash
87
- @hash
88
- end
89
-
90
- def inspect
91
- to_hash.to_s
92
- end
93
- alias_method :to_s, :inspect
94
-
95
- def type
96
- @properties.type
97
- end
98
88
  end
99
89
  end
data/lib/mangdown/page.rb CHANGED
@@ -9,7 +9,7 @@ module Mangdown
9
9
  @name = Tools.valid_path_name(name)
10
10
  @chapter = chapter
11
11
  @manga = manga
12
- @uri = Mangdown::Uri.new(uri)
12
+ @uri = Mangdown::Uri.new(uri)
13
13
  end
14
14
 
15
15
  # explicit conversion to page
@@ -36,10 +36,10 @@ module Mangdown
36
36
 
37
37
  # downloads to specified directory
38
38
  def download_to(dir = Dir.pwd, opts = { force_download: false })
39
- return if file_exist?(dir) && !opts[:force_download]
40
-
41
39
  # cleanup existing file (all extensions)
42
40
  delete_files!(dir) if opts[:force_download]
41
+
42
+ return if file_exist?(dir)
43
43
 
44
44
  image = Tools.get(uri)
45
45
 
@@ -1,26 +1,37 @@
1
1
  module Mangdown
2
- class Properties
2
+ module Properties
3
3
 
4
- def initialize(uri, site = nil, doc = nil, name = nil)
5
- adapter_class = adapter_for_site(uri || site)
4
+ def self.included(base)
5
+ base.extend(ClassMethods)
6
+ end
6
7
 
7
- if adapter_class
8
- @adapter = adapter_class.new(uri, doc, name)
9
- else
10
- raise Adapter::NoAdapterError.new(uri || site)
11
- end
8
+ def properties
9
+ @_properties ||= {}
10
+ end
11
+
12
+ def [](key)
13
+ properties[key]
14
+ end
15
+
16
+ def inspect
17
+ properties.inspect
12
18
  end
13
19
 
14
- private
15
- def adapter_for_site(site)
16
- ADAPTERS.find { |adapter| site.to_s[adapter.site] }
20
+ def to_s
21
+ properties.to_s
17
22
  end
18
23
 
19
- def method_missing(method, *args, &block)
20
- if @adapter.respond_to?(method)
21
- @adapter.__send__(method, *args, &block)
22
- else
23
- super
24
+ module ClassMethods
25
+ def properties(*names)
26
+ names.each do |name|
27
+ define_method(name) do
28
+ properties[name]
29
+ end
30
+
31
+ define_method("#{name}=") do |other|
32
+ properties[name] = other
33
+ end
34
+ end
24
35
  end
25
36
  end
26
37
  end
@@ -1,3 +1,3 @@
1
1
  module Mangdown
2
- VERSION = "0.13.4"
2
+ VERSION = "0.14.0"
3
3
  end
data/lib/mangdown.rb CHANGED
@@ -4,28 +4,44 @@ require 'yaml'
4
4
  require 'zip'
5
5
  require 'filemagic'
6
6
 
7
- module Mangdown
8
- ADAPTERS = []
9
-
10
- DOWNLOAD_DIR ||= Dir.home + '/manga'
11
- end
12
-
13
- require_relative 'mangdown/adapter'
14
- adapters = "#{File.expand_path('../mangdown/adapter', __FILE__)}/*.rb"
15
- Dir[adapters].each do |f|
16
- require_relative f
17
- end
18
-
19
7
  require_relative 'mangdown/equality'
20
- require_relative 'mangdown/tools'
21
8
  require_relative 'mangdown/properties'
9
+ require_relative 'mangdown/tools'
22
10
  require_relative 'mangdown/uri'
23
11
  require_relative 'mangdown/page'
24
12
  require_relative 'mangdown/chapter'
25
13
  require_relative 'mangdown/manga'
26
14
  require_relative 'mangdown/manga_list.rb'
27
15
  require_relative 'mangdown/cbz'
28
- require_relative 'mangdown/commands'
29
16
  require_relative 'mangdown/md_hash'
30
17
 
18
+ require_relative 'mangdown/adapter'
19
+ require_relative 'mangdown/adapter/no_adapter_error'
20
+ require_relative 'mangdown/adapter/mangareader.rb'
21
+
22
+ module Mangdown
23
+ ADAPTERS = {}
24
+
25
+ DOWNLOAD_DIR ||= Dir.home + '/manga'
26
+
27
+ def self.register_adapter(name, adapter)
28
+ ADAPTERS[name] = adapter
29
+ end
30
+
31
+ def self.adapter(name)
32
+ ADAPTERS[name]
33
+ end
34
+
35
+ def self.adapter!(uri, site = nil, doc = nil, name = nil)
36
+ adapter_name = (uri || site).to_s
37
+ klass = ADAPTERS.values.find { |adapter| adapter.for?(adapter_name) }
38
+
39
+ if klass
40
+ klass.new(uri, doc, name)
41
+ else
42
+ raise Adapter::NoAdapterError.new(adapter_name)
43
+ end
44
+ end
45
+ end
31
46
 
47
+ Mangdown.register_adapter(:mangareader, Mangdown::Mangareader)
metadata CHANGED
@@ -1,29 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mangdown
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.4
4
+ version: 0.14.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - jphager2
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-03-06 00:00:00.000000000 Z
11
+ date: 2016-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
- - !ruby/object:Gem::Dependency
14
- name: faraday
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - "~>"
18
- - !ruby/object:Gem::Version
19
- version: 0.9.0
20
- type: :runtime
21
- prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - "~>"
25
- - !ruby/object:Gem::Version
26
- version: 0.9.0
27
13
  - !ruby/object:Gem::Dependency
28
14
  name: typhoeus
29
15
  requirement: !ruby/object:Gem::Requirement
@@ -146,15 +132,11 @@ files:
146
132
  - README.md
147
133
  - lib/mangdown.rb
148
134
  - lib/mangdown/adapter.rb
149
- - lib/mangdown/adapter/mangafox.rb
150
- - lib/mangdown/adapter/mangahere.rb
151
- - lib/mangdown/adapter/mangapanda.rb
152
135
  - lib/mangdown/adapter/mangareader.rb
153
136
  - lib/mangdown/adapter/no_adapter_error.rb
154
- - lib/mangdown/adapter/wiemanga.rb
155
137
  - lib/mangdown/cbz.rb
156
138
  - lib/mangdown/chapter.rb
157
- - lib/mangdown/commands.rb
139
+ - lib/mangdown/client.rb
158
140
  - lib/mangdown/equality.rb
159
141
  - lib/mangdown/manga.rb
160
142
  - lib/mangdown/manga_list.rb
@@ -164,18 +146,6 @@ files:
164
146
  - lib/mangdown/tools.rb
165
147
  - lib/mangdown/uri.rb
166
148
  - lib/mangdown/version.rb
167
- - spec/lib/mangdown/cbz_spec.rb
168
- - spec/lib/mangdown/chapter_spec.rb
169
- - spec/lib/mangdown/commands_spec.rb
170
- - spec/lib/mangdown/equality_spec.rb
171
- - spec/lib/mangdown/manga_list_spec.rb
172
- - spec/lib/mangdown/manga_spec.rb
173
- - spec/lib/mangdown/mangdown_hash_spec.rb
174
- - spec/lib/mangdown/page_spec.rb
175
- - spec/lib/mangdown/properties_spec.rb
176
- - spec/lib/mangdown/tools_spec.rb
177
- - spec/lib/mangdown/uri_spec.rb
178
- - spec/spec_helper.rb
179
149
  homepage: https://github.com/jphager2/mangdown
180
150
  licenses:
181
151
  - MIT
@@ -1,47 +0,0 @@
1
- module Mangdown
2
- class Mangafox < Adapter::Base
3
- Mangdown::ADAPTERS << self
4
-
5
- def initialize(uri, doc, name)
6
- super
7
- @manga_list_css = 'div.manga_list li a'
8
- @chapter_list_css = 'a.tips'
9
- @root = 'http://mangafox.me'
10
- @manga_list_uri = "#{@root}/manga"
11
- @manga_name_css = "#title h1"
12
- @manga_link_prefix = ''
13
- @reverse_chapters = true
14
- @manga_uri_regex =
15
- /#{@root}\/manga\/[^\/]+?\//i
16
- @chapter_uri_regex =
17
- /#{@manga_uri_regex}(v\d+\/)?(c\d+\/)(1\.html)/i
18
- @page_uri_regex = /.+\.(png|jpg|jpeg)$/i
19
- end
20
-
21
- def manga_name
22
- CGI.unescapeHTML(super.sub(/ Manga/, '').downcase).upcase
23
- end
24
-
25
- def build_page_uri(uri, manga, chapter, page_num)
26
- uri.sub(/\d+\.html/, "#{page_num}.html")
27
- end
28
-
29
- def num_pages
30
- doc.css('select')[1].css('option').length - 1
31
- end
32
-
33
- def page_image_src
34
- page_image[:src]
35
- end
36
-
37
- def page_image_name
38
- doc.css('select')[1].css('option[selected]').first().text
39
- .rjust(3,'0')
40
- end
41
-
42
- private
43
- def page_image
44
- doc.css('img')[0]
45
- end
46
- end
47
- end
@@ -1,51 +0,0 @@
1
- module Mangdown
2
- class Mangahere < Adapter::Base
3
- Mangdown::ADAPTERS << self
4
-
5
- def initialize(uri, doc, name)
6
- super
7
- @root = 'http://www.mangahere.co'
8
- @manga_list_css = 'a.manga_info'
9
- @manga_name_css = 'h1.title'
10
- @chapter_list_css = '.detail_list ul a'
11
- @manga_list_uri = "#{@root}/mangalist/"
12
- @manga_link_prefix = ''
13
- @reverse_chapters = true
14
- @manga_uri_regex =
15
- /#{@root}\/manga\/([^\/]+\/)?/i
16
- @chapter_uri_regex =
17
- /#{@manga_uri_regex}(v\d+\/)?(c\d+\/)(1\.html)?/i
18
- @page_uri_regex = /.+\.(png|jpg|jpeg)$/i
19
- end
20
-
21
- def manga_name
22
- CGI.unescapeHTML(super.downcase).upcase
23
- end
24
-
25
- def build_page_uri(uri, manga, chapter, page_num)
26
- if page_num == 1
27
- uri
28
- else
29
- uri.sub(/\/[^\/]*$/, "/#{page_num}.html")
30
- end
31
- end
32
-
33
- def num_pages
34
- doc.css('.go_page span.right select')[0].css('option').length
35
- end
36
-
37
- def page_image_src
38
- page_image[:src].sub(/\?.*$/, '')
39
- end
40
-
41
- def page_image_name
42
- doc.css('.go_page span.right select')[0].css('option[selected]')
43
- .text.rjust(3, '0')
44
- end
45
-
46
- private
47
- def page_image
48
- doc.css('.read_img img')[0]
49
- end
50
- end
51
- end
@@ -1,13 +0,0 @@
1
- require_relative 'mangareader'
2
-
3
- module Mangdown
4
- class Mangapanda < Mangareader
5
- Mangdown::ADAPTERS << self
6
-
7
- def initialize(uri, doc, name)
8
- @root = 'http://www.mangapanda.com'
9
- super
10
- end
11
- end
12
- end
13
-
@@ -1,49 +0,0 @@
1
- module Mangdown
2
- class Wiemanga < Adapter::Base
3
- Mangdown::ADAPTERS << self
4
-
5
- def initialize(uri, doc, name)
6
- super
7
- @root = 'http://www.wiemanga.com'
8
- @manga_list_uri =
9
- "#{@root}/search/?name_sel=contain&author_sel=contain" +
10
- "&completed_series=either"
11
- @manga_name_css = '.bookmessagebox h1'
12
- @manga_list_css = 'a.resultbookname'
13
- @chapter_list_css =
14
- '.chapterlist tr:not(:first-child) .col1 a'
15
- @manga_link_prefix = ''
16
- @reverse_chapters = true
17
- @manga_uri_regex = /#{@root}\/manga\/([^\/]+)(\.html)?/i
18
- @chapter_uri_regex =
19
- /#{@root}\/chapter\/([^\/]+)\/(\d+)(\/|\.html)?/i
20
- @page_uri_regex = /.+\.(png|jpg|jpeg)$/i
21
- end
22
-
23
- def manga_name
24
- super.sub(/ Manga$/, "")
25
- end
26
-
27
- def build_page_uri(uri, manga, chapter, page_num)
28
- "#{uri.sub(/\/+$/, '')}-#{page_num}.html"
29
- end
30
-
31
- def num_pages
32
- doc.css('select#page')[0].css('option').length
33
- end
34
-
35
- def page_image_src
36
- page_image[:src]
37
- end
38
-
39
- def page_image_name
40
- doc.css('select#page')[0].css('option[selected]')[0]
41
- .text.rjust(3, '0')
42
- end
43
-
44
- private
45
- def page_image
46
- doc.css('img#comicpic')[0]
47
- end
48
- end
49
- end
@@ -1,40 +0,0 @@
1
- require_relative '../../spec_helper'
2
-
3
- describe Mangdown::CBZ do
4
- describe 'CBZ all' do
5
-
6
- before do
7
- @fixtures_dir = File.expand_path('../../../fixtures', __FILE__)
8
- @tmp_dir = File.expand_path('../../../../tmp', __FILE__)
9
- @nisekoi_dir = @tmp_dir + "/Nisekoi"
10
- FileUtils.cp_r(@fixtures_dir + '/Nisekoi', @tmp_dir)
11
- Mangdown::CBZ.all(@nisekoi_dir)
12
- end
13
-
14
- after do
15
- FileUtils.rm_r(@nisekoi_dir, force: true)
16
- end
17
-
18
- it 'must have equal numbers of cbz files and chapter directories' do
19
- chapter_count = Dir["#{@nisekoi_dir}/*/"].length
20
- cbz_count = Dir["#{@nisekoi_dir}/*.cbz"].length
21
- cbz_count.must_equal(chapter_count)
22
- end
23
-
24
- it 'cbz file names should be valid' do
25
- filenames = Dir["#{@nisekoi_dir}/*.cbz"]
26
- filenames.each do |name|
27
- name.must_match /\d{3}\.cbz/
28
- end
29
- end
30
-
31
- it 'cbz image file names should be valid' do
32
- zips = Dir["#{@nisekoi_dir}/*.cbz"]
33
- names = zips.flat_map {|f| Zip::File.new(f).glob('*').map(&:name)}
34
- names.each do |name|
35
- # Don't use #must_match, it causes an error with Zip::File
36
- (name =~ /Page \d{3}/).wont_be_nil
37
- end
38
- end
39
- end
40
- end
@@ -1,79 +0,0 @@
1
- require_relative '../../spec_helper'
2
-
3
- describe Mangdown::Chapter do
4
- let(:download_path) { File.expand_path('../../../../tmp', __FILE__) }
5
- let(:chapter_name) { "Dragon Ball 1" }
6
- let(:download_chapter_name) { "Dragon Ball 001" }
7
- let(:uri) {
8
- "http://www.mangareader.net/105-2100-1/dragon-ball/chapter-1.html"
9
- }
10
-
11
- before do
12
- VCR.insert_cassette 'events', record: :new_episodes
13
- @chapter = Mangdown::Chapter.new(uri, chapter_name)
14
- end
15
-
16
- after do
17
- VCR.eject_cassette
18
- FileUtils.rm_r("#{download_path}/#{chapter_name}", force: true)
19
- end
20
-
21
- describe 'new' do
22
- it 'must return a kind of Chapter but not an instance of Chapter' do
23
- end
24
- end
25
-
26
- describe 'to_chapter' do
27
- it 'must return the same class of chapter' do
28
- new_chapter = @chapter.to_chapter
29
- new_chapter.must_be_instance_of @chapter.class
30
- end
31
- end
32
-
33
- describe 'each' do
34
- it 'must return an enumerator' do
35
- @chapter.each.must_be_instance_of Enumerator
36
- end
37
-
38
- it 'must iterate through each page' do
39
- count = 0
40
- @chapter.each do |page|
41
- page.to_page.must_be_instance_of Mangdown::Page
42
- count += 1
43
- end
44
- count.must_equal @chapter.pages.length
45
- end
46
- end
47
-
48
- describe 'download_to' do
49
- before do
50
- # @times will start at 0 and end at N, where N is the number
51
- # of specs in this describe block
52
- @times ||= 0
53
- @chapter.download_to(download_path) if @times == 0
54
- @times += 1
55
- end
56
-
57
- after do
58
- # @times will start at 0 and end at N, where N is the number
59
- # of specs in this describe block
60
- if @times == 2 && Dir.exist?("#{download_path}/#{chapter_name}")
61
- FileUtils.rm_r(@nisekoi_dir, force: true)
62
- end
63
- end
64
-
65
- it 'must create a subdirectory with the chapter name' do
66
- skip
67
- Dir.exist?("#{download_path}/#{download_chapter_name}").must_equal true
68
- end
69
-
70
- it 'must have page files in the sub directory' do
71
- skip
72
- files = Dir["#{download_path}/#{chapter_name}/*"]
73
- files.each do |name|
74
- @chapter.pages.any? { |page| name =~ /#{page.name}/ }
75
- .must_equal true
76
- end
77
- end
78
- end
79
- end