kindai 1.5.1 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 1.5.1
1
+ 1.6.0
data/kindai.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{kindai}
8
- s.version = "1.5.1"
8
+ s.version = "1.6.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["hitode909"]
12
- s.date = %q{2011-02-11}
12
+ s.date = %q{2011-02-26}
13
13
  s.default_executable = %q{kindai.rb}
14
14
  s.description = %q{kindai.rb is kindai digital library downloader.}
15
15
  s.email = %q{hitode909@gmail.com}
@@ -38,6 +38,7 @@ Gem::Specification.new do |s|
38
38
  "lib/kindai/spread.rb",
39
39
  "lib/kindai/spread_downloader.rb",
40
40
  "lib/kindai/util.rb",
41
+ "lib/kindai/util/database.rb",
41
42
  "publish.rb",
42
43
  "spec/book_downloader_spec.rb",
43
44
  "spec/book_spec.rb",
data/lib/kindai/book.rb CHANGED
@@ -8,7 +8,13 @@ module Kindai
8
8
  raise "not info:ndljp" unless permalink_uri.match(/info\:ndljp/)
9
9
  me = new
10
10
  me.permalink_uri = permalink_uri
11
- me
11
+ return me
12
+ end
13
+
14
+ def self.new_from_local_directory(path)
15
+ metadata = File.join(path, 'metadata')
16
+ permalink = open(metadata).read.chomp
17
+ return self.new_from_permalink(permalink)
12
18
  end
13
19
 
14
20
  # ----- metadata -----
@@ -15,8 +15,9 @@ module Kindai
15
15
  end
16
16
 
17
17
  def download
18
- return false if self.has_file?
19
18
  create_directory
19
+ write_metadata
20
+ return false if self.has_file?
20
21
  download_spreads
21
22
  return true
22
23
  end
@@ -36,6 +37,16 @@ module Kindai
36
37
  return success
37
38
  end
38
39
 
40
+ def write_metadata
41
+ open(metadata_path, 'w') {|f|
42
+ f.puts book.permalink_uri
43
+ } unless File.exists?(metadata_path)
44
+ end
45
+
46
+ def metadata_path
47
+ File.join(book_path, 'metadata')
48
+ end
49
+
39
50
  def has_file?
40
51
  File.directory?(self.book_path) && self.spread_downloaders.all?(&:has_file?)
41
52
  end
@@ -13,6 +13,7 @@ module Kindai::Interface
13
13
  downloader.download
14
14
 
15
15
  publisher = Kindai::Publisher.new_from_path downloader.book_path
16
+ publisher.book = book
16
17
  publisher.publish_auto
17
18
  end
18
19
 
@@ -2,6 +2,7 @@
2
2
  module Kindai
3
3
  class Publisher
4
4
  attr_accessor :root_path
5
+ attr_accessor :book
5
6
 
6
7
  def self.new_from_path(root_path)
7
8
  me = self.new
@@ -41,6 +42,7 @@ module Kindai
41
42
  def publish
42
43
  Kindai::Util.logger.info("publish #{root_path}, #{config(:name)}")
43
44
  raise "no name" unless config(:name)
45
+ self.ensure_book
44
46
  if seems_finished?
45
47
  Kindai::Util.logger.info("already published")
46
48
  return
@@ -56,10 +58,16 @@ module Kindai
56
58
  end
57
59
 
58
60
  def publish_auto
61
+ self.ensure_book
59
62
  self.clone.trim.resize(1280, 960).trim.zip.name('iphone').publish
60
63
  self.clone.trim.resize(600, 800).divide.zip.name('kindle').publish
61
64
  end
62
65
 
66
+ def ensure_book
67
+ @book ||= Kindai::Book.new_from_local_directory(root_path)
68
+ true
69
+ end
70
+
63
71
  # ------------------------------------
64
72
  protected
65
73
 
@@ -90,7 +98,8 @@ module Kindai
90
98
 
91
99
  def trim!(source_path)
92
100
  return trim_path if files(source_path).length == files(trim_path).length
93
- info = config(:trim).kind_of?(Hash) ? config(:trim) : Kindai::Util.trim_info_by_files(original_files)
101
+ info = config(:trim).kind_of?(Hash) ? config(:trim) : Kindai::Util.trim_info_auto(book, original_files)
102
+ Kindai::Util.logger.info "trim position: #{info}"
94
103
  files(source_path).each{|file|
95
104
  dst = File.join(trim_path, File.basename(file))
96
105
  Kindai::Util.trim_file_to(file, dst, info)
@@ -0,0 +1,69 @@
1
+ # -*- coding: utf-8 -*-
2
+ require 'open-uri'
3
+ require 'net/http'
4
+ require 'json'
5
+ require 'ostruct'
6
+
7
+ module Kindai::Util::Database
8
+ ENDPOINT = URI.parse 'http://gigaschema.appspot.com/hitode909/kindai.json'
9
+
10
+ def self.items
11
+ @items ||= JSON.parse(open(ENDPOINT).read)['data'].map{|item|
12
+ begin
13
+ hash = JSON.parse(item['value'])
14
+ self.validate(hash)
15
+ OpenStruct.new(hash)
16
+ rescue => error
17
+ Kindai::Util.logger.warn error
18
+ end
19
+ }.compact
20
+ end
21
+
22
+ def self.item_for_book(book)
23
+ self.items.find{|item| item.uri == book.permalink_uri}
24
+ end
25
+
26
+ def self.save_item(book, info)
27
+ previous_item = self.item_for_book(book)
28
+ if previous_item and previous_item.version >= Kindai::VERSION
29
+ Kindai::Util.logger.warn "Database has newer version of #{book.title}. To save, delete it first."
30
+ return false
31
+ end
32
+
33
+ send_data = {
34
+ 'uri' => book.permalink_uri,
35
+ 'title' => book.title,
36
+ 'author' => book.author,
37
+ 'x' => info[:x],
38
+ 'y' => info[:y],
39
+ 'width' => info[:width],
40
+ 'height' => info[:height],
41
+ 'version' => Kindai::VERSION
42
+ }
43
+ self.validate(send_data)
44
+
45
+ res = Net::HTTP.start(ENDPOINT.host, ENDPOINT.port){|http|
46
+ request = Net::HTTP::Post.new(ENDPOINT.path)
47
+ request.set_form_data({:value => send_data.to_json})
48
+ http.request(request)
49
+ }
50
+ case res.code
51
+ when '200'
52
+ JSON.parse(res.body)
53
+ else
54
+ raise res
55
+ end
56
+ end
57
+
58
+ def self.validate(info)
59
+ %w{uri title author}.each{|key|
60
+ raise "key #{key} is required" unless info.has_key? key
61
+ raise "key #{key} must be kind of String" unless info[key].kind_of? String
62
+ }
63
+
64
+ %w{x y width height version}.each{|key|
65
+ raise "key #{key} is required" unless info.has_key? key
66
+ raise "key #{key} must be kind of Numeric" unless info[key].kind_of? Numeric
67
+ }
68
+ end
69
+ end
data/lib/kindai/util.rb CHANGED
@@ -115,8 +115,30 @@ module Kindai::Util
115
115
  return got
116
116
  end
117
117
 
118
+ def self.trim_info_auto(book, files)
119
+ info = nil
120
+ item = Kindai::Util::Database.item_for_book(book)
121
+ if item
122
+ self.logger.info "found trimming info"
123
+ return {
124
+ :x => item.x,
125
+ :y => item.y,
126
+ :width => item.width,
127
+ :height => item.height,
128
+ }
129
+ end
130
+
131
+ info = self.trim_info_by_files(files)
132
+ self.logger.info "save trimming info"
133
+ Kindai::Util::Database.save_item(book, info)
134
+ return info
135
+ rescue => error
136
+ self.logger.error "#{error.class}: #{error.message}"
137
+ info || self.trim_info_by_files(files)
138
+ end
139
+
118
140
  def self.trim_info_by_files(files)
119
- Kindai::Util.logger.info "get trim info"
141
+ Kindai::Util.logger.info "get trimming info"
120
142
  positions = {:x => [], :y => [], :width => [], :height => []}
121
143
  files.each{|file|
122
144
  pos = trim_info(file)
@@ -132,7 +154,6 @@ module Kindai::Util
132
154
  [:x, :y, :width, :height].each{|key|
133
155
  good_pos[key] = average(positions[key])
134
156
  }
135
- Kindai::Util.logger.info "trim position #{good_pos}"
136
157
  good_pos
137
158
  end
138
159
 
data/lib/kindai.rb CHANGED
@@ -10,10 +10,11 @@ require 'pathname'
10
10
  require 'fileutils'
11
11
 
12
12
  module Kindai
13
- VERSION = File.read(File.join(File.dirname(__FILE__), '../VERSION')).strip
13
+ VERSION = File.read(File.join(File.dirname(__FILE__), '../VERSION')).strip.to_f
14
14
 
15
15
  require 'kindai/cli'
16
16
  require 'kindai/util'
17
+ require 'kindai/util/database'
17
18
  require 'kindai/book'
18
19
  require 'kindai/spread'
19
20
  require 'kindai/book_downloader'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kindai
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.5.1
4
+ version: 1.6.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-02-11 00:00:00.000000000 +09:00
12
+ date: 2011-02-26 00:00:00.000000000 +09:00
13
13
  default_executable: kindai.rb
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: nokogiri
17
- requirement: &2152665880 !ruby/object:Gem::Requirement
17
+ requirement: &2152615900 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ! '>='
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: '0'
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *2152665880
25
+ version_requirements: *2152615900
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rmagick
28
- requirement: &2152664980 !ruby/object:Gem::Requirement
28
+ requirement: &2152614660 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ! '>='
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
- version_requirements: *2152664980
36
+ version_requirements: *2152614660
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: zipruby
39
- requirement: &2152664280 !ruby/object:Gem::Requirement
39
+ requirement: &2152613680 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: '0'
45
45
  type: :runtime
46
46
  prerelease: false
47
- version_requirements: *2152664280
47
+ version_requirements: *2152613680
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: rspec
50
- requirement: &2152663320 !ruby/object:Gem::Requirement
50
+ requirement: &2152612200 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ~>
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: 2.3.0
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *2152663320
58
+ version_requirements: *2152612200
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: bundler
61
- requirement: &2152662500 !ruby/object:Gem::Requirement
61
+ requirement: &2152611260 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ~>
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: 1.0.0
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *2152662500
69
+ version_requirements: *2152611260
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: jeweler
72
- requirement: &2152661560 !ruby/object:Gem::Requirement
72
+ requirement: &2152605260 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ~>
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: 1.5.2
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *2152661560
80
+ version_requirements: *2152605260
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: rcov
83
- requirement: &2152660740 !ruby/object:Gem::Requirement
83
+ requirement: &2152604380 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ! '>='
@@ -88,7 +88,7 @@ dependencies:
88
88
  version: '0'
89
89
  type: :development
90
90
  prerelease: false
91
- version_requirements: *2152660740
91
+ version_requirements: *2152604380
92
92
  description: kindai.rb is kindai digital library downloader.
93
93
  email: hitode909@gmail.com
94
94
  executables:
@@ -117,6 +117,7 @@ files:
117
117
  - lib/kindai/spread.rb
118
118
  - lib/kindai/spread_downloader.rb
119
119
  - lib/kindai/util.rb
120
+ - lib/kindai/util/database.rb
120
121
  - publish.rb
121
122
  - spec/book_downloader_spec.rb
122
123
  - spec/book_spec.rb
@@ -144,7 +145,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
144
145
  version: '0'
145
146
  segments:
146
147
  - 0
147
- hash: 1731474605592743609
148
+ hash: 1870768052266429891
148
149
  required_rubygems_version: !ruby/object:Gem::Requirement
149
150
  none: false
150
151
  requirements: