mangdown 0.7.1 → 0.7.2

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,46 +1,47 @@
1
- module Mangdown
2
- class Manga
3
- include ::Mangdown::Tools
4
-
5
- attr_reader :chapters, :chapters_list, :name, :uri
6
-
7
- def initialize(uri, name)
8
- @uri = uri
9
- @name = name
10
- @chapters = []
11
- @chapters_list = []
12
- @doc = get_doc(@uri)
13
- @root = get_root(@uri)
14
-
15
- get_chapters_list
16
- end
17
-
18
- def get_chapters_list
19
- @doc.css('div#chapterlist td a').each do |chapter|
20
- @chapters_list << ([@root + chapter['href'], chapter.text])
21
- end
22
-
23
- @doc = "Nokogiri::HTML::Document"
24
- end
25
-
26
- def get_chapter(index)
27
-
28
- uri, name = @chapters_list[index]
29
-
30
- unless @chapters.find {|chp| (chp.name == name) or (chp.uri == uri)}
31
- # this is far from ideal
32
- chapter_klass = if @root.include?('mangareader')
33
- MRChapter
34
- elsif @root.include?('fakku')
35
- FKChapter
36
- else
37
- NO_Chapter
38
- end
39
-
40
- @chapters << chapter_klass.new(uri, name)
41
- else
42
- nil
43
- end
44
- end
45
- end
46
- end
1
+ module Mangdown
2
+ class Manga
3
+ include ::Mangdown::Tools
4
+
5
+ attr_reader :chapters, :chapters_list, :name, :uri
6
+
7
+ def initialize(uri, name)
8
+ @uri = uri
9
+ @name = name
10
+
11
+ #Keeping these chapter objects in memory could be expensive
12
+ @chapters = []
13
+ @chapters_list = []
14
+
15
+ get_chapters_list
16
+ end
17
+
18
+ def get_chapters_list
19
+ doc = ::Mangdown::Tools.get_doc(@uri)
20
+ root = ::Mangdown::Tools.get_root(@uri)
21
+
22
+ #get the link with chapter name and uri
23
+ doc.css('div#chapterlist td a').each do |chapter|
24
+ @chapters_list << ([root + chapter['href'], chapter.text])
25
+ end
26
+ end
27
+
28
+ def get_chapter(index)
29
+
30
+ root = ::Mangdown::Tools.get_root(@uri)
31
+ uri, name = @chapters_list[index]
32
+
33
+ unless @chapters.find {|chp| (chp.name == name) or (chp.uri == uri)}
34
+ # this is far from ideal
35
+ chapter_klass = if root.include?('mangareader')
36
+ MRChapter
37
+ else
38
+ NO_Chapter
39
+ end
40
+
41
+ @chapters << chapter_klass.new(uri, name)
42
+ else
43
+ puts "This chapter has already been added"
44
+ end
45
+ end
46
+ end
47
+ end
@@ -1,8 +1,8 @@
1
- module Mangdown
2
- class MDHash < ::Hash
3
- def get_manga
4
- Manga.new(self[:uri], self[:name])
5
- end
6
- end
7
- end
8
-
1
+ module Mangdown
2
+ class MDHash < ::Hash
3
+ def get_manga
4
+ Manga.new(self[:uri], self[:name])
5
+ end
6
+ end
7
+ end
8
+
@@ -1,16 +1,21 @@
1
- module Mangdown
2
- class Page
3
- attr_reader :filename, :uri
4
-
5
- def initialize( uri, filename )
6
- @filename = filename + '.jpg'
7
- @uri = uri
8
- end
9
-
10
- def download
11
- File.open(@filename, 'wb') do |file|
12
- file.write(open(URI.encode(@uri, '[]')).read)
13
- end
14
- end
15
- end
16
- end
1
+ module Mangdown
2
+ class Page
3
+ attr_reader :filename, :uri
4
+
5
+ def initialize( uri, filename )
6
+
7
+ # this will probably become a problem when adding different
8
+ # Manga sites which may use different file types than .jpg
9
+ @filename = filename + '.jpg'
10
+ @uri = uri
11
+ end
12
+
13
+ # this method should probably be moved somewhere else because
14
+ # pages, chapters and mangas can all be "downloaded"
15
+ def download
16
+ File.open(@filename, 'wb') do |file|
17
+ file.write(open(URI.encode(@uri, '[]')).read)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -1,52 +1,63 @@
1
- module Mangdown
2
- class PopularManga
3
- include ::Mangdown::Tools
4
-
5
- attr_reader :uri, :mangas_list, :mangas, :name
6
-
7
- def initialize(uri, num_mangas, name = "My Pop Manga")
8
- @uri = uri
9
- @num_mangas = num_mangas
10
- @name = name
11
-
12
- @mangas_list = []
13
- @mangas = []
14
-
15
- @root = get_root(@uri)
16
-
17
- get_mangas_list
18
- end
19
-
20
- def get_manga(index)
21
- uri, name = @mangas_list[index - 1]
22
- unless @mangas.find {|manga| (manga.name == name) or (manga.uri == uri)}
23
- @mangas << Manga.new( uri, name )
24
- else
25
- nil
26
- end
27
- end
28
-
29
- def get_mangas_list
30
- (@num_mangas / 30.0).ceil.times do |time|
31
- get_pop_page_manga(time).each { |manga| @mangas_list << manga }
32
- end
33
-
34
- @doc = "Nokogiri::HTML::Document"
35
- end
36
-
37
- def get_pop_page_manga(time)
38
- num = 30 * (time)
39
- page = @root + '/popular/' + num.to_s
40
- get_doc(page)
41
-
42
- last = (@num_mangas > 30) ? 30 : @num_mangas
43
- @num_mangas -= 30
44
-
45
- get_manga_on_page[0..(last - 1)]
46
- end
47
-
48
- def get_manga_on_page
49
- @doc.css('h3 a').map {|a| [@root + a['href'], a.text]}
50
- end
51
- end
52
- end
1
+ module Mangdown
2
+ class PopularManga
3
+
4
+ attr_reader :uri, :mangas_list, :mangas, :name
5
+
6
+ def initialize(uri, num_mangas = 0, name = "My Pop Manga")
7
+ @uri = uri
8
+ @num_mangas = num_mangas
9
+ @name = name
10
+
11
+ @mangas_list = []
12
+
13
+ #Depreciated
14
+ @mangas = []
15
+
16
+ get_mangas_list
17
+ end
18
+
19
+ #Depreciated
20
+ def get_manga(number)
21
+ puts "This has been depreciated, don't use PopularManga @mangas!"
22
+ uri = @mangas_list[number - 1][:uri]
23
+ name = @mangas_list[number - 1][:name]
24
+
25
+ unless @mangas.find {|manga| (manga.name == name) or
26
+ (manga.uri == uri)}
27
+ @mangas << Manga.new( uri, name )
28
+ else
29
+ puts "This manga has already been added.."
30
+ end
31
+ end
32
+
33
+ private
34
+ def get_mangas_list
35
+ (@num_mangas / 30.0).ceil.times do |time|
36
+ get_pop_page_manga(time).each do |manga|
37
+ @mangas_list << manga
38
+ end
39
+ end
40
+ end
41
+
42
+ def get_pop_page_manga(time)
43
+ root = ::Mangdown::Tools.get_root(@uri)
44
+
45
+ num = 30 * (time)
46
+ page = root + '/popular/' + num.to_s
47
+ doc = ::Mangdown::Tools.get_doc(page)
48
+
49
+ last = (@num_mangas > 30) ? 30 : @num_mangas
50
+ @num_mangas -= 30
51
+
52
+ get_manga_on_page(doc, root)[0..(last - 1)]
53
+ end
54
+
55
+ def get_manga_on_page(doc, root)
56
+ doc.css('h3 a').map do |a|
57
+ h = MDHash.new
58
+ h[:uri], h[:name] = (root + a['href']), a.text
59
+ h
60
+ end
61
+ end
62
+ end
63
+ end
@@ -1,110 +1,114 @@
1
- module Mangdown
2
- module Tools
3
- extend self
4
-
5
- def get_doc(uri)
6
- @doc = ::Nokogiri::HTML(open(uri))
7
- end
8
-
9
- def get_root(uri)
10
- @root = ::URI::join(uri, "/").to_s[0..-2]
11
- end
12
-
13
- def to_s
14
- "<#{self.class}::#{self.object_id} : #{@name} : #{@uri}>"
15
- end
16
-
17
- def eql?(other)
18
- (@name == other.name) and (@uri == other.uri)
19
- end
20
-
21
- def ==(other)
22
- puts 'You may want to use :eql?'
23
- super
24
- end
25
-
26
- def no_time_out(tries = 3)
27
- begin
28
- timeout(120) do
29
- yield
30
- end
31
- rescue
32
- if tries > 0
33
- tries -= 1
34
- puts "Tries left: #{tries}"
35
- no_time_out(tries)
36
- else
37
- return
38
- end
39
- end
40
- end
41
-
42
- def slow_get_chapters(manga, bgn, nd)
43
- @@file_name = File.expand_path("#{manga.name}_temp.yml")
44
-
45
- exists = File.exist?(@@file_name)
46
- manga_from_file = YAML.load(File.open(@@file_name, 'r').read) if exists
47
-
48
- if manga_from_file and (manga_from_file.chapters.length > 0)
49
- frst = (manga.chapters_list[bgn][1] ==
50
- manga_from_file.chapters.first.name)
51
- lst = (manga.chapters_list[nd][1] == manga_from_file.chapters.last.name)
52
- manga = manga_from_file if (frst and lst)
53
- else
54
- manga.chapters_list[bgn..nd].each_index do |i|
55
- no_time_out {manga.get_chapter(i + bgn)}
56
- end
57
- end
58
-
59
- File.open(@@file_name, 'w') {|f| f.write(manga.to_yaml)}
60
-
61
- manga
62
- end
63
-
64
- def slow_dl_chapters(manga)
65
- start_dir = Dir.pwd
66
- Dir.mkdir(manga.name) unless Dir.exist?(manga.name)
67
- Dir.chdir(manga.name)
68
-
69
- manga.chapters.each do |chap|
70
- # puts "DL - #{chap.name}.."
71
- no_time_out {chap.download}
72
- end
73
-
74
- File.delete(@@file_name)
75
- Dir.chdir(start_dir)
76
- end
77
-
78
- def check_file_or_dir_name(name)
79
- num = name.slice(/(\d+)(\.jpg)*\Z/, 1)
80
-
81
- if num
82
- while num.length < 3
83
- num = '0' + num
84
- end
85
-
86
- name = name.sub(/(\d+)(\.jpg)*\Z/, num + '\2')
87
- end
88
-
89
- name
90
- end
91
-
92
- def check_dir(dir)
93
- Dir.glob(dir + '/*').each do |d|
94
- next if d.include?('.cbz')
95
- yield(d)
96
- end
97
- end
98
-
99
- def validate_file_or_dir_names(dir)
100
- check_dir(dir) do |e|
101
- f = check_file_or_dir_name(e)
102
-
103
- unless f == e
104
- File.rename(e, f)
105
- end
106
- end
107
- end
108
- end
109
- end
110
-
1
+ module Mangdown
2
+ module Tools
3
+ extend self
4
+
5
+ def get_doc(uri)
6
+ @doc = ::Nokogiri::HTML(open(uri))
7
+ end
8
+
9
+ def get_root(uri)
10
+ @root = ::URI::join(uri, "/").to_s[0..-2]
11
+ end
12
+
13
+ def to_s
14
+ "<#{self.class}::#{self.object_id} : #{@name} : #{@uri}>"
15
+ end
16
+
17
+ def eql?(other)
18
+ (@name == other.name) and (@uri == other.uri)
19
+ end
20
+
21
+ def ==(other)
22
+ puts 'You may want to use :eql?'
23
+ super
24
+ end
25
+
26
+ def no_time_out(tries = 3)
27
+ begin
28
+ timeout(120) do
29
+ yield
30
+ end
31
+ rescue
32
+ if tries > 0
33
+ tries -= 1
34
+ puts "Tries left: #{tries}"
35
+ no_time_out(tries)
36
+ else
37
+ return
38
+ end
39
+ end
40
+ end
41
+
42
+ def slow_get_chapters(manga, bgn, nd)
43
+ @@file_name = File.expand_path("#{manga.name}_temp.yml")
44
+
45
+ exists = File.exist?(@@file_name)
46
+
47
+ if exists
48
+ manga_from_file = YAML.load(File.open(@@file_name, 'r').read)
49
+ end
50
+
51
+ if manga_from_file and (manga_from_file.chapters.length > 0)
52
+ frst = (manga.chapters_list[bgn][1] ==
53
+ manga_from_file.chapters.first.name)
54
+ lst = (manga.chapters_list[nd][1] ==
55
+ manga_from_file.chapters.last.name)
56
+ manga = manga_from_file if (frst and lst)
57
+ else
58
+ manga.chapters_list[bgn..nd].each_index do |i|
59
+ no_time_out {manga.get_chapter(i + bgn)}
60
+ end
61
+ end
62
+
63
+ File.open(@@file_name, 'w') {|f| f.write(manga.to_yaml)}
64
+
65
+ manga
66
+ end
67
+
68
+ def slow_dl_chapters(manga)
69
+ start_dir = Dir.pwd
70
+ Dir.mkdir(manga.name) unless Dir.exist?(manga.name)
71
+ Dir.chdir(manga.name)
72
+
73
+ manga.chapters.each do |chap|
74
+ # puts "DL - #{chap.name}.."
75
+ no_time_out {chap.download}
76
+ end
77
+
78
+ File.delete(@@file_name)
79
+ Dir.chdir(start_dir)
80
+ end
81
+
82
+ def check_file_or_dir_name(name)
83
+ num = name.slice(/(\d+)(\.jpg)*\Z/, 1)
84
+
85
+ if num
86
+ while num.length < 3
87
+ num = '0' + num
88
+ end
89
+
90
+ name = name.sub(/(\d+)(\.jpg)*\Z/, num + '\2')
91
+ end
92
+
93
+ name
94
+ end
95
+
96
+ def check_dir(dir)
97
+ Dir.glob(dir + '/*').each do |d|
98
+ next if d.include?('.cbz')
99
+ yield(d)
100
+ end
101
+ end
102
+
103
+ def validate_file_or_dir_names(dir)
104
+ check_dir(dir) do |e|
105
+ f = check_file_or_dir_name(e)
106
+
107
+ unless f == e
108
+ File.rename(e, f)
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
114
+