epub_book 0.1.15 → 0.1.16

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: bcf2e6b52c434a9c609291c5815c524227caa0b9
4
- data.tar.gz: 06059a204f8a130809c19fd9bc386fe79681267b
3
+ metadata.gz: 8a69bb745d60d25bdb4eeb2b6a6dcc2b0d115b4e
4
+ data.tar.gz: 86a99f95e8a35e0fa12163c784eb2cfe56aad574
5
5
  SHA512:
6
- metadata.gz: d53320d61f9f247fecd630b8ca5540677ec4a8a9263d7803216f8ee5373f8ad87eff281cc1eda2aba22006c83b6ddcd31b0d7692aada7ee77d3ff0571730f782
7
- data.tar.gz: cbec566608c5d506ccb5d74d901b9a3f9bd8f5fe84fff38e9a67b2fb2ed18746a19825dd396c84dec87494eaafd60f9b0fdd026abdca8e5351ed8483efede3ad
6
+ metadata.gz: e1d24a611b214d0f4a37d8f0c67bf0285095a1c8f51f87b6c747b019ae70345eba11189962b1e42300e0570fe96aa22c59fde873a8e10563fa7a23102c422518
7
+ data.tar.gz: 6a2f053ca97e68a5db5e83278d171a82bab14e2fb51a2399ae2889c1e45e95ac62f539858a4430640c9c9d04ccbe74fd105b6b559fcdb8efb83b695e37b5937d
data/epub_book.gemspec CHANGED
@@ -27,15 +27,16 @@ Gem::Specification.new do |spec|
27
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
28
  spec.require_paths = ["lib"]
29
29
 
30
- spec.add_dependency 'nokogiri','~> 1.6.8'
31
- spec.add_dependency 'eeepub'
32
- spec.add_dependency 'zip-zip'
33
- spec.add_dependency 'mail'
30
+ spec.add_dependency 'http','~> 0'
31
+ spec.add_dependency 'nokogiri','~> 0'
32
+ spec.add_dependency 'eeepub','~> 0'
33
+ spec.add_dependency 'zip-zip','~> 0'
34
+ spec.add_dependency 'mail','~> 0'
34
35
 
35
36
 
36
37
  spec.add_development_dependency "bundler", "~> 1.12"
37
38
  spec.add_development_dependency "rake", "~> 10.0"
38
39
  spec.add_development_dependency "rspec", "~> 3.0"
39
- spec.add_development_dependency "pry"
40
- spec.add_development_dependency "pry-doc"
40
+ spec.add_development_dependency "pry",'~> 0'
41
+ spec.add_development_dependency "pry-doc",'~> 0'
41
42
  end
@@ -1,4 +1,4 @@
1
- require 'open-uri'
1
+ require 'http'
2
2
  require 'nokogiri'
3
3
  require 'eeepub'
4
4
  require 'base64'
@@ -21,8 +21,7 @@ module EpubBook
21
21
  class Book
22
22
  UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36"
23
23
  Referer = "http://www.baidu.com/"
24
- attr_accessor :title_css, :index_item_css, :body_css, :limit, :item_attr, :page_css, :page_attr,:cover
25
- attr_accessor :cover_css, :description_css,:path,:user_agent,:referer,:creator,:mail_to
24
+ attr_accessor :title_css, :index_item_css, :body_css, :limit, :item_attr, :page_css, :page_attr,:cover,:cover_css, :description_css,:path,:user_agent,:referer,:creator,:mail_to, :folder_name,:des_url
26
25
 
27
26
 
28
27
  Reg = /<script.*?>.*?<\/script>/m
@@ -40,21 +39,24 @@ module EpubBook
40
39
  @body_css = '.articlebody'
41
40
  @item_attr = "href"
42
41
  yield self if block_given?
43
- @book_path = File.join((@path || `pwd`.strip), @folder_name)
44
42
  end
45
43
 
44
+ def book_path
45
+ @book_path ||= File.join((@path || `pwd`.strip), @folder_name)
46
+ end
46
47
 
47
48
  def link_host
48
49
  @link_host ||= @index_url[/\A(http:\/\/.*?)\/\w+/,1]
49
50
  end
50
51
 
51
52
  def book
52
- Dir.mkdir(@book_path) unless test(?d,@book_path)
53
- @book ||= test(?s,File.join(@book_path,'index.yml')) ? YAML.load(File.open(File.join(@book_path,'index.yml'))) : ({files: []})
53
+ return @book if @book
54
+ Dir.mkdir(book_path) unless test(?d,book_path)
55
+ @book = test(?s,File.join(book_path,'index.yml')) ? YAML.load(File.open(File.join(book_path,'index.yml'))) : {files: []}
54
56
  end
55
57
 
56
58
  def save_book
57
- File.open(File.join(@book_path,'index.yml' ),'w') do |f|
59
+ File.open(File.join(book_path,'index.yml' ),'w') do |f|
58
60
  f.write(@book.to_yaml)
59
61
  end
60
62
  end
@@ -66,7 +68,7 @@ module EpubBook
66
68
  fetch_book
67
69
  if !@cover_css && @cover
68
70
  generate_cover = <<-eof
69
- convert #{File.expand_path("../../../#{@cover}",__FILE__)} -font tsxc.ttf -gravity center -fill red -pointsize 16 -draw "text 0,0 '#{book[:title]}'" #{File.join(@book_path,@cover)}
71
+ convert #{File.expand_path("../../../#{@cover}",__FILE__)} -font tsxc.ttf -gravity center -fill red -pointsize 16 -draw "text 0,0 '#{book[:title]}'" #{File.join(book_path,@cover)}
70
72
  eof
71
73
  system(generate_cover)
72
74
  end
@@ -84,47 +86,53 @@ module EpubBook
84
86
  epub.description book[:description] if book[:description]
85
87
 
86
88
  book[:files] = book[:files][0...limit] if limit
89
+ _files = []
90
+ book[:files].collect! do |item|
91
+ _file = File.join(book_path,item[:content])
92
+ if test(?f, _file)
93
+ _files.push(_file)
94
+ item
95
+ end
96
+ end
97
+ book[:files].compact!
87
98
 
88
- epub.files book[:files].map{|item| File.join(@book_path,item[:content])}.push(File.join(@book_path,@cover))
99
+ epub.files _files.push(File.join(book_path,@cover))
89
100
  epub.nav book[:files]
90
101
 
102
+ book[:epub_file] = File.join(book_path,"#{book_name || @folder_name}.epub")
91
103
 
92
- epub_file = File.join(@book_path,"#{book_name || @folder_name}.epub")
104
+ yield self if block_given?
93
105
 
94
- epub.save(epub_file)
106
+ epub.save(book[:epub_file])
95
107
 
96
108
  #send mail
97
- puts mail_to
109
+
98
110
  if mail_to
99
111
  mailer = Mailer.new
100
112
  mailer.to = mail_to
101
- mailer.add_file epub_file
113
+ mailer.add_file book[:epub_file]
102
114
  mailer.body = "您创建的电子书[#{book[:title]}]见附件\n"
103
-
104
115
  mailer.send_mail
105
116
  end
106
117
 
107
118
  end
108
119
 
109
-
110
-
111
-
112
120
  #得到书目索引
113
121
  def fetch_index(url=nil)
122
+ book[:files] = []
114
123
  url ||= @index_url
115
- doc = Nokogiri::HTML(judge_encoding(open(URI.encode(url),"User-Agent" => @user_agent ,'Referer'=> @referer).read))
124
+ doc = Nokogiri::HTML(judge_encoding(HTTP.headers("User-Agent" => @user_agent ,'Referer'=> @referer).get(URI.encode(url)).to_s))
116
125
  #generate index.yml
117
126
 
118
127
  if !book[:title]
119
128
  doc1 = if @des_url.nil?
120
129
  doc
121
130
  else
122
- Nokogiri::HTML(judge_encoding(open(URI.encode(@des_url),"User-Agent" => @user_agent ,'Referer'=> @referer).read))
131
+ Nokogiri::HTML(judge_encoding(HTTP.headers("User-Agent" => @user_agent ,'Referer'=> @referer).get(URI.encode(generate_abs_url(doc.css(@des_url).attr("href").to_s))).to_s))
123
132
  end
124
133
  get_des(doc1)
125
134
  end
126
135
 
127
-
128
136
  doc.css(@index_item_css).each do |item|
129
137
  _href = URI.encode(item.attr(@item_attr).to_s)
130
138
  next if _href.start_with?('javascript') || _href.start_with?('#')
@@ -151,28 +159,26 @@ module EpubBook
151
159
 
152
160
  def fetch_book
153
161
  #重新得到书目,如果不存在或重新索引的话
154
- fetch_index if !test(?s,File.join(@book_path,'index.yml'))
162
+ fetch_index if !test(?s,File.join(book_path,'index.yml'))
163
+ EpubBook.logger.info "------Fetch book----------"
155
164
  book[:files].each_with_index do |item,index|
156
165
  break if limit && index >= limit
157
166
 
158
- content_path = File.join(@book_path,item[:content])
167
+ content_path = File.join(book_path,item[:content])
159
168
 
160
169
  #如果文件存在且长度不为0则获取下一个
161
170
  next if test(?s,content_path)
162
171
 
163
172
  begin
164
- doc_file = Nokogiri::HTML(judge_encoding(open(item[:url],"User-Agent" => @user_agent,'Referer'=> @referer).read))
173
+ doc_file = Nokogiri::HTML(judge_encoding(HTTP.headers("User-Agent" => @user_agent,'Referer'=> @referer).get(item[:url]).to_s))
165
174
 
166
175
  File.open(content_path,'w') do |f|
167
176
  f.write("<h3>#{item[:label]}</h3>")
168
177
  f.write(doc_file.css(@body_css).to_s.gsub(Reg,''))
169
178
  end
170
-
171
- puts item[:label]
172
-
173
179
  rescue Exception => e
174
- puts "Error:#{e.message}"
175
- puts e.backtrace.inspect
180
+ EpubBook.logger.info "Error:#{e.message},#{item.inspect}"
181
+ #EpubBook.logger.info e.backtrace
176
182
  next
177
183
  end
178
184
  end
@@ -183,8 +189,9 @@ module EpubBook
183
189
  private
184
190
  #is valid encoding
185
191
  def judge_encoding(str)
192
+ /<meta.*?charset\s*=[\s\"\']?utf-8/i =~ str ? str : str.force_encoding('gbk').encode('utf-8',invalid: :replace, undef: :replace)
186
193
  str.scrub! unless str.valid_encoding?
187
- /<meta.*?charset\s*=[\s\"\']?utf-8/i =~ str ? str : str.force_encoding('gbk').encode('utf-8')
194
+ str
188
195
  end
189
196
 
190
197
  #得到书名,介绍,及封面
@@ -193,7 +200,7 @@ module EpubBook
193
200
  if @cover_css && !book[:cover]
194
201
  cover_url = doc.css(@cover_css).attr("src").to_s
195
202
  cover_url = generate_abs_url(cover_url) #link_host + cover_url unless cover_url.start_with?("http")
196
- cover_path = File.join(@book_path,@cover)
203
+ cover_path = File.join(book_path,@cover)
197
204
  system("curl #{cover_url} -o #{cover_path} ")
198
205
  book[:cover] = cover_path
199
206
  end
@@ -0,0 +1,68 @@
1
+ module EpubBook
2
+
3
+ # Contains logging behaviour.
4
+ module Loggable
5
+
6
+ # Get the logger.
7
+ #
8
+ # @note Will try to grab Rails' logger first before creating a new logger
9
+ # with stdout.
10
+ #
11
+ # @example Get the logger.
12
+ # Loggable.logger
13
+ #
14
+ # @return [ Logger ] The logger.
15
+ #
16
+ # @since 3.0.0
17
+ def logger
18
+ return @logger if defined?(@logger)
19
+ @logger = rails_logger || default_logger
20
+ end
21
+
22
+ # Set the logger.
23
+ #
24
+ # @example Set the logger.
25
+ # Loggable.logger = Logger.new($stdout)
26
+ #
27
+ # @param [ Logger ] The logger to set.
28
+ #
29
+ # @return [ Logger ] The new logger.
30
+ #
31
+ # @since 3.0.0
32
+ def logger=(logger)
33
+ @logger = logger
34
+ end
35
+
36
+ private
37
+
38
+ # Gets the default EpubBook logger - stdout.
39
+ #
40
+ # @api private
41
+ #
42
+ # @example Get the default logger.
43
+ # Loggable.default_logger
44
+ #
45
+ # @return [ Logger ] The default logger.
46
+ #
47
+ # @since 3.0.0
48
+ def default_logger
49
+ logger = Logger.new($stdout)
50
+ logger.level = EpubBook.config.log_level
51
+ logger
52
+ end
53
+
54
+ # Get the Rails logger if it's defined.
55
+ #
56
+ # @api private
57
+ #
58
+ # @example Get Rails' logger.
59
+ # Loggable.rails_logger
60
+ #
61
+ # @return [ Logger ] The Rails logger.
62
+ #
63
+ # @since 3.0.0
64
+ def rails_logger
65
+ defined?(::Rails) && ::Rails.respond_to?(:logger) && ::Rails.logger
66
+ end
67
+ end
68
+ end
@@ -1,3 +1,3 @@
1
1
  module EpubBook
2
- VERSION = "0.1.15"
2
+ VERSION = "0.1.16"
3
3
  end
data/lib/epub_book.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require "epub_book/version"
2
2
  require 'epub_book/book'
3
+ require 'epub_book/loggable'
3
4
  require 'mail'
4
5
 
5
6
  module EpubBook
@@ -7,15 +8,18 @@ module EpubBook
7
8
  autoload :Book, "epub_book/book"
8
9
  autoload :Mailer, "epub_book/mailer"
9
10
 
10
- Config = Struct.new(:setting_file,:mail_from,:mail_subject,:mail_body,:mail_address,:mail_port,:mail_user_name,:mail_password) do
11
+ Config = Struct.new(:setting_file,:mail_from,:mail_subject,:mail_body,:mail_address,:mail_port,:mail_user_name,:mail_password,:log_level) do
11
12
  include Singleton
12
13
  def initialize
13
14
  self.mail_subject = 'epub 电子书'
14
15
  #self.mail_body = "您创建的电子书见附件\n"
15
16
  self.mail_port = 25
17
+ self.log_level = "info"
16
18
  end
17
19
  end
18
20
 
21
+ extend Loggable
22
+
19
23
  #book initialize, and the block will prior the yml setting
20
24
  def self.create_book(url,bookname=nil,des_url=nil)
21
25
 
metadata CHANGED
@@ -1,69 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epub_book
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.15
4
+ version: 0.1.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - qmliu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-06 00:00:00.000000000 Z
11
+ date: 2016-12-27 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: http
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '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'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: nokogiri
15
29
  requirement: !ruby/object:Gem::Requirement
16
30
  requirements:
17
31
  - - "~>"
18
32
  - !ruby/object:Gem::Version
19
- version: 1.6.8
33
+ version: '0'
20
34
  type: :runtime
21
35
  prerelease: false
22
36
  version_requirements: !ruby/object:Gem::Requirement
23
37
  requirements:
24
38
  - - "~>"
25
39
  - !ruby/object:Gem::Version
26
- version: 1.6.8
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: eeepub
29
43
  requirement: !ruby/object:Gem::Requirement
30
44
  requirements:
31
- - - ">="
45
+ - - "~>"
32
46
  - !ruby/object:Gem::Version
33
47
  version: '0'
34
48
  type: :runtime
35
49
  prerelease: false
36
50
  version_requirements: !ruby/object:Gem::Requirement
37
51
  requirements:
38
- - - ">="
52
+ - - "~>"
39
53
  - !ruby/object:Gem::Version
40
54
  version: '0'
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: zip-zip
43
57
  requirement: !ruby/object:Gem::Requirement
44
58
  requirements:
45
- - - ">="
59
+ - - "~>"
46
60
  - !ruby/object:Gem::Version
47
61
  version: '0'
48
62
  type: :runtime
49
63
  prerelease: false
50
64
  version_requirements: !ruby/object:Gem::Requirement
51
65
  requirements:
52
- - - ">="
66
+ - - "~>"
53
67
  - !ruby/object:Gem::Version
54
68
  version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: mail
57
71
  requirement: !ruby/object:Gem::Requirement
58
72
  requirements:
59
- - - ">="
73
+ - - "~>"
60
74
  - !ruby/object:Gem::Version
61
75
  version: '0'
62
76
  type: :runtime
63
77
  prerelease: false
64
78
  version_requirements: !ruby/object:Gem::Requirement
65
79
  requirements:
66
- - - ">="
80
+ - - "~>"
67
81
  - !ruby/object:Gem::Version
68
82
  version: '0'
69
83
  - !ruby/object:Gem::Dependency
@@ -112,28 +126,28 @@ dependencies:
112
126
  name: pry
113
127
  requirement: !ruby/object:Gem::Requirement
114
128
  requirements:
115
- - - ">="
129
+ - - "~>"
116
130
  - !ruby/object:Gem::Version
117
131
  version: '0'
118
132
  type: :development
119
133
  prerelease: false
120
134
  version_requirements: !ruby/object:Gem::Requirement
121
135
  requirements:
122
- - - ">="
136
+ - - "~>"
123
137
  - !ruby/object:Gem::Version
124
138
  version: '0'
125
139
  - !ruby/object:Gem::Dependency
126
140
  name: pry-doc
127
141
  requirement: !ruby/object:Gem::Requirement
128
142
  requirements:
129
- - - ">="
143
+ - - "~>"
130
144
  - !ruby/object:Gem::Version
131
145
  version: '0'
132
146
  type: :development
133
147
  prerelease: false
134
148
  version_requirements: !ruby/object:Gem::Requirement
135
149
  requirements:
136
- - - ">="
150
+ - - "~>"
137
151
  - !ruby/object:Gem::Version
138
152
  version: '0'
139
153
  description: 'create epub from a book index url and mail to you. by setting a default_setting.yml
@@ -160,6 +174,7 @@ files:
160
174
  - exe/create_book
161
175
  - lib/epub_book.rb
162
176
  - lib/epub_book/book.rb
177
+ - lib/epub_book/loggable.rb
163
178
  - lib/epub_book/mailer.rb
164
179
  - lib/epub_book/version.rb
165
180
  - tsxc.ttf