epub_book 0.1.13 → 0.1.18

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
- SHA1:
3
- metadata.gz: 242f6d9ee15d028f0c72cd604790292af9c9b2d9
4
- data.tar.gz: 9856509153f5caafd9f2aae760dc4f8013e302a6
2
+ SHA256:
3
+ metadata.gz: df55d3587ef2f535b307f63117af6dafa73597a5007ad641e25ce88b17ef78b9
4
+ data.tar.gz: 0f78bbb8a4dee88540915e1cb95f28cd8abf872b07a8adeb74473ef171162bb5
5
5
  SHA512:
6
- metadata.gz: 088faed36f70d1b635ae0e4eff49648d0f96b985388af0b5fdb319495c62b37662a559a9dc9a06e94564f6cd18173bbbb1238657be56d36f1fffadd3afa3ee27
7
- data.tar.gz: 516aee02a5ffa3f7b6fee89ff55fd2399ae7564b69510d7ea1d4c70693906784bcc5df88a012be9223d508cc17033f6cb535f59999ddf76f514bb914953c1e44
6
+ metadata.gz: 2337421c1b0a1b0ce4d7096f6c899091044c5a69dc9297f95ec22b212f6d0f76677deb2edf53be7a7aa7d89df633ce56cea621b39d2692579a29c0c7060ffced
7
+ data.tar.gz: 23b4b10d567941e7b8dbf7a8662e370fa5b2d73ec0d6f979ca2e398007cee4b44743cac691395d6583b0aad1567e3ba7acf6cbebba7de36e50281c5450259124
data/README.md CHANGED
@@ -29,6 +29,7 @@ Or install it yourself as:
29
29
  book.body_css = '.wrapper #content'
30
30
  book.creator = 'javy_liu'
31
31
  book.path = '/tmp'
32
+ book.ext_name = 'txt'
32
33
  end
33
34
  ```
34
35
 
@@ -44,6 +45,7 @@ Or install it yourself as:
44
45
  config.mail_user_name = 'ex@example.com'
45
46
  config.mail_password = 'password'
46
47
  config.mail_from = 'yourmail@example.com'
48
+ config.ext_name = 'epub'
47
49
  end
48
50
  ```
49
51
  Or use a ./default_setting.yml file have following content
@@ -69,6 +71,7 @@ Or use a ./default_setting.yml file have following content
69
71
  creator: 'user name'
70
72
  path: '/'
71
73
  mail_to: 'yourmail@example.com'
74
+ ext_name: 'txt'
72
75
 
73
76
  #special host book setting(the key is book_url's host which replacing the dot with underline)
74
77
  #http://www.piaotian.net/html/0/431/index.html
@@ -111,6 +114,7 @@ if you have setting file , you can create book like following
111
114
  book.creator #epub creator
112
115
  book.path #epub book save path
113
116
  book.mail_to #if your want send by email when epub created, set this to your email
117
+ book.ext_name #can create epub or txt file
114
118
  ```
115
119
  ## Perform `create_book` in your terminal
116
120
  ```bash
data/epub_book.gemspec CHANGED
@@ -27,7 +27,8 @@ 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'
30
+ spec.add_dependency 'http','~> 2.1'
31
+ spec.add_dependency 'nokogiri','1.6.8.1'
31
32
  spec.add_dependency 'eeepub'
32
33
  spec.add_dependency 'zip-zip'
33
34
  spec.add_dependency 'mail'
@@ -37,5 +38,6 @@ Gem::Specification.new do |spec|
37
38
  spec.add_development_dependency "rake", "~> 10.0"
38
39
  spec.add_development_dependency "rspec", "~> 3.0"
39
40
  spec.add_development_dependency "pry"
41
+ spec.add_development_dependency "pry-byebug"
40
42
  spec.add_development_dependency "pry-doc"
41
43
  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, :ext_name) 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
 
@@ -1,4 +1,4 @@
1
- require 'open-uri'
1
+ require 'http'
2
2
  require 'nokogiri'
3
3
  require 'eeepub'
4
4
  require 'base64'
@@ -16,13 +16,13 @@ require 'yaml'
16
16
  #user_agent 访问代理
17
17
  #referer 访问原地址
18
18
  #creator 责任人
19
+ #ext_name 扩展名 epub,txt
19
20
 
20
21
  module EpubBook
21
22
  class Book
22
23
  UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.116 Safari/537.36"
23
24
  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
25
+ 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,:ext_name
26
26
 
27
27
 
28
28
  Reg = /<script.*?>.*?<\/script>/m
@@ -39,22 +39,27 @@ module EpubBook
39
39
  @cover = 'cover.jpg'
40
40
  @body_css = '.articlebody'
41
41
  @item_attr = "href"
42
+ @ext_name = 'epub'
42
43
  yield self if block_given?
43
- @book_path = File.join((@path || `pwd`.strip), @folder_name)
44
44
  end
45
45
 
46
+ def book_path
47
+ @book_path ||= File.join((@path || `pwd`.strip), @folder_name)
48
+ end
46
49
 
47
50
  def link_host
48
- @link_host ||= @index_url[/\A(http:\/\/.*?)\/\w+/,1]
51
+ @link_host ||= @index_url[/\A(https?:\/\/.*?)\/\w+/,1]
49
52
  end
50
53
 
51
54
  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: []})
55
+ return @book if @book
56
+ Dir.mkdir(book_path) unless test(?d,book_path)
57
+ @book = test(?s,File.join(book_path,'index.yml')) ? YAML.load(File.open(File.join(book_path,'index.yml'))) : {files: []}
54
58
  end
55
59
 
60
+ #save catalog file
56
61
  def save_book
57
- File.open(File.join(@book_path,'index.yml' ),'w') do |f|
62
+ File.open(File.join(book_path,'index.yml' ),'w') do |f|
58
63
  f.write(@book.to_yaml)
59
64
  end
60
65
  end
@@ -63,68 +68,78 @@ module EpubBook
63
68
  #创建书本
64
69
  def generate_book(book_name=nil)
65
70
  #获取epub源数据
66
- fetch_book
67
- if !@cover_css && @cover
68
- 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)}
70
- eof
71
- system(generate_cover)
72
- end
73
-
74
- epub = EeePub.make
71
+ fetch_index if !test(?s,File.join(book_path,'index.yml'))
75
72
 
76
- epub.title book[:title]
77
- epub.creator @creator
78
- epub.publisher @creator
79
- epub.date Time.now
80
- epub.identifier "http://javy_liu.com/book/#{@folder_name}", :scheme => 'URL'
81
- epub.uid "http://javy_liu.com/book/#{@folder_name}"
82
- epub.cover @cover
83
- epub.subject book[:title]
84
- epub.description book[:description] if book[:description]
85
-
86
- book[:files] = book[:files][0...limit] if limit
87
-
88
- epub.files book[:files].map{|item| File.join(@book_path,item[:content])}.push(File.join(@book_path,@cover))
89
- epub.nav book[:files]
73
+ book[:file_abs_name] = File.join(book_path,"#{book[:title]}.#{ext_name}")
90
74
 
75
+ fetch_book
76
+ if ext_name == 'epub'
77
+ if !@cover_css && @cover
78
+ generate_cover = <<-eof
79
+ 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)}
80
+ eof
81
+ system(generate_cover)
82
+ end
91
83
 
92
- epub_file = File.join(@book_path,"#{book_name || @folder_name}.epub")
84
+ epub = EeePub.make
85
+
86
+ epub.title book[:title]
87
+ epub.creator @creator
88
+ epub.publisher @creator
89
+ epub.date Time.now
90
+ epub.identifier "http://javy_liu.com/book/#{@folder_name}", :scheme => 'URL'
91
+ epub.uid "http://javy_liu.com/book/#{@folder_name}"
92
+ epub.cover @cover
93
+ epub.subject book[:title]
94
+ epub.description book[:description] if book[:description]
95
+
96
+ book[:files] = book[:files][0...limit] if limit
97
+ _files = []
98
+ book[:files].collect! do |item|
99
+ _file = File.join(book_path,item[:content])
100
+ if test(?f, _file)
101
+ _files.push(_file)
102
+ item
103
+ end
104
+ end
105
+ book[:files].compact!
93
106
 
94
- epub.save(epub_file)
107
+ epub.files _files.push(File.join(book_path,@cover))
108
+ epub.nav book[:files]
109
+ yield self if block_given?
95
110
 
111
+ epub.save(book[:file_abs_name])
112
+ end
96
113
  #send mail
97
- puts mail_to
114
+
98
115
  if mail_to
99
116
  mailer = Mailer.new
100
117
  mailer.to = mail_to
101
- mailer.add_file epub_file
118
+ mailer.add_file book[:file_abs_name]
102
119
  mailer.body = "您创建的电子书[#{book[:title]}]见附件\n"
103
-
104
120
  mailer.send_mail
105
121
  end
106
122
 
107
123
  end
108
124
 
109
-
110
-
111
-
112
125
  #得到书目索引
113
126
  def fetch_index(url=nil)
127
+ book[:files] = []
114
128
  url ||= @index_url
115
- doc = Nokogiri::HTML(judge_encoding(open(URI.encode(url),"User-Agent" => @user_agent ,'Referer'=> @referer).read))
129
+ doc = Nokogiri::HTML(judge_encoding(HTTP.headers("User-Agent" => @user_agent ,'Referer'=> @referer).get(URI.encode(url)).to_s))
116
130
  #generate index.yml
131
+ EpubBook.logger.info "------Fetch index--#{url}---------------"
117
132
 
118
133
  if !book[:title]
119
134
  doc1 = if @des_url.nil?
120
135
  doc
121
136
  else
122
- Nokogiri::HTML(judge_encoding(open(URI.encode(@des_url),"User-Agent" => @user_agent ,'Referer'=> @referer).read))
137
+ 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
138
  end
124
139
  get_des(doc1)
125
140
  end
126
141
 
127
-
142
+ #binding.pry
128
143
  doc.css(@index_item_css).each do |item|
129
144
  _href = URI.encode(item.attr(@item_attr).to_s)
130
145
  next if _href.start_with?('javascript') || _href.start_with?('#')
@@ -151,49 +166,72 @@ module EpubBook
151
166
 
152
167
  def fetch_book
153
168
  #重新得到书目,如果不存在或重新索引的话
154
- fetch_index if !test(?s,File.join(@book_path,'index.yml'))
169
+ #fetch_index if !test(?s,File.join(book_path,'index.yml'))
170
+ EpubBook.logger.info "------Fetch book----------"
171
+ #open a txt file to write
172
+ if ext_name == 'txt'
173
+ txt_file = File.open(book[:file_abs_name], 'a')
174
+ end
155
175
  book[:files].each_with_index do |item,index|
156
176
  break if limit && index >= limit
157
177
 
158
- content_path = File.join(@book_path,item[:content])
178
+ content_path = File.join(book_path,item[:content])
159
179
 
160
180
  #如果文件存在且长度不为0则获取下一个
181
+ #binding.pry
161
182
  next if test(?s,content_path)
162
183
 
163
184
  begin
164
- doc_file = Nokogiri::HTML(judge_encoding(open(item[:url],"User-Agent" => @user_agent,'Referer'=> @referer).read))
165
-
166
- File.open(content_path,'w') do |f|
167
- f.write("<h3>#{item[:label]}</h3>")
168
- f.write(doc_file.css(@body_css).to_s.gsub(Reg,''))
185
+ doc_file = Nokogiri::HTML(judge_encoding(HTTP.headers("User-Agent" => @user_agent,'Referer'=> @referer).get(item[:url]).to_s))
186
+
187
+ EpubBook.logger.info item[:label]
188
+ #binding.pry
189
+ if ext_name == 'pub'
190
+ File.open(content_path,'w') do |f|
191
+ f.write("<h3>#{item[:label]}</h3>")
192
+ f.write(doc_file.css(@body_css).to_s.gsub(Reg,''))
193
+ end
194
+ else
195
+ txt_file.write("\n\n")
196
+ txt_file.write(item[:label])
197
+ txt_file.write("\n ")
198
+ txt_file.write(doc_file.css(@body_css).text)
169
199
  end
170
-
171
- puts item[:label]
172
-
173
200
  rescue Exception => e
174
- puts "Error:#{e.message}"
175
- puts e.backtrace.inspect
201
+ EpubBook.logger.info "Error:#{e.message},#{item.inspect}"
202
+ #EpubBook.logger.info e.backtrace
176
203
  next
177
204
  end
178
205
  end
179
206
 
207
+ txt_file.close if ext_name == 'txt'
208
+
180
209
  end
181
210
 
182
211
 
183
212
  private
184
213
  #is valid encoding
185
214
  def judge_encoding(str)
215
+ EpubBook.logger.info str.encoding
216
+ /<meta.*?charset\s*=[\s\"\']?utf-8/i =~ str ? str : str.force_encoding('gbk').encode!('utf-8',invalid: :replace, undef: :replace)
186
217
  str.scrub! unless str.valid_encoding?
187
- /<meta.*?charset\s*=[\s\"\']?utf-8/i =~ str ? str : str.force_encoding('gbk').encode('utf-8')
218
+
219
+ EpubBook.logger.info "-------encode 后 #{str.encoding}"
220
+ str
188
221
  end
189
222
 
190
223
  #得到书名,介绍,及封面
191
224
  def get_des(doc)
192
225
  book[:title] = doc.css(@title_css).text.strip
193
- if @cover_css && !book[:cover]
226
+
227
+ #EpubBook.logger.info doc
228
+ #EpubBook.logger.info @title_css
229
+
230
+ #binding.pry
231
+ if @cover_css && !book[:cover] && ext_name == 'epub'
194
232
  cover_url = doc.css(@cover_css).attr("src").to_s
195
233
  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)
234
+ cover_path = File.join(book_path,@cover)
197
235
  system("curl #{cover_url} -o #{cover_path} ")
198
236
  book[:cover] = cover_path
199
237
  end
@@ -204,13 +242,13 @@ module EpubBook
204
242
  end
205
243
 
206
244
  def generate_abs_url(url)
207
- if _href.start_with?("http")
245
+ if url.start_with?("http")
208
246
  url
209
- elsif _href.start_with?("/")
210
- "#{link_host}#{_href}"
247
+ elsif url.start_with?("/")
248
+ "#{link_host}#{url}"
211
249
  else
212
250
  @path_name ||= @index_url[/.*\//]
213
- "#{@path_name}#{_href}"
251
+ "#{@path_name}#{url}"
214
252
  end
215
253
 
216
254
  end
@@ -0,0 +1,69 @@
1
+ require 'logger'
2
+ module EpubBook
3
+
4
+ # Contains logging behaviour.
5
+ module Loggable
6
+
7
+ # Get the logger.
8
+ #
9
+ # @note Will try to grab Rails' logger first before creating a new logger
10
+ # with stdout.
11
+ #
12
+ # @example Get the logger.
13
+ # Loggable.logger
14
+ #
15
+ # @return [ Logger ] The logger.
16
+ #
17
+ # @since 3.0.0
18
+ def logger
19
+ return @logger if defined?(@logger)
20
+ @logger = rails_logger || default_logger
21
+ end
22
+
23
+ # Set the logger.
24
+ #
25
+ # @example Set the logger.
26
+ # Loggable.logger = Logger.new($stdout)
27
+ #
28
+ # @param [ Logger ] The logger to set.
29
+ #
30
+ # @return [ Logger ] The new logger.
31
+ #
32
+ # @since 3.0.0
33
+ def logger=(logger)
34
+ @logger = logger
35
+ end
36
+
37
+ private
38
+
39
+ # Gets the default EpubBook logger - stdout.
40
+ #
41
+ # @api private
42
+ #
43
+ # @example Get the default logger.
44
+ # Loggable.default_logger
45
+ #
46
+ # @return [ Logger ] The default logger.
47
+ #
48
+ # @since 3.0.0
49
+ def default_logger
50
+ logger = ::Logger.new($stdout)
51
+ logger.level = EpubBook.config.log_level
52
+ logger
53
+ end
54
+
55
+ # Get the Rails logger if it's defined.
56
+ #
57
+ # @api private
58
+ #
59
+ # @example Get Rails' logger.
60
+ # Loggable.rails_logger
61
+ #
62
+ # @return [ Logger ] The Rails logger.
63
+ #
64
+ # @since 3.0.0
65
+ def rails_logger
66
+ defined?(::Rails) && ::Rails.respond_to?(:logger) && ::Rails.logger
67
+ end
68
+ end
69
+ end
@@ -1,3 +1,3 @@
1
1
  module EpubBook
2
- VERSION = "0.1.13"
2
+ VERSION = "0.1.18"
3
3
  end
metadata CHANGED
@@ -1,29 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: epub_book
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.13
4
+ version: 0.1.18
5
5
  platform: ruby
6
6
  authors:
7
7
  - qmliu
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-09-06 00:00:00.000000000 Z
11
+ date: 2021-06-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: nokogiri
14
+ name: http
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 1.6.8
19
+ version: '2.1'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 1.6.8
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: nokogiri
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '='
32
+ - !ruby/object:Gem::Version
33
+ version: 1.6.8.1
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '='
39
+ - !ruby/object:Gem::Version
40
+ version: 1.6.8.1
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: eeepub
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -122,6 +136,20 @@ dependencies:
122
136
  - - ">="
123
137
  - !ruby/object:Gem::Version
124
138
  version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: pry-byebug
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - ">="
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
125
153
  - !ruby/object:Gem::Dependency
126
154
  name: pry-doc
127
155
  requirement: !ruby/object:Gem::Requirement
@@ -153,13 +181,12 @@ files:
153
181
  - LICENSE.txt
154
182
  - README.md
155
183
  - Rakefile
156
- - bin/console
157
- - bin/setup
158
184
  - cover.jpg
159
185
  - epub_book.gemspec
160
186
  - exe/create_book
161
187
  - lib/epub_book.rb
162
188
  - lib/epub_book/book.rb
189
+ - lib/epub_book/loggable.rb
163
190
  - lib/epub_book/mailer.rb
164
191
  - lib/epub_book/version.rb
165
192
  - tsxc.ttf
@@ -168,7 +195,7 @@ licenses:
168
195
  - MIT
169
196
  metadata:
170
197
  allowed_push_host: https://rubygems.org
171
- post_install_message:
198
+ post_install_message:
172
199
  rdoc_options: []
173
200
  require_paths:
174
201
  - lib
@@ -183,9 +210,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
183
210
  - !ruby/object:Gem::Version
184
211
  version: '0'
185
212
  requirements: []
186
- rubyforge_project:
187
- rubygems_version: 2.4.8
188
- signing_key:
213
+ rubygems_version: 3.2.3
214
+ signing_key:
189
215
  specification_version: 4
190
216
  summary: create epub from a book index url and mail to you.
191
217
  test_files: []
data/bin/console DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "epub_book"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- require "pry"
11
- Pry.start
12
-
13
- #require "irb"
14
- #IRB.start
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here