epub_book 0.1.17 → 0.1.18
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +4 -0
- data/epub_book.gemspec +2 -1
- data/lib/epub_book.rb +1 -1
- data/lib/epub_book/book.rb +74 -43
- data/lib/epub_book/loggable.rb +2 -1
- data/lib/epub_book/version.rb +1 -1
- metadata +24 -13
- data/bin/console +0 -14
- data/bin/setup +0 -8
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: df55d3587ef2f535b307f63117af6dafa73597a5007ad641e25ce88b17ef78b9
|
4
|
+
data.tar.gz: 0f78bbb8a4dee88540915e1cb95f28cd8abf872b07a8adeb74473ef171162bb5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
@@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
|
|
28
28
|
spec.require_paths = ["lib"]
|
29
29
|
|
30
30
|
spec.add_dependency 'http','~> 2.1'
|
31
|
-
spec.add_dependency 'nokogiri','
|
31
|
+
spec.add_dependency 'nokogiri','1.6.8.1'
|
32
32
|
spec.add_dependency 'eeepub'
|
33
33
|
spec.add_dependency 'zip-zip'
|
34
34
|
spec.add_dependency 'mail'
|
@@ -38,5 +38,6 @@ Gem::Specification.new do |spec|
|
|
38
38
|
spec.add_development_dependency "rake", "~> 10.0"
|
39
39
|
spec.add_development_dependency "rspec", "~> 3.0"
|
40
40
|
spec.add_development_dependency "pry"
|
41
|
+
spec.add_development_dependency "pry-byebug"
|
41
42
|
spec.add_development_dependency "pry-doc"
|
42
43
|
end
|
data/lib/epub_book.rb
CHANGED
@@ -8,7 +8,7 @@ module EpubBook
|
|
8
8
|
autoload :Book, "epub_book/book"
|
9
9
|
autoload :Mailer, "epub_book/mailer"
|
10
10
|
|
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
|
+
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
|
12
12
|
include Singleton
|
13
13
|
def initialize
|
14
14
|
self.mail_subject = 'epub 电子书'
|
data/lib/epub_book/book.rb
CHANGED
@@ -16,12 +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,:cover_css, :description_css,:path,:user_agent,:referer,:creator,:mail_to, :folder_name,:des_url
|
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
|
25
26
|
|
26
27
|
|
27
28
|
Reg = /<script.*?>.*?<\/script>/m
|
@@ -38,6 +39,7 @@ module EpubBook
|
|
38
39
|
@cover = 'cover.jpg'
|
39
40
|
@body_css = '.articlebody'
|
40
41
|
@item_attr = "href"
|
42
|
+
@ext_name = 'epub'
|
41
43
|
yield self if block_given?
|
42
44
|
end
|
43
45
|
|
@@ -46,7 +48,7 @@ module EpubBook
|
|
46
48
|
end
|
47
49
|
|
48
50
|
def link_host
|
49
|
-
@link_host ||= @index_url[/\A(
|
51
|
+
@link_host ||= @index_url[/\A(https?:\/\/.*?)\/\w+/,1]
|
50
52
|
end
|
51
53
|
|
52
54
|
def book
|
@@ -55,6 +57,7 @@ module EpubBook
|
|
55
57
|
@book = test(?s,File.join(book_path,'index.yml')) ? YAML.load(File.open(File.join(book_path,'index.yml'))) : {files: []}
|
56
58
|
end
|
57
59
|
|
60
|
+
#save catalog file
|
58
61
|
def save_book
|
59
62
|
File.open(File.join(book_path,'index.yml' ),'w') do |f|
|
60
63
|
f.write(@book.to_yaml)
|
@@ -65,52 +68,54 @@ module EpubBook
|
|
65
68
|
#创建书本
|
66
69
|
def generate_book(book_name=nil)
|
67
70
|
#获取epub源数据
|
71
|
+
fetch_index if !test(?s,File.join(book_path,'index.yml'))
|
72
|
+
|
73
|
+
book[:file_abs_name] = File.join(book_path,"#{book[:title]}.#{ext_name}")
|
74
|
+
|
68
75
|
fetch_book
|
69
|
-
if
|
70
|
-
|
76
|
+
if ext_name == 'epub'
|
77
|
+
if !@cover_css && @cover
|
78
|
+
generate_cover = <<-eof
|
71
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)}
|
72
|
-
|
73
|
-
|
74
|
-
end
|
75
|
-
|
76
|
-
epub = EeePub.make
|
77
|
-
|
78
|
-
epub.title book[:title]
|
79
|
-
epub.creator @creator
|
80
|
-
epub.publisher @creator
|
81
|
-
epub.date Time.now
|
82
|
-
epub.identifier "http://javy_liu.com/book/#{@folder_name}", :scheme => 'URL'
|
83
|
-
epub.uid "http://javy_liu.com/book/#{@folder_name}"
|
84
|
-
epub.cover @cover
|
85
|
-
epub.subject book[:title]
|
86
|
-
epub.description book[:description] if book[:description]
|
87
|
-
|
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
|
80
|
+
eof
|
81
|
+
system(generate_cover)
|
95
82
|
end
|
96
|
-
end
|
97
|
-
book[:files].compact!
|
98
|
-
|
99
|
-
epub.files _files.push(File.join(book_path,@cover))
|
100
|
-
epub.nav book[:files]
|
101
83
|
|
102
|
-
|
103
|
-
|
104
|
-
|
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!
|
105
106
|
|
106
|
-
|
107
|
+
epub.files _files.push(File.join(book_path,@cover))
|
108
|
+
epub.nav book[:files]
|
109
|
+
yield self if block_given?
|
107
110
|
|
111
|
+
epub.save(book[:file_abs_name])
|
112
|
+
end
|
108
113
|
#send mail
|
109
114
|
|
110
115
|
if mail_to
|
111
116
|
mailer = Mailer.new
|
112
117
|
mailer.to = mail_to
|
113
|
-
mailer.add_file book[:
|
118
|
+
mailer.add_file book[:file_abs_name]
|
114
119
|
mailer.body = "您创建的电子书[#{book[:title]}]见附件\n"
|
115
120
|
mailer.send_mail
|
116
121
|
end
|
@@ -123,6 +128,7 @@ module EpubBook
|
|
123
128
|
url ||= @index_url
|
124
129
|
doc = Nokogiri::HTML(judge_encoding(HTTP.headers("User-Agent" => @user_agent ,'Referer'=> @referer).get(URI.encode(url)).to_s))
|
125
130
|
#generate index.yml
|
131
|
+
EpubBook.logger.info "------Fetch index--#{url}---------------"
|
126
132
|
|
127
133
|
if !book[:title]
|
128
134
|
doc1 = if @des_url.nil?
|
@@ -133,6 +139,7 @@ module EpubBook
|
|
133
139
|
get_des(doc1)
|
134
140
|
end
|
135
141
|
|
142
|
+
#binding.pry
|
136
143
|
doc.css(@index_item_css).each do |item|
|
137
144
|
_href = URI.encode(item.attr(@item_attr).to_s)
|
138
145
|
next if _href.start_with?('javascript') || _href.start_with?('#')
|
@@ -159,22 +166,36 @@ module EpubBook
|
|
159
166
|
|
160
167
|
def fetch_book
|
161
168
|
#重新得到书目,如果不存在或重新索引的话
|
162
|
-
fetch_index if !test(?s,File.join(book_path,'index.yml'))
|
169
|
+
#fetch_index if !test(?s,File.join(book_path,'index.yml'))
|
163
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
|
164
175
|
book[:files].each_with_index do |item,index|
|
165
176
|
break if limit && index >= limit
|
166
177
|
|
167
178
|
content_path = File.join(book_path,item[:content])
|
168
179
|
|
169
180
|
#如果文件存在且长度不为0则获取下一个
|
181
|
+
#binding.pry
|
170
182
|
next if test(?s,content_path)
|
171
183
|
|
172
184
|
begin
|
173
185
|
doc_file = Nokogiri::HTML(judge_encoding(HTTP.headers("User-Agent" => @user_agent,'Referer'=> @referer).get(item[:url]).to_s))
|
174
186
|
|
175
|
-
|
176
|
-
|
177
|
-
|
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)
|
178
199
|
end
|
179
200
|
rescue Exception => e
|
180
201
|
EpubBook.logger.info "Error:#{e.message},#{item.inspect}"
|
@@ -183,21 +204,31 @@ module EpubBook
|
|
183
204
|
end
|
184
205
|
end
|
185
206
|
|
207
|
+
txt_file.close if ext_name == 'txt'
|
208
|
+
|
186
209
|
end
|
187
210
|
|
188
211
|
|
189
212
|
private
|
190
213
|
#is valid encoding
|
191
214
|
def judge_encoding(str)
|
192
|
-
|
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)
|
193
217
|
str.scrub! unless str.valid_encoding?
|
218
|
+
|
219
|
+
EpubBook.logger.info "-------encode 后 #{str.encoding}"
|
194
220
|
str
|
195
221
|
end
|
196
222
|
|
197
223
|
#得到书名,介绍,及封面
|
198
224
|
def get_des(doc)
|
199
225
|
book[:title] = doc.css(@title_css).text.strip
|
200
|
-
|
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'
|
201
232
|
cover_url = doc.css(@cover_css).attr("src").to_s
|
202
233
|
cover_url = generate_abs_url(cover_url) #link_host + cover_url unless cover_url.start_with?("http")
|
203
234
|
cover_path = File.join(book_path,@cover)
|
data/lib/epub_book/loggable.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'logger'
|
1
2
|
module EpubBook
|
2
3
|
|
3
4
|
# Contains logging behaviour.
|
@@ -46,7 +47,7 @@ module EpubBook
|
|
46
47
|
#
|
47
48
|
# @since 3.0.0
|
48
49
|
def default_logger
|
49
|
-
logger = Logger.new($stdout)
|
50
|
+
logger = ::Logger.new($stdout)
|
50
51
|
logger.level = EpubBook.config.log_level
|
51
52
|
logger
|
52
53
|
end
|
data/lib/epub_book/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: epub_book
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
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:
|
11
|
+
date: 2021-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: http
|
@@ -28,16 +28,16 @@ dependencies:
|
|
28
28
|
name: nokogiri
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- -
|
31
|
+
- - '='
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 1.6.8.1
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- -
|
38
|
+
- - '='
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
40
|
+
version: 1.6.8.1
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: eeepub
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -136,6 +136,20 @@ dependencies:
|
|
136
136
|
- - ">="
|
137
137
|
- !ruby/object:Gem::Version
|
138
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'
|
139
153
|
- !ruby/object:Gem::Dependency
|
140
154
|
name: pry-doc
|
141
155
|
requirement: !ruby/object:Gem::Requirement
|
@@ -167,8 +181,6 @@ files:
|
|
167
181
|
- LICENSE.txt
|
168
182
|
- README.md
|
169
183
|
- Rakefile
|
170
|
-
- bin/console
|
171
|
-
- bin/setup
|
172
184
|
- cover.jpg
|
173
185
|
- epub_book.gemspec
|
174
186
|
- exe/create_book
|
@@ -183,7 +195,7 @@ licenses:
|
|
183
195
|
- MIT
|
184
196
|
metadata:
|
185
197
|
allowed_push_host: https://rubygems.org
|
186
|
-
post_install_message:
|
198
|
+
post_install_message:
|
187
199
|
rdoc_options: []
|
188
200
|
require_paths:
|
189
201
|
- lib
|
@@ -198,9 +210,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
198
210
|
- !ruby/object:Gem::Version
|
199
211
|
version: '0'
|
200
212
|
requirements: []
|
201
|
-
|
202
|
-
|
203
|
-
signing_key:
|
213
|
+
rubygems_version: 3.2.3
|
214
|
+
signing_key:
|
204
215
|
specification_version: 4
|
205
216
|
summary: create epub from a book index url and mail to you.
|
206
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
|