epub_book 0.1.2 → 0.1.3

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: 988d7b70bfcc5a8c9f127f3571881dc513556370
4
- data.tar.gz: 8e468e991b0fb86bc591713739f01037b78f79b2
3
+ metadata.gz: 6576163ae0d036a05da85d98b83375314ca8de30
4
+ data.tar.gz: fbf0608c5fd2db1cb122386b9f76be697a98f4d9
5
5
  SHA512:
6
- metadata.gz: 91501148b27c75734dab39ca1a4c336a22a17b7a08c2553841b247b7545e6f524aa27a356fee722368369ad7ef7f86f96012f00ea62d6d36b192ec44f80c7d8b
7
- data.tar.gz: dbe5c7559a2134bf4be084a319eb444dd0cdf70ed203b04782a5d8a64c9e616c5b4af404da64220e2d617ade474af2ac12696c902e9b467a96eb68374147b9b3
6
+ metadata.gz: e3d76543d588dfdc76a48b38133127081338ea0adba5fed4c1d630c167da624bb155bf87ac26da9befb598e5c6289eb55d858406f7fb0e3e2e6b6b501b1fd735
7
+ data.tar.gz: 995f91d7e0138583c4012920c69d6c1e5c3912ab91ba7f6a238410cb4663944ab9c6849485a547f13858cfd8b7a1f97ed9a9dde20ee0d426f774ee5b5def1801
data/README.md CHANGED
@@ -19,11 +19,27 @@ Or install it yourself as:
19
19
  $ gem install epub_book
20
20
 
21
21
  ## Usage
22
+ ### create book
23
+ ```ruby
24
+ EpubBook.create_book(book_url,bookname, des_url) do |book|
25
+ book.cover_css = '.pic_txt_list .pic img'
26
+ book.description_css = '.box p.description'
27
+ book.title_css = '.pic_txt_list h3 span'
28
+ book.index_item_css = 'ul.list li.c3 a'
29
+ book.body_css = '.wrapper #content'
30
+ book.creator = 'javy_liu'
31
+ book.path = '/tmp'
32
+ end
33
+ ```
34
+
22
35
 
23
- Setting
36
+ ###use Setting
24
37
  ```ruby
25
- #smtp setting
38
+ #default setting, this setting is prior the default_setting.yml
26
39
  EpubBook.configure do |config|
40
+ #if not set,it will use `pwd`/default_setting.yml
41
+ config.setting_file = "./default_setting.yml"
42
+ #you can set following setting in default_setting.yml
27
43
  config.mail_address = 'smtp.example.com'
28
44
  config.mail_user_name = 'ex@example.com'
29
45
  config.mail_password = 'password'
@@ -42,6 +58,7 @@ Or use a ./default_setting.yml file have following content
42
58
  mail_user_name: smpt_mail@example.com
43
59
  mail_password: smpt_pwd
44
60
 
61
+ #default book setting
45
62
  book:
46
63
  limit: 10
47
64
  cover_css: '.pic_txt_list .pic img'
@@ -52,33 +69,33 @@ Or use a ./default_setting.yml file have following content
52
69
  creator: 'user name'
53
70
  path: '/'
54
71
  mail_to: 'yourmail@example.com'
55
- book_url: http://www.quanben5.com/n/bubushenglian/xiaoshuo.html
56
- bookname: bbsl
72
+
73
+ #special host book setting(the key is book_url's host which replacing the dot with underline)
74
+ www_piaotian_net:
75
+ cover_css: '.pic_txt_list .pic img'
76
+ description_css: '.box p.description'
77
+ title_css: '.pic_txt_list h3 span'
78
+ index_item_css: 'ul.list li.c3 a'
79
+ body_css: '.wrapper #content'
80
+ path: '/'
57
81
  ```
58
- Create book
82
+
83
+ if you have setting file , you can create book like following
59
84
  ```ruby
60
- EpubBook.create_book(book_url,bookname) do |book|
61
- book.cover_css = '.pic_txt_list .pic img'
62
- book.description_css = '.box p.description'
63
- book.title_css = '.pic_txt_list h3 span'
64
- book.index_item_css = 'ul.list li.c3 a'
65
- book.body_css = '.wrapper #content'
66
- book.creator = 'javy_liu'
67
- book.path = '/home/oswap/ruby_test/epub_book/'
68
- book.mail_to = ''
69
- end
85
+ EpubBook.create_book(book_url,bookname, des_url)
70
86
  ```
71
87
 
72
88
  ## Parameter specification
73
89
  ```ruby
74
90
  book_url(required): internal book index page url (this page may include the description or cover)
75
91
  bookname(optional): created book file name, if not set ,it will use the Base64.url_encode(book_url)[-10,-2]
76
- ```
92
+ des_url(optional): if the book cover and book description is not in book_url catalog,you can set this for the cover and description
77
93
 
94
+ ```
78
95
 
79
96
  ## Block parameter specification
80
97
  ```ruby
81
- book.cover_css #book cover image css path
98
+ book.cover_css #book cover image css path
82
99
  book.description_css #book description css path
83
100
  book.title_css #book title css path
84
101
  book.index_item_css #book catalog item css path
@@ -89,7 +106,7 @@ Create book
89
106
  ```
90
107
  ## Perform `create_book` in your terminal
91
108
  ```bash
92
- create_book
109
+ create_book(book_catalog_link, bookname)
93
110
  ```
94
111
  ## Development
95
112
 
data/exe/create_book CHANGED
@@ -6,19 +6,12 @@ require 'yaml'
6
6
 
7
7
  # use like
8
8
  # bin/create_book http://www.quanben5.com/n/jinyiyexing/xiaoshuo.html bookname
9
- url,bookname = ARGV
9
+ url,bookname,des_url = ARGV
10
10
 
11
- default_config = YAML.load(File.open('./default_setting.yml'))
12
11
  #smtp setting
13
12
  EpubBook.configure do |config|
14
- default_config['smtp_config'].each_pair do |key,value|
15
- config.send("#{key}=",value)
16
- end
13
+ config.setting_file = './default_setting.yml'
17
14
  end
18
15
 
19
- EpubBook.create_book(url || default_config['book_url'],bookname || default_config['bookname']) do |book|
20
- default_config['book'].each_pair do |key,value|
21
- book.send("#{key}=",value)
22
- end
23
- end
16
+ EpubBook.create_book(url,bookname,des_url)
24
17
 
@@ -27,8 +27,9 @@ module EpubBook
27
27
 
28
28
  Reg = /<script.*?>.*?<\/script>/m
29
29
 
30
- def initialize(index_url )
30
+ def initialize(index_url,des_url=nil )
31
31
  @index_url = index_url
32
+ @des_url = des_url
32
33
  @user_agent = UserAgent
33
34
  @referer = Referer
34
35
  @folder_name = Base64.urlsafe_encode64(Array(index_url).pack('P'))
@@ -98,6 +99,7 @@ module EpubBook
98
99
  mailer = Mailer.new
99
100
  mailer.to = mail_to
100
101
  mailer.add_file epub_file
102
+ mailer.body = "您创建的电子书[#{book[:title]}]见附件\n"
101
103
 
102
104
  mailer.send_mail
103
105
  end
@@ -105,23 +107,17 @@ module EpubBook
105
107
  end
106
108
 
107
109
 
110
+ #得到书目索引
108
111
  def fetch_index(url=nil)
109
112
  url ||= @index_url
110
113
  doc = Nokogiri::HTML(open(URI.encode(url),"User-Agent" => @user_agent ,'Referer'=> @referer).read)
111
114
  #generate index.yml
112
115
 
113
- book[:title] ||= doc.css(@title_css).text.strip
114
-
115
- if @cover_css && !book[:cover]
116
- cover_url = doc.css(@cover_css).attr("src").to_s
117
- cover_url = link_host + cover_url unless cover_url.start_with?("http")
118
- system("curl #{cover_url} -o #{File.join(@book_path,@cover)} ")
119
- book[:cover] = File.join(@book_path,@cover)
116
+ if !book[:title]
117
+ doc1 = @des_url.nil? ? doc : Nokogiri::HTML(open(URI.encode(des_url),"User-Agent" => @user_agent ,'Referer'=> @referer).read)
118
+ get_des(doc1)
120
119
  end
121
120
 
122
- if @description_css && !book[:description]
123
- book[:description] = doc.css(@description_css).text
124
- end
125
121
 
126
122
  doc.css(@index_item_css).each do |item|
127
123
  _href = URI.encode(item.attr(@item_attr).to_s)
@@ -142,7 +138,6 @@ module EpubBook
142
138
 
143
139
  #保存书目
144
140
  save_book
145
-
146
141
  end
147
142
 
148
143
  def fetch_book
@@ -175,6 +170,24 @@ module EpubBook
175
170
 
176
171
  end
177
172
 
173
+
174
+ private
175
+ #得到书名,介绍,及封面
176
+ def get_des(doc)
177
+ book[:title] = doc.css(@title_css).text.strip
178
+ if @cover_css && !book[:cover]
179
+ cover_url = doc.css(@cover_css).attr("src").to_s
180
+ cover_url = link_host + cover_url unless cover_url.start_with?("http")
181
+ cover_path = File.join(@book_path,@cover)
182
+ system("curl #{cover_url} -o #{cover_path} ")
183
+ book[:cover] = cover_path
184
+ end
185
+
186
+ if @description_css && !book[:description]
187
+ book[:description] = doc.css(@description_css).text
188
+ end
189
+ end
190
+
178
191
  end
179
192
 
180
193
  end
@@ -1,23 +1,9 @@
1
- require 'mail'
2
-
3
1
  module EpubBook
4
2
  class Mailer
5
3
  extend Forwardable
6
- ::Mail.defaults do
7
- delivery_method :smtp, {
8
- :address => EpubBook.config.mail_address,
9
- :port => EpubBook.config.mail_port,
10
- :user_name => EpubBook.config.mail_user_name,
11
- :password => EpubBook.config.mail_password,
12
- :authentication => :plain,
13
- :enable_starttls_auto => true
14
- }
15
- end
16
4
 
17
5
  def_delegators :@mailer, :from,:from=,:to,:to=,:subject,:subject=,:body,:body=,:add_file
18
6
 
19
-
20
-
21
7
  def initialize
22
8
  @mailer = Mail.new do
23
9
  from EpubBook.config.mail_from
@@ -1,3 +1,3 @@
1
1
  module EpubBook
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
data/lib/epub_book.rb CHANGED
@@ -1,44 +1,41 @@
1
1
  require "epub_book/version"
2
2
  require 'epub_book/book'
3
+ require 'mail'
3
4
 
4
5
  module EpubBook
5
6
  # Your code goes here...
6
7
  autoload :Book, "epub_book/book"
7
8
  autoload :Mailer, "epub_book/mailer"
8
9
 
9
- def self.create_book(url,bookname=nil,&block)
10
- epub_book = Book.new(url,&block)
11
- #do |book|
12
- # book.limit = 5
13
- # book.cover_css = '.pic_txt_list .pic img'
14
- # book.description_css = '.box p.description'
15
- # book.title_css = '.pic_txt_list h3 span'
16
- # book.index_item_css = 'ul.list li.c3 a'
17
- # book.body_css = '.wrapper #content'
18
- # book.creator = 'javy_liu'
19
- # book.path = '/home/oswap/ruby_test/epub_book/'
20
- # book.user_agent = ''
21
- # book.referer = ''
22
- # book.mail_to = 'javy_liu@163.com'
23
- #end
24
- #epub_book.fetch_index
25
- epub_book.generate_book(bookname)
26
- end
27
-
28
- Config = Struct.new(:mail_from,:mail_subject,:mail_body,:mail_address,:mail_port,:mail_user_name,:mail_password)
10
+ Config = Struct.new(:setting_file,:mail_from,:mail_subject,:mail_body,:mail_address,:mail_port,:mail_user_name,:mail_password)
29
11
  class Config
30
12
  include Singleton
31
13
  def initialize
32
14
  self.mail_subject = 'epub 电子书'
33
15
  self.mail_body = "您创建的电子书见附件\n"
34
16
  self.mail_port = 25
35
- #mail_from
36
- #mail_address
37
- #mail_user_name
38
- #mail_password
39
17
  end
40
18
  end
41
19
 
20
+ #book initialize, and the block will prior the yml setting
21
+ def self.create_book(url,bookname=nil,des_url=nil)
22
+
23
+ url_host_key = url[/\/\/(.*?)\//,1].tr(?.,'_')
24
+
25
+ epub_book = Book.new(url,des_url) do |book|
26
+ (default_config['book']||{}).merge(default_config[url_host_key]||{}).each_pair do |key,value|
27
+ book.send("#{key}=",value)
28
+ end
29
+ end
30
+
31
+ yield epub_book if block_given?
32
+
33
+ #epub_book.fetch_index
34
+ epub_book.generate_book(bookname)
35
+
36
+ epub_book
37
+ end
38
+
42
39
 
43
40
  def self.config
44
41
  Config.instance
@@ -47,4 +44,27 @@ module EpubBook
47
44
  def self.configure
48
45
  yield config
49
46
  end
47
+
48
+ #you can set in configure block or default_setting.yml,and configure block prior the yml setting
49
+ def self.default_config
50
+ unless @default_config
51
+ @default_config= YAML.load(config.setting_file || File.open("#{`pwd`.strip}/default_setting.yml"))
52
+ configure do |_config|
53
+ @default_config['smtp_config'].each_pair do |key,value|
54
+ _config[key] ||= value
55
+ end
56
+ end
57
+ ::Mail.defaults do
58
+ delivery_method :smtp, {
59
+ :address => EpubBook.config.mail_address,
60
+ :port => EpubBook.config.mail_port,
61
+ :user_name => EpubBook.config.mail_user_name,
62
+ :password => EpubBook.config.mail_password,
63
+ :authentication => :plain,
64
+ :enable_starttls_auto => true
65
+ }
66
+ end
67
+ end
68
+ @default_config
69
+ end
50
70
  end
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.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - qmliu
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-08-17 00:00:00.000000000 Z
11
+ date: 2016-08-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: nokogiri