kindler 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: c7c145fec387913b4c7932e17191afaecfd8d600
4
+ data.tar.gz: bc3c783ced0c32f0fbec6b4200d81343b71d1960
5
+ SHA512:
6
+ metadata.gz: 34ab7c1aba8e3ce22b5b6f01e8d6d40ddae6f1b888d8aa08bb1907d1b84875111cbe4f278214fdbdd7e35225b69dbc391a59f8eef3e466ea923251445565b0f4
7
+ data.tar.gz: 65c96edbee7af171a2bcc0bc3fdaa6acb94042725f4166e234fcd33bcb0f61b7e6c3adbff70d398f04fee8bbac81ce1f76815460b613ed11576cd3cf3dd88c24
@@ -6,6 +6,7 @@ require "cgi"
6
6
  require "erb"
7
7
  require "shellwords"
8
8
  require "fileutils"
9
+ require 'thread'
9
10
  # require 'mini_magick'
10
11
  require_relative 'kindler/railtie' if defined?(Rails)
11
12
  require_relative "kindler/version"
@@ -152,37 +153,48 @@ module Kindler
152
153
  end
153
154
 
154
155
  def localize_images
155
- images_count = 1
156
- pages.each do |page|
157
- article = Nokogiri::HTML(page[:content])
158
- article.css('img').each do |img|
159
- begin
160
- # get remote address
161
- image_remote_address = img.attr('src')
162
- unless image_remote_address.start_with?('http')
163
- image_remote_address = URI.join(page[:url], image_remote_address).to_s
164
- end
165
- # get local address
166
- image_data = open(image_remote_address)
167
- image_extname = get_image_extname(image_data,image_remote_address)
168
- image_local_address = File.join(tmp_dir,"#{images_count}#{image_extname}")
169
- # download images
170
- debug "begin fetch image #{image_remote_address}"
171
- debug "save to #{image_local_address}"
172
- #`curl #{image_remote_address} > #{image_local_address}`
173
- File.open(image_local_address,'wb') do |f|
174
- f.write image_data.read
175
- end
176
- debug 'Image saved'
177
- # replace local url address
178
- img.attributes['src'].value = "#{image_local_address}"
179
- page[:content] = article.inner_html
180
- # add to manifest
181
- local_images << "#{image_local_address}"
182
- images_count += 1
183
- rescue Exception => e
184
- debug "got error when fetch and save image: #{e}"
156
+ threads = []
157
+ mutex = Mutex.new
158
+ pages.each_with_index do |page|
159
+ threads << Thread.new do
160
+ download_image_of_article(page, mutex)
161
+ end
162
+ end
163
+
164
+ # join the threads
165
+ threads.map(&:join)
166
+ end
167
+
168
+
169
+ def download_image_of_article(page, mutex)
170
+ article = Nokogiri::HTML(page[:content])
171
+ article.css('img').each do |img|
172
+ begin
173
+ # get remote address
174
+ image_remote_address = img.attr('src')
175
+ unless image_remote_address.start_with?('http')
176
+ image_remote_address = URI.join(page[:url], image_remote_address).to_s
177
+ end
178
+ # get local address
179
+ image_data = open(image_remote_address)
180
+ image_extname = get_image_extname(image_data,image_remote_address)
181
+ image_name = (0...10).map{(65+rand(26)).chr}.join
182
+ image_local_address = File.join(tmp_dir,"#{image_name}#{image_extname}")
183
+ # download images
184
+ debug "begin fetch image #{image_remote_address}"
185
+ debug "save to #{image_local_address}"
186
+ #`curl #{image_remote_address} > #{image_local_address}`
187
+ File.open(image_local_address,'wb') do |f|
188
+ f.write image_data.read
185
189
  end
190
+ debug 'Image saved'
191
+ # replace local url address
192
+ img.attributes['src'].value = "#{image_local_address}"
193
+ page[:content] = article.inner_html
194
+ # add to manifest
195
+ mutex.synchronize { local_images << "#{image_local_address}" }
196
+ rescue Exception => e
197
+ debug "got error when fetch and save image: #{e}"
186
198
  end
187
199
  end
188
200
  end
@@ -1,3 +1,3 @@
1
1
  module Kindler
2
- VERSION = "0.4.3"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -61,7 +61,7 @@ describe "Mobi book file generator" do
61
61
  book.add_page :title=>'page3',:author=>'mike1',:content=>'<img src="http://images.fanpop.com/images/image_uploads/widescreen-season-4-wallpaper-lost-661159_1680_1050.jpg"/>this is the page 3',:wrap=>true
62
62
  book.generate
63
63
  book.should be_generated
64
- File.should be_exist("./#{Kindler::Book::TMP_DIR_PREFIX}#{title}/1.jpg")
64
+ Dir.glob("./#{Kindler::Book::TMP_DIR_PREFIX}#{title}/*.jpg").length.should == 2
65
65
  end
66
66
 
67
67
  it "can access pages information before generate" do
@@ -105,7 +105,7 @@ describe "Mobi book file generator" do
105
105
  book.add_page :title=>'page3',:author=>'mike1',:url => 'http://media2.glamour-sales.com.cn/media/some_url',:content=>'<img src="/media/catalog/category/Stroili_banner_02.jpg"/>this is the page 3',:wrap=>true
106
106
  book.generate
107
107
  book.should be_generated
108
- File.should be_exist("./#{Kindler::Book::TMP_DIR_PREFIX}#{title}/1.jpg")
108
+ Dir.glob("./#{Kindler::Book::TMP_DIR_PREFIX}#{title}/*.jpg").length.should == 2
109
109
  end
110
110
 
111
111
  it "should generate mobi books on specify output_dir " do
metadata CHANGED
@@ -1,30 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kindler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
5
- prerelease:
4
+ version: 0.5.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - dongbin.li
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-06-12 00:00:00.000000000 Z
11
+ date: 2013-07-07 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: nokogiri
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
- - - ! '>='
17
+ - - '>='
20
18
  - !ruby/object:Gem::Version
21
19
  version: '0'
22
20
  type: :runtime
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
- - - ! '>='
24
+ - - '>='
28
25
  - !ruby/object:Gem::Version
29
26
  version: '0'
30
27
  description: kindler is a rubygem allow you to generate kindle mobi book very easily
@@ -57,27 +54,26 @@ files:
57
54
  - spec/spec_helper.rb
58
55
  homepage: https://github.com/29decibel/kindler
59
56
  licenses: []
57
+ metadata: {}
60
58
  post_install_message:
61
59
  rdoc_options: []
62
60
  require_paths:
63
61
  - lib
64
62
  required_ruby_version: !ruby/object:Gem::Requirement
65
- none: false
66
63
  requirements:
67
- - - ! '>='
64
+ - - '>='
68
65
  - !ruby/object:Gem::Version
69
66
  version: '0'
70
67
  required_rubygems_version: !ruby/object:Gem::Requirement
71
- none: false
72
68
  requirements:
73
- - - ! '>='
69
+ - - '>='
74
70
  - !ruby/object:Gem::Version
75
71
  version: '0'
76
72
  requirements: []
77
73
  rubyforge_project: kindler
78
- rubygems_version: 1.8.23
74
+ rubygems_version: 2.0.2
79
75
  signing_key:
80
- specification_version: 3
76
+ specification_version: 4
81
77
  summary: kindler is a rubygem allow you to generate kindle mobi book very easily
82
78
  test_files:
83
79
  - features/basic.feature