mangdown 0.8.3 → 0.8.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2661378fbd595b26a9bf3287f4ac62998aa6839c
4
- data.tar.gz: 020610efc7afb039d77415d18ac5f5a232541ca5
3
+ metadata.gz: 6443de1c0cb154bfc68bd4b1f5cea81f58946693
4
+ data.tar.gz: 8726490e4b2178e714ab66db8ee27afafe4107db
5
5
  SHA512:
6
- metadata.gz: 6ce635544cdd44bdfa0c6b8bb39cd296ecac6e356130440ceceeb5c86a0b617496ac5b66cc0a027e64f20934515dbc6b7da37e7dc314b7e56ef950c5f0be4c27
7
- data.tar.gz: d96dbacc58a1388b8b3a57bf89538af380b2920100dc5453d61fc8402b3653f005f2fab566f506a2a24a78b531737e36983cbb97d4b0826e1bf74c488742c001
6
+ metadata.gz: 767c5e5aeb49ff9b2e28d16035ff718d054bded41e78cf690c53659593663469eb98fd22ac03cef0f6b08bbe58c50637450427150f998e319c76c08affc3497a
7
+ data.tar.gz: 064ca3d79312925c8b919dc9383aedfc8c8884ec8aa40abb923053b1ba1fa9f076a892f2a817a47c44d1146bee3e5a7a84c7d89ed249c94746857c173614993f
@@ -5,7 +5,11 @@ require 'yaml'
5
5
  require 'timeout'
6
6
  require 'zip'
7
7
 
8
+ require_relative 'mangdown/mangdown'
9
+
8
10
  require_relative 'mangdown/tools'
11
+ require_relative 'mangdown/properties'
12
+ require_relative 'mangdown/uri'
9
13
  require_relative 'mangdown/page'
10
14
  require_relative 'mangdown/chapter'
11
15
  require_relative 'mangdown/manga'
@@ -7,32 +7,55 @@ module Mangdown
7
7
  zip_file_name = dir + '.cbz'
8
8
  dir += '/' unless dir[-1] == '/'
9
9
 
10
- ::Zip::File.open(zip_file_name, ::Zip::File::CREATE) do |zp|
10
+ ::Zip::File.open(zip_file_name, ::Zip::File::CREATE) do |zip|
11
11
  Dir[File.join(dir, '**', '**')].each do |file|
12
- zp.add(file.sub(dir, ''), file)
12
+ zip.add(file.sub(dir, ''), file)
13
13
  end
14
14
  end
15
15
  end
16
16
 
17
17
  def cbz_sub_dirs(dir)
18
- check_dir(dir) do |d|
19
- cbz_dir(d)
18
+ check_dir(dir) do |sub_dir|
19
+ cbz_dir(sub_dir)
20
20
  end
21
21
  end
22
22
 
23
23
  def all(main_dir)
24
- if Dir.exist?(main_dir)
25
- # Make sure all sub dirs are checked
26
- validate_file_or_dir_names(main_dir)
27
- # Make sure all sub dirs have files checked
28
- check_dir(main_dir) { |d| validate_file_or_dir_names(d)}
29
- # Create cbz files for all sub dirs
30
- cbz_sub_dirs(main_dir)
31
- # new line
32
- puts
33
- else
34
- puts "Cannot find directory"
24
+ # Make sure all sub dirs are checked
25
+ validate_file_or_dir_names(main_dir)
26
+ # Make sure all sub dirs have files checked
27
+ check_dir(main_dir) { |dir| validate_file_or_dir_names(dir)}
28
+ # Create cbz files for all sub dirs
29
+ cbz_sub_dirs(main_dir)
30
+ # new line
31
+ puts
32
+ end
33
+
34
+ def check_dir(dir)
35
+ Dir.glob(dir + '/*').each do |file_name|
36
+ #do block on each file name, but skip .cbz files
37
+ next if file_name.include?('.cbz')
38
+ yield(file_name)
39
+ end
40
+ end
41
+
42
+ def validate_file_or_dir_names(dir)
43
+ check_dir(dir) do |file_name|
44
+ checked_name = check_file_or_dir_name(file_name)
45
+ File.rename(file_name, checked_name)
35
46
  end
36
47
  end
48
+
49
+ def check_file_or_dir_name(name)
50
+ # slice the last number in the file or directory name,
51
+ # which will be either the page number or the chapter number
52
+ num = name.slice(/(\d+)(\.jpg)*\Z/, 1)
53
+ return unless num
54
+
55
+ zeros_to_add = (3-num.length) > 0 ? (3-num.length) : 0
56
+ num = "0" * zeros_to_add + num
57
+ name.sub(/(\d+)(\.jpg)*\Z/, num + '\2')
58
+ end
59
+
37
60
  end
38
61
  end
@@ -1,96 +1,133 @@
1
1
  module Mangdown
2
- class Chapter
2
+ class Chapter
3
3
 
4
- attr_reader :pages
4
+ attr_reader :name, :uri, :pages
5
5
 
6
- # initialize with MDHash and only have @info[:key]
7
- def initialize(info)
8
- @info = info
9
- @pages = []
6
+ def initialize(name, uri)
7
+ @manga = name.slice(/(^.+)\s/, 1)
8
+ @chapter = name.slice(/\d+\z/)
9
+ @name = name
10
+ @uri = Mangdown::Uri.new(uri)
11
+ @pages = []
12
+ @properties = Properties.new(@uri)
10
13
 
11
- get_pages
12
- end
14
+ get_pages
15
+ end
13
16
 
14
- # reader method for uri
15
- def uri
16
- @info[:uri]
17
- end
17
+ # explicit conversion to chapter
18
+ def to_chapter
19
+ self
20
+ end
18
21
 
19
- # reader method for name
20
- def name
21
- @info[:name]
22
- end
23
-
24
- # download should be in its own module
25
- # and the start_dir is sandwich code and should be moved and
26
- # passed a block
27
22
  def download
28
- start_dir = Dir.pwd
23
+ warn "#download is depreciated use #download_to"
29
24
 
30
- Dir.mkdir(@info[:name]) unless Dir.exists?(@info[:name])
31
- Dir.chdir(@info[:name])
25
+ dir = File.expand_path(@name)
26
+ Dir.mkdir(dir) unless Dir.exists?(dir)
32
27
 
33
- @pages.each {|page| page.download}
34
-
35
- Dir.chdir(start_dir)
36
- end
37
-
38
- private
39
- def get_pages
40
- uri = @info[:uri]
41
- doc = ::Mangdown::Tools.get_doc(uri)
42
-
43
- get_num_pages(doc).times do
44
- page = get_page(doc)
45
- uri = get_next_uri(doc)
46
-
47
- @pages << Page.new(page)
48
- doc = ::Mangdown::Tools.get_doc(uri)
28
+ threads = []
29
+ @pages.each do |page|
30
+ threads << Thread.new(page) do |this_page|
31
+ this_page.download_to(dir)
49
32
  end
50
33
  end
51
- end
52
34
 
53
- class MRChapter < Chapter
54
- private
55
- def get_page(doc)
56
- image = doc.css('img')[0]
35
+ threads.each {|thread| thread.join}
36
+ return @pages.length
37
+ end
57
38
 
58
- hash = MDHash.new
59
- hash[:uri], hash[:name] = image['src'], (image['alt'] + ".jpg")
60
- hash
39
+ # download all pages in a chapter
40
+ def download_to(dir)
41
+ dir = File.expand_path(dir + '/' + @name)
42
+ Dir.mkdir(dir) unless Dir.exists?(dir)
43
+
44
+ threads = []
45
+ @pages.each do |page|
46
+ threads << Thread.new(page) do |this_page|
47
+ this_page.download_to(dir)
48
+ end
61
49
  end
62
50
 
63
- def get_next_uri(doc)
64
- #root url + the href of the link to the next page
65
- ::Mangdown::Tools.get_root(@info[:uri]) +
66
- doc.css('div#imgholder a')[0]['href']
51
+ threads.each {|thread| thread.join}
52
+ return @pages.length
53
+ end
54
+
55
+ private
56
+
57
+ # get page objects for all pages in a chapter
58
+ def get_pages
59
+ threads = []
60
+ get_num_pages(get_page_doc(1)).times do |page|
61
+ threads << Thread.new(page) do |this_page|
62
+ hash = get_page(get_page_doc(this_page + 1))
63
+ @pages << hash.to_page
64
+ end
65
+ end
66
+
67
+ threads.each {|thread| thread.join}
68
+ return @pages.length
69
+ end
70
+ end
71
+
72
+ # mangareader chapter object
73
+ class MRChapter < Chapter
74
+ private
75
+
76
+ # get the doc for a given page number
77
+ def get_page_doc(num)
78
+ doc = Tools.get_doc(
79
+ Mangdown::Uri.new(
80
+ @properties.root +
81
+ "/#{@manga.gsub(' ', '-')}/#{@chapter}/#{num}"
82
+ ).downcase
83
+ )
67
84
  end
68
-
69
- def get_num_pages(doc)
70
- # the select is a dropdown menu of chapter pages
85
+
86
+ # get the page uri and name
87
+ def get_page(doc)
88
+ image = doc.css('img')[0]
89
+
90
+ MDHash.new(
91
+ uri: image['src'],
92
+ name: (image['alt'] + ".jpg")
93
+ )
94
+ end
95
+
96
+ # get the number of pages
97
+ def get_num_pages(doc)
98
+ # the select is a dropdown menu of chapter pages
71
99
  doc.css('select')[1].children.length
100
+ end
101
+ end
102
+
103
+ # mangafox chapter object
104
+ class MFChapter < Chapter
105
+ private
106
+
107
+ # get the doc for a given page number
108
+ def get_page_doc(num)
109
+ doc = Tools.get_doc(
110
+ Mangdown::Uri.new(
111
+ @properties.root +
112
+ "/manga/#{@manga.gsub(' ', '_')}/c#{@chapter}/" +
113
+ "#{num}.html"
114
+ ).downcase
115
+ )
72
116
  end
73
- end
74
-
75
- class MFChapter < Chapter
76
- private
77
- def get_page(doc)
78
- image = doc.css('img')[0]
79
117
 
80
- hash = MDHash.new
81
- hash[:uri] = image[:src]
82
- hash[:name] = image[:src].sub(/.+\//, '')
83
- hash
84
- end
85
-
86
-
87
- def get_next_uri(doc)
88
- root = @info[:uri].slice(/.+\//)
89
- root + doc.css('div#viewer a')[0][:href]
90
- end
91
-
92
- def get_num_pages(doc)
93
- doc.css('select')[1].children.length - 1
94
- end
95
- end
118
+ # get the page name and uri
119
+ def get_page(doc)
120
+ image = doc.css('img')[0]
121
+
122
+ MDHash.new(
123
+ uri: image[:src],
124
+ name: image[:src].sub(/.+\//, '')
125
+ )
126
+ end
127
+
128
+ # get the number of pages
129
+ def get_num_pages(doc)
130
+ doc.css('select')[1].children.length - 1
131
+ end
132
+ end
96
133
  end
@@ -2,74 +2,70 @@ module M
2
2
  extend self
3
3
 
4
4
  include ::Mangdown
5
- extend ::Mangdown::Tools
6
5
 
6
+ DATA_FILE_PATH = Dir.home + '/.manga_list.yml'
7
+
8
+ # check if the search key contains letters or numbers
7
9
  def valid_search?(search)
8
10
  search =~ /\w/
9
11
  end
10
-
11
- def file_path
12
- Dir.home + '/.manga_list.yml'
13
- end
14
12
 
15
- #returns a list of hash with :uri and :name of mangas found in @@list
13
+ # check if the data file is current
14
+ def file_current?(file)
15
+ File.exists?(file) ? File.mtime(file) > (Time.now - 604800) : false
16
+ end
17
+
18
+ # return a list of hash with :uri and :name of mangas found in list
16
19
  def find(search)
17
20
  unless valid_search?(search)
18
21
  puts "Searches must contain letters and numbers.."
19
22
  return []
20
23
  end
21
24
 
22
- # Previously, popular manga was used for find, but there is a
23
- # much better way to do it
24
- # PopularManga.new('http://www.mangareader.net/popular', 3000)
25
-
26
- if File.exist?(file_path)
27
- file_current = File.mtime(file_path) > (Time.now - 60*60*24*7)
28
- end
29
-
30
- if file_current
31
- @@list = YAML.load(File.open(file_path, 'r').read)
25
+ if file_current?(DATA_FILE_PATH)
26
+ list = YAML.load(File.open(DATA_FILE_PATH, 'r').read)
32
27
  else
33
- @@list = MangaList.new(
28
+ list = MangaList.new(
34
29
  'http://www.mangareader.net/alphabetical',
35
30
  'http://mangafox.me/manga/'
36
31
  )
37
32
 
38
- File.open(file_path, 'w+') do |f|
39
- f.write( @@list.to_yaml )
33
+ File.open(DATA_FILE_PATH, 'w+') do |file|
34
+ file.write( list.to_yaml )
40
35
  end
41
36
  end
42
37
 
43
- search_result = @@list.mangas.select do |manga|
38
+ search_result = list.mangas.select do |manga|
44
39
  manga[:name].downcase.include?(search.downcase)
45
40
  end
46
41
 
47
- puts "Could not find manga" if search_result.empty?
48
-
49
- search_result
42
+ search_result.empty? ? (puts "Could not find manga") : search_result
50
43
  end
51
44
 
52
- def download(manga, first_chapter = 0, last_chapter = -1)
45
+ # download a manga (accepts MDHash) between the chapters given
46
+ # by default the entire manga will be downloaded
47
+ def download(manga, first = 0, last = -1)
53
48
  if manga.class == MDHash
54
- manga = Manga.new(manga)
49
+ manga = manga.to_manga
55
50
  end
56
51
 
57
- chapters = ::Mangdown::Tools.slow_get_chapters(
58
- manga, first_chapter, last_chapter)
59
- ::Mangdown::Tools.slow_dl_chapters(chapters)
52
+ Tools::Downloader.new(manga, first, last)
60
53
  end
61
54
 
55
+ # cbz all subdirectories in a directory
62
56
  def cbz(dir)
63
- CBZ.all(dir)
57
+ Dir.exist?(dir) ? CBZ.all(dir) : (puts "Cannot find directory")
64
58
  end
65
59
 
60
+ # display help file
66
61
  def help
67
62
  help_file = File.expand_path('../../doc/help.txt',
68
63
  File.dirname(__FILE__))
69
64
  puts File.open(help_file, 'r').read
70
65
  end
71
66
 
67
+ # delete data file
72
68
  def clean_up
73
- File.delete(file_path) if File.exist?(file_path)
69
+ File.delete(DATA_FILE_PATH) if File.exist?(DATA_FILE_PATH)
74
70
  end
75
71
  end
@@ -1,12 +1,13 @@
1
1
  module Mangdown
2
+
3
+ # mangdown manga object, which holds chapters
2
4
  class Manga
3
- include ::Mangdown::Tools
4
5
 
5
- attr_reader :chapters, :chapters_list
6
+ attr_reader :name, :uri, :chapters, :chapters_list
6
7
 
7
- #initialize only use @info[:key]
8
- def initialize(info)
9
- @info = info
8
+ def initialize(name, uri)
9
+ @name = name
10
+ @uri = uri
10
11
 
11
12
  #Keeping these chapter objects in memory could be expensive
12
13
  @chapters = []
@@ -15,69 +16,41 @@ module Mangdown
15
16
  get_chapters_list
16
17
  end
17
18
 
18
- # reader method for uri
19
- def uri
20
- @info[:uri]
21
- end
22
-
23
- # reader method for name
24
- def name
25
- @info[:name]
26
- end
19
+ # explicit conversion to manga
20
+ def to_manga
21
+ self
22
+ end
27
23
 
24
+ # get push MDHashes of manga chapters to @chapters
28
25
  def get_chapters_list
29
- doc = ::Mangdown::Tools.get_doc(@info[:uri])
30
- root = ::Mangdown::Tools.get_root(@info[:uri])
26
+ properties = Properties.new(@uri)
27
+ doc = Tools.get_doc(@uri)
28
+ root = properties.root
31
29
 
32
30
  #get the link with chapter name and uri
33
- doc.css(css_klass(root)).each do |chapter|
34
- hash = MDHash.new
35
- hash[:uri] = (root + chapter[:href].sub(root, ''))
36
- hash[:name] = chapter.text
37
- @chapters_list << hash
31
+ doc.css(properties.manga_css_klass).each do |chapter|
32
+ @chapters_list << MDHash.new(
33
+ uri: (root + chapter[:href].sub(root, '')),
34
+ name: chapter.text,
35
+ )
38
36
  end
39
37
 
40
- if root =~ /mangafox/
41
- @chapters_list.reverse!
42
- end
43
- end
44
-
45
- def css_klass(site)
46
- case site
47
- when /mangareader/
48
- 'div#chapterlist td a'
49
- when /mangafox/
50
- 'a.tips'
51
- else
52
- nil
53
- end
38
+ @chapters_list.reverse! if properties.reverse
54
39
  end
55
40
 
56
- # chapter is an MDHash
41
+ # returns a MDHash if the chapter is found in @chapters
57
42
  def chapter_found(chapter)
58
43
  @chapters.find do |chp|
59
44
  (chp.name == chapter[:name]) or (chp.uri == chapter[:uri])
60
45
  end
61
46
  end
62
47
 
48
+ # pushes a Chapter object into @chapters unless it is already there
63
49
  def get_chapter(index)
64
-
65
- root = ::Mangdown::Tools.get_root(@info[:uri])
66
50
  chapter = @chapters_list[index]
67
51
 
68
52
  unless chapter_found(chapter)
69
- # this should be put in a module the case is used in
70
- # almost every class
71
- chapter_klass = case root
72
- when /mangareader/
73
- ::Mangdown::MRChapter
74
- when /mangafox/
75
- ::Mangdown::MFChapter
76
- else
77
- return :no_chapter
78
- end
79
-
80
- @chapters << chapter_klass.new(chapter)
53
+ @chapters << chapter.to_chapter
81
54
  else
82
55
  puts "This chapter has already been added"
83
56
  end
@@ -1,46 +1,27 @@
1
1
  module Mangdown
2
+
3
+ # a list of manga
2
4
  class MangaList
3
5
 
4
6
  attr_reader :mangas
5
7
 
6
8
  def initialize(*uri)
7
- @uri = uri
8
9
  @mangas = []
9
10
 
10
- @uri.each {|uri| get_mangas(uri)}
11
+ uri.each {|uri| get_mangas(uri)}
11
12
  end
12
13
 
14
+ # get a list of mangas from the uri
13
15
  def get_mangas(uri)
14
- doc = ::Mangdown::Tools.get_doc(uri)
15
- root = ::Mangdown::Tools.get_root(uri)
16
-
16
+ properties = Properties.new(uri)
17
+ doc = Tools.get_doc(uri)
17
18
 
18
19
  # This should be put in a tool
19
- doc.css(css_klass(root)).each do |a|
20
- hash = MDHash.new
21
- #hot fix
22
- url = case root
23
- when /mangareader/
24
- root + a[:href]
25
- when /mangafox/
26
- a[:href]
27
- else
28
- nil
29
- end
30
-
31
- hash[:uri], hash[:name] = url, a.text
32
- @mangas << hash
33
- end
34
- end
35
-
36
- def css_klass(site)
37
- case site
38
- when /mangareader/
39
- 'ul.series_alpha li a'
40
- when /mangafox/
41
- 'div.manga_list li a'
42
- else
43
- nil
20
+ doc.css(properties.manga_list_css_klass).each do |a|
21
+ @mangas << MDHash.new(
22
+ uri: "#{properties.manga_link_prefix}#{a[:href]}",
23
+ name: a.text
24
+ )
44
25
  end
45
26
  end
46
27
  end
@@ -0,0 +1,23 @@
1
+ module Mangdown
2
+
3
+ extend self
4
+
5
+ def to_s
6
+ "<#{self.class}::#{self.object_id}" <<
7
+ @name ? " : #{@name}" : '' <<
8
+ name ? " : #{name}" : '' <<
9
+ @uri ? " : #{@uri}" : '' <<
10
+ uri ? " : #{uri}" : '' <<
11
+ ">"
12
+ end
13
+
14
+ def eql?(other)
15
+ (self.name == other.name) and (self.uri == other.uri)
16
+ end
17
+
18
+ def ==(other)
19
+ puts "You may want to use :eql?"
20
+ super
21
+ end
22
+ end
23
+
@@ -1,8 +1,26 @@
1
1
  module Mangdown
2
2
  class MDHash < ::Hash
3
- def get_manga
4
- Manga.new(self)
3
+
4
+ def initialize(options = {})
5
+ self[:uri] = options[:uri]
6
+ self[:name] = options[:name]
7
+ end
8
+
9
+ # explicit conversion to manga
10
+ def to_manga
11
+ Manga.new(self[:name], self[:uri])
5
12
  end
13
+
14
+ # explicit conversion to chapter
15
+ def to_chapter
16
+ klass = Properties.new(self[:uri]).chapter_klass
17
+ klass.new(self[:name], self[:uri])
18
+ end
19
+
20
+ # explicit conversion to page
21
+ def to_page
22
+ Page.new(self[:name], self[:uri])
23
+ end
6
24
  end
7
25
  end
8
26
 
@@ -1,30 +1,35 @@
1
- module Mangdown
2
- class Page
1
+ class Mangdown::Page
3
2
 
4
- attr_reader :info
3
+ include Mangdown
5
4
 
6
- # like chapter and manga, should initialize with MDHash, and use
7
- # @info[:key]
8
- def initialize(info)
9
- @info = info
10
- end
5
+ attr_reader :name, :uri
11
6
 
12
- def filename
13
- @info[:name]
14
- end
7
+ def initialize(name, uri)
8
+ @name = name
9
+ @uri = Mangdown::Uri.new(uri)
10
+ end
15
11
 
16
- def uri
17
- @info[:uri]
18
- end
12
+ # explicit conversion to page
13
+ def to_page
14
+ self
15
+ end
16
+
17
+ # download the page
18
+ def download
19
+ warn "#download is depreciated use #download_to"
20
+ return nil if File.exist?(@name)
21
+ File.open(@name, 'wb') do |file|
22
+ file.write(open(uri).read)
23
+ end
24
+ end
19
25
 
20
- # this method should probably be moved somewhere else because
21
- # pages, chapters and mangas can all be "downloaded"
22
- def download
23
- unless File.exist?(@info[:name])
24
- File.open(@info[:name], 'wb') do |file|
25
- file.write(open(URI.encode(@info[:uri], '[]')).read)
26
- end
27
- end
26
+ # downloads to specified directory
27
+ def download_to(dir)
28
+ path = dir + '/' + @name
29
+ # don't download again
30
+ return nil if File.exist?(path)
31
+ File.open(path, 'wb') do |file|
32
+ file.write(open(uri).read)
28
33
  end
29
34
  end
30
35
  end
@@ -23,7 +23,7 @@ module Mangdown
23
23
 
24
24
  unless @mangas.find {|mnga| (mnga.name == manga[:name]) or
25
25
  (mnga.uri == manga[:uri])}
26
- @mangas << Manga.new(manga)
26
+ @mangas << manga.to_manga
27
27
  else
28
28
  puts "This manga has already been added.."
29
29
  end
@@ -0,0 +1,43 @@
1
+ module Mangdown
2
+ class Properties
3
+
4
+ attr_reader :info
5
+
6
+ def initialize(site)
7
+ @info = Hash.new
8
+
9
+ case site
10
+ when /mangareader/
11
+ mangareader
12
+ when /mangafox/
13
+ mangafox
14
+ else
15
+ nil
16
+ end
17
+ end
18
+
19
+ def mangareader
20
+ @info[:manga_list_css_klass] = 'ul.series_alpha li a'
21
+ @info[:manga_css_klass] = 'div#chapterlist td a'
22
+ @info[:chapter_klass] = MRChapter
23
+ @info[:root] = 'http://www.mangareader.net'
24
+ @info[:manga_link_prefix] = @info[:root]
25
+ @info[:reverse] = false
26
+ end
27
+
28
+ def mangafox
29
+ @info[:manga_list_css_klass] = 'div.manga_list li a'
30
+ @info[:manga_css_klass] = 'a.tips'
31
+ @info[:chapter_klass] = MFChapter
32
+ @info[:root] = 'http://mangafox.me'
33
+ @info[:manga_link_prefix] = ''
34
+ @info[:reverse] = true
35
+ end
36
+
37
+ private
38
+ def method_missing(method, *args, &block)
39
+ return @info[method] unless @info[method].nil?
40
+ super
41
+ end
42
+ end
43
+ end
@@ -1,140 +1,89 @@
1
+ require 'timeout'
2
+
1
3
  module Mangdown
2
4
  module Tools
3
5
  extend self
4
6
 
5
- def special?(klass)
6
- klasses = [Manga, Chapter, Page, MRChapter, MFChapter]
7
-
8
- if klasses.find(self.class)
9
- true
10
- else
11
- false
12
- end
13
- end
7
+ def return_to_start_dir
8
+ start = Dir.pwd
9
+ yield
10
+ Dir.chdir(start)
11
+ rescue Exception => error
12
+ Dir.chdir(start)
13
+ raise error
14
+ end
14
15
 
15
16
  def get_doc(uri)
16
- @doc = ::Nokogiri::HTML(open(uri))
17
+ @doc = ::Nokogiri::HTML(open(uri))
17
18
  end
18
19
 
19
20
  def get_root(uri)
20
21
  @root = ::URI::join(uri, "/").to_s[0..-2]
21
22
  end
22
-
23
- def to_s
24
- if special?(self.class)
25
- "<#{self.class}::#{self.object_id} : #{@name} : #{@uri}>"
26
- else
27
- super
28
- end
29
- end
30
-
31
- def eql?(other)
32
- if special?(self.class)
33
- (self.name == other.name) and (self.uri == other.uri)
34
- else
35
- super
36
- end
37
- end
38
-
39
- def ==(other)
40
- if special?(self.class)
41
- puts 'You may want to use eql?'
42
- end
43
-
44
- super
45
- end
46
-
23
+
47
24
  def no_time_out(tries = 3, &block)
48
25
  tries -= 1
49
26
  begin
50
- timeout(120) do
51
- yield
52
- end
53
- rescue
54
- if tries >= 0
55
- puts "Tries left: #{tries}"
56
- no_time_out(tries, &block)
57
- else
58
- return :timed_out
59
- end
27
+ timeout(120) { yield }
28
+ rescue Timeout::Error
29
+ tries >= 0 ? no_time_out(tries, &block) : :timed_out
60
30
  end
61
31
  end
62
32
 
63
- def slow_get_chapters(manga, bgn, nd)
64
- @@file_name = File.expand_path("#{manga.name}_temp.yml")
33
+ class Downloader
34
+ include Tools
65
35
 
66
- # get Manga object from file if it exists
67
- if File.exist?(@@file_name)
68
- manga_from_file = YAML.load(File.open(@@file_name, 'r').read)
69
- end
36
+ def initialize(manga, first, last)
37
+ @tempfile = File.expand_path("#{manga.name}_temp.yml")
70
38
 
71
- if manga_from_file and (manga_from_file.chapters.length > 0)
72
- frst = (manga.chapters_list[bgn][:name] ==
73
- manga_from_file.chapters.first.name)
74
- lst = (manga.chapters_list[nd][:name] ==
75
- manga_from_file.chapters.last.name)
76
- # check if this is the right temp file
77
- manga = manga_from_file if (frst and lst)
78
- else
79
- manga.chapters_list[bgn..nd].each_index do |i|
80
- no_time_out {manga.get_chapter(i + bgn)}
81
- end
39
+ chapters = slow_get_chapters(manga, first, last)
40
+ slow_dl_chapters(chapters)
82
41
  end
83
42
 
84
- File.open(@@file_name, 'w') {|f| f.write(manga.to_yaml)}
85
-
86
- manga
87
- end
88
-
89
- def slow_dl_chapters(manga)
90
- start_dir = Dir.pwd
91
- Dir.mkdir(manga.name) unless Dir.exist?(manga.name)
92
- Dir.chdir(manga.name)
93
-
94
- manga_start_dir = Dir.pwd
95
- manga.chapters.each do |chap|
96
- no_time_out do
97
- Dir.chdir(manga_start_dir)
98
- chap.download
43
+ def slow_get_chapters(manga, bgn, nd)
44
+ # get Manga object from file if it exists
45
+ if File.exist?(@tempfile)
46
+ manga_from_file = YAML
47
+ .load(File.open(@tempfile, 'r').read)
99
48
  end
100
- end
101
49
 
102
- File.delete(@@file_name)
103
- Dir.chdir(start_dir)
104
- end
105
-
106
- def check_file_or_dir_name(name)
107
-
108
- # slice the last number in the file or directory name,
109
- # which will be either the page number or the chapter number
110
- num = name.slice(/(\d+)(\.jpg)*\Z/, 1)
111
-
112
- if num
113
- while num.length < 3
114
- num = '0' + num
50
+ # if the manga is from tempfile and the manga has chapters
51
+ # then check if it has the right chapters
52
+ if manga_from_file and (manga_from_file.chapters.length > 0)
53
+ frst = (manga.chapters_list[bgn][:name] ==
54
+ manga_from_file.chapters.first.name)
55
+ lst = (manga.chapters_list[nd][:name] ==
56
+ manga_from_file.chapters.last.name)
57
+ manga = manga_from_file if (frst and lst)
58
+ else
59
+ threads = []
60
+ manga.chapters_list[bgn..nd].each_index do |index|
61
+ threads << Thread.new(index) do |i|
62
+ no_time_out {manga.get_chapter(i + bgn)}
63
+ end
64
+ end
65
+ threads.each {|thread| thread.join}
115
66
  end
116
-
117
- name = name.sub(/(\d+)(\.jpg)*\Z/, num + '\2')
118
- end
119
67
 
120
- name
121
- end
68
+ File.open(@tempfile, 'w') {|f| f.write(manga.to_yaml)}
122
69
 
123
- def check_dir(dir)
124
- Dir.glob(dir + '/*').each do |file_name|
125
- #do block on each file name, but skip .cbz files
126
- next if file_name.include?('.cbz')
127
- yield(file_name)
70
+ manga
128
71
  end
129
- end
130
72
 
131
- def validate_file_or_dir_names(dir)
132
- check_dir(dir) do |file_name|
133
- checked_name = check_file_or_dir_name(file_name)
73
+ def slow_dl_chapters(manga)
74
+ dir = File.expand_path(manga.name)
75
+ Dir.mkdir(dir) unless Dir.exist?(dir)
134
76
 
135
- unless file_name == checked_name
136
- File.rename(file_name, checked_name)
77
+ threads = []
78
+ manga.chapters.each do |chap|
79
+ threads << Thread.new(chap) do |this_chap|
80
+ no_time_out {this_chap.download_to(dir)}
81
+ end
137
82
  end
83
+
84
+ threads.each {|thread| thread.join}
85
+
86
+ File.delete(@tempfile)
138
87
  end
139
88
  end
140
89
  end
@@ -0,0 +1,8 @@
1
+ module Mangdown::Uri
2
+
3
+ extend self
4
+
5
+ def new(uri)
6
+ URI.encode(uri, '[]')
7
+ end
8
+ end
@@ -0,0 +1,5 @@
1
+ class Chapter < ActiveRecord::Base
2
+
3
+ belongs_to :manga
4
+ has_many :pages
5
+ end
@@ -0,0 +1,6 @@
1
+ class Manga < ActiveRecord::Base
2
+
3
+ belongs_to :site
4
+ has_many :chapters
5
+ end
6
+
@@ -0,0 +1,4 @@
1
+ class Page < ActiveRecord::Base
2
+
3
+ belongs_to :chapter
4
+ end
@@ -0,0 +1,4 @@
1
+ class Site < ActiveRecord::Base
2
+
3
+ has_many :mangas
4
+ end
@@ -1,11 +1,14 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module Mangdown
4
- @@chapter_hash = MDHash.new
5
- @@chapter_hash[:uri] = 'http://www.mangareader.net/bleach/537'
6
- @@chapter_hash[:name] = 'Bleach 537'
7
-
8
- chapter = MRChapter.new(@@chapter_hash)
4
+ @@chapter_hash = MDHash.new(
5
+ uri: 'http://www.mangareader.net/bleach/537',
6
+ name: 'Bleach 537',
7
+ manga: 'Bleach',
8
+ chapter: '537',
9
+ )
10
+
11
+ chapter = @@chapter_hash.to_chapter
9
12
  chapter.download
10
13
 
11
14
  STUB_PATH = File.expand_path('../../objects/chapter.yml', __FILE__)
@@ -30,48 +33,31 @@ module Mangdown
30
33
  end
31
34
 
32
35
  it "should have the right first page at the 0 index" do
33
- expect(@chapter.pages.first.filename).to eq(
36
+ expect(@chapter.pages.first.name).to eq(
34
37
  'Bleach 537 - Page 1.jpg')
35
38
  end
36
39
 
37
40
  it "should have the right last page at the -1 index" do
38
- expect(@chapter.pages.last.filename).to eq(
41
+ expect(@chapter.pages.last.name).to eq(
39
42
  'Bleach 537 - Page 21.jpg')
40
43
  end
41
44
 
42
45
  context "as a MFChapter" do
43
46
  it "should have pages" do
44
- hash = MDHash.new
45
- hash[:uri] =
46
- 'http://mangafox.me/manga/kitsune_no_yomeiri/v01/c001/1.html'
47
- hash[:name] = 'Kitsune no Yomeiri 1'
48
-
49
- chapter = MFChapter.new(hash)
47
+ hash = MDHash.new(
48
+ uri: 'http://mangafox.me/manga/kitsune_no_yomeiri/v01/c001/1.html',
49
+ name: 'Kitsune no Yomeiri 1',
50
+ manga: 'Kitsune no Yomeiri',
51
+ chapter: 1,
52
+ )
53
+
54
+ chapter = hash.to_chapter
50
55
 
51
56
  expect(chapter.pages).not_to be_empty
52
57
  end
53
58
  end
54
59
  end
55
60
 
56
- # Move these to Tools spec
57
- =begin
58
- context "when the functions for get_pages are run" do
59
- it "should have the right chapter uri" do
60
- expect(@chapter.uri).to eq(@@uri)
61
- end
62
-
63
- it "should get the right image link and filename from a uri" do
64
- expect(@chapter.get_page).to eq(
65
- ['http://i25.mangareader.net/bleach/537/bleach-4149721.jpg',
66
- 'Bleach 537 - Page 1'])
67
- end
68
-
69
- it "should get the right root from uri" do
70
- expect(@chapter.get_root(@@uri)).to eq('http://www.mangareader.net')
71
- end
72
- end
73
- =end
74
-
75
61
  context "when chapter is downloaded" do
76
62
  it "should have a sub directory in the current directory" do
77
63
  dir = Dir.pwd
@@ -82,13 +68,9 @@ module Mangdown
82
68
 
83
69
  it "should download all it's pages" do
84
70
  dir = Dir.pwd + '/' + @chapter.name
85
- # expect(dir).to include("mangdown/#{@chapter.name}")
86
71
 
87
72
  pages_in_dir = Dir.glob(dir + '/*.jpg').length
88
73
  expect(pages_in_dir).to eq(@chapter.pages.length)
89
- #@chapter.pages.each do |page|
90
- # expect(Dir.glob(dir + '/*' )).to include(dir +
91
- # '/' + page.filename)
92
74
  end
93
75
  end
94
76
  end
@@ -4,15 +4,17 @@ module M
4
4
 
5
5
  describe "commands" do
6
6
  before(:all) do
7
- hash = MDHash.new
8
- hash[:uri] = 'http://www.mangareader.net/6-no-trigger'
9
- hash[:name] = '6 no Trigger'
10
- @manga = Manga.new(hash)
7
+ hash = MDHash.new(
8
+ uri: 'http://www.mangareader.net/6-no-trigger',
9
+ name: '6 no Trigger'
10
+ )
11
+ @manga = hash.to_manga
11
12
 
12
- hash = MDHash.new
13
- hash[:uri] = 'http://mangafox.me/manga/naruto/'
14
- hash[:name] = 'Naruto'
15
- @mf_manga = Manga.new(hash)
13
+ hash = MDHash.new(
14
+ uri: 'http://mangafox.me/manga/naruto/',
15
+ name: 'Naruto'
16
+ )
17
+ @mf_manga = hash.to_manga
16
18
 
17
19
  M.download(@manga, 1, 3)
18
20
  M.cbz("./#{@manga.name}")
@@ -46,6 +48,7 @@ module M
46
48
 
47
49
  it "should download a manga from MF" do
48
50
  dir = Dir.pwd
51
+ #@mf_manga.remove_chapters
49
52
  M.download(@mf_manga, 500, 501)
50
53
  expect(Dir.glob(dir + "/#{@mf_manga.name}/*").length).to eq(2)
51
54
  end
@@ -1,11 +1,12 @@
1
1
  require_relative '../spec_helper'
2
2
 
3
3
  module Mangdown
4
- @@manga_hash = MDHash.new
5
- @@manga_hash[:uri] = 'http://www.mangareader.net/94/bleach.html'
6
- @@manga_hash[:name] = 'Bleach'
4
+ @@manga_hash = MDHash.new(
5
+ uri: 'http://www.mangareader.net/94/bleach.html',
6
+ name: 'Bleach'
7
+ )
7
8
 
8
- manga = Manga.new(@@manga_hash)
9
+ manga = @@manga_hash.to_manga
9
10
 
10
11
  MANGA_STUB_PATH = File.expand_path('../../objects/manga.yml',
11
12
  __FILE__)
@@ -19,7 +20,6 @@ module Mangdown
19
20
  before(:each) do
20
21
  print '@'
21
22
  @manga = YAML.load(File.open(Mangdown::MANGA_STUB_PATH, 'r').read)
22
- #@manga.get_doc(@@manga_hash[:uri])
23
23
  end
24
24
 
25
25
  context "When a manga is initialized" do
@@ -33,10 +33,11 @@ module Mangdown
33
33
 
34
34
  context "as a MangaFox manga" do
35
35
  it "should have chapters" do
36
- hash = MDHash.new
37
- hash[:uri] = 'http://mangafox.me/manga/masca_the_beginning/'
38
- hash[:name] = 'Masca: The Beginning'
39
- manga = Manga.new(hash)
36
+ hash = MDHash.new(
37
+ uri: 'http://mangafox.me/manga/masca_the_beginning/',
38
+ name: 'Masca: The Beginning'
39
+ )
40
+ manga = hash.to_manga
40
41
  expect(manga.chapters_list).not_to be_empty
41
42
  end
42
43
  end
@@ -50,13 +51,15 @@ module Mangdown
50
51
  it "should have the right first chapter" do
51
52
  expect(@manga.chapters_list.first).to eq({
52
53
  uri:'http://www.mangareader.net/94-8-1/bleach/chapter-1.html',
53
- name: 'Bleach 1'})
54
+ name: 'Bleach 1',
55
+ })
54
56
  end
55
57
 
56
58
  it "should have the right 465th chapter" do
57
59
  expect(@manga.chapters_list[464]).to eq({
58
60
  uri: 'http://www.mangareader.net/bleach/465',
59
- name: 'Bleach 465'})
61
+ name: 'Bleach 465',
62
+ })
60
63
  end
61
64
  end
62
65
 
@@ -4,13 +4,14 @@ module Mangdown
4
4
 
5
5
  describe MDHash do
6
6
  before(:all) do
7
- @hash = MDHash.new
8
- @hash[:uri] = 'http://www.mangareader.net/103/one-piece.html'
9
- @hash[:name] = 'One Piece'
7
+ @hash = MDHash.new(
8
+ uri: 'http://www.mangareader.net/103/one-piece.html',
9
+ name: 'One Piece'
10
+ )
10
11
  end
11
12
 
12
13
  it "should get a manga object from get_manga" do
13
- expect(@hash.get_manga).to be_kind_of(Manga)
14
+ expect(@hash.to_manga).to be_kind_of(Manga)
14
15
  end
15
16
  end
16
17
  end
@@ -4,12 +4,12 @@ module Mangdown
4
4
  describe Page do
5
5
  before(:all) do
6
6
  @dir = Dir.pwd
7
- @page_hash = MDHash.new
8
- @page_hash[:uri] =
9
- 'http://i25.mangareader.net/bleach/537/bleach-4149721.jpg'
10
- @page_hash[:name] = "Bleach 537 - Page 1"
7
+ @page_hash = MDHash.new(
8
+ uri: 'http://i25.mangareader.net/bleach/537/bleach-4149721.jpg',
9
+ name: "Bleach 537 - Page 1"
10
+ )
11
11
 
12
- @page = Page.new(@page_hash)
12
+ @page = @page_hash.to_page
13
13
  @page.download
14
14
  PAGE_STUB_PATH = File.expand_path('../../objects/page.yml',
15
15
  __FILE__)
@@ -22,7 +22,7 @@ module Mangdown
22
22
 
23
23
  context "when page is downloaded" do
24
24
  it "should download itself to the current directory" do
25
- expect(Dir.glob(@dir + '/*')).to include(@dir + '/' + @page.filename)
25
+ expect(Dir.glob(@dir + '/*')).to include(@dir + '/' + @page.name)
26
26
  end
27
27
  end
28
28
 
@@ -0,0 +1,20 @@
1
+ # Move these to a Tools spec
2
+ =begin
3
+ context "when the functions for get_pages are run" do
4
+ it "should have the right chapter uri" do
5
+ expect(@chapter.uri).to eq(@@uri)
6
+ end
7
+
8
+ it "should get the right image link and name from a uri" do
9
+ expect(@chapter.get_page).to eq(
10
+ ['http://i25.mangareader.net/bleach/537/bleach-4149721.jpg',
11
+ 'Bleach 537 - Page 1'])
12
+ end
13
+
14
+ it "should get the right root from uri" do
15
+ expect(@chapter.get_root(@@uri)).to eq('http://www.mangareader.net')
16
+ end
17
+ end
18
+ =end
19
+
20
+
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mangdown
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.3
4
+ version: 0.8.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - jphager2
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-02-14 00:00:00.000000000 Z
11
+ date: 2014-06-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: A gem to download Manga
13
+ description: A gem to download Manga, (pg integration in dev)
14
14
  email: jphager2@gmail.com
15
15
  executables: []
16
16
  extensions: []
@@ -24,10 +24,17 @@ files:
24
24
  - lib/mangdown/commands.rb
25
25
  - lib/mangdown/manga.rb
26
26
  - lib/mangdown/manga_list.rb
27
+ - lib/mangdown/mangdown.rb
27
28
  - lib/mangdown/mangdown_hash.rb
28
29
  - lib/mangdown/page.rb
29
30
  - lib/mangdown/popular.rb
31
+ - lib/mangdown/properties.rb
30
32
  - lib/mangdown/tools.rb
33
+ - lib/mangdown/uri.rb
34
+ - models/chapter.rb
35
+ - models/manga.rb
36
+ - models/page.rb
37
+ - models/site.rb
31
38
  - spec/mangdown/chapter_spec.rb
32
39
  - spec/mangdown/commands_spec.rb
33
40
  - spec/mangdown/manga_list_spec.rb
@@ -35,9 +42,10 @@ files:
35
42
  - spec/mangdown/mangdown_hash_spec.rb
36
43
  - spec/mangdown/page_spec.rb
37
44
  - spec/mangdown/popular_spec.rb
45
+ - spec/mangdown/tools_spec.rb
38
46
  - spec/objects/make_chapter.rb
39
47
  - spec/spec_helper.rb
40
- homepage:
48
+ homepage: https://github.com/jphager2/mangdown
41
49
  licenses:
42
50
  - MIT
43
51
  metadata: {}