epub-search 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.markdown +3 -0
- data/README.markdown +1 -1
- data/app/watch.rb +60 -63
- data/lib/epub/search/database.rb +37 -21
- data/lib/epub/search/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4ed411d6029d1a65837e0c574e68a698e069da58
|
4
|
+
data.tar.gz: e6d33b7d2c5d246ae7b5fbe66973fb058a3db5a6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0e1694e9a1adb8108eea54288e5117d3f062206ff1e39afb3f44103814add105ccb59bba79b3ecd91eade825ca01f598411bb3b346783fba04bfee6332295a87
|
7
|
+
data.tar.gz: bb52658436263d8b3d8647a0d905f347b3c96a3f4132ea0d58d23acd1f07d017374f898fc0b384201fcd4ce452f9ace0048b34a88ff1142a510202e0cfcf6566
|
data/CHANGELOG.markdown
CHANGED
@@ -4,6 +4,9 @@ Change Log
|
|
4
4
|
0.0.3
|
5
5
|
-----
|
6
6
|
* Specify the version of Listen gem as '>= 1.0.0' to aopt to changed API
|
7
|
+
* Use monitor for thread safety
|
8
|
+
* Remove record for book which was removed during watcher was sleeping
|
9
|
+
* Add recored of book which was added during watcher was sleeping
|
7
10
|
|
8
11
|
0.0.2
|
9
12
|
-----
|
data/README.markdown
CHANGED
data/app/watch.rb
CHANGED
@@ -13,11 +13,8 @@ class Watch
|
|
13
13
|
|
14
14
|
def run(notify_on_change: true, daemonize: false, debug: false)
|
15
15
|
@notify, @daemonize, @debug = notify_on_change, daemonize, debug
|
16
|
-
|
17
|
-
|
18
|
-
$stderr.puts "daemonize: #{@daemonize}"
|
19
|
-
$stderr.puts "debug: #{@debug}"
|
20
|
-
end
|
16
|
+
|
17
|
+
log_run_parameters if @debug
|
21
18
|
|
22
19
|
$PROGRAM_NAME = File.basename($PROGRAM_NAME)
|
23
20
|
$stderr.puts 'start to watch:'
|
@@ -30,52 +27,7 @@ class Watch
|
|
30
27
|
EPUB::Search::Database::Actor.supervise_as :db, @db
|
31
28
|
|
32
29
|
catch_up
|
33
|
-
|
34
|
-
Listen.to! *@directories, :filter => EPUB_RE do |modified, added, removed|
|
35
|
-
modified.each do |file_path|
|
36
|
-
next unless file_path =~ EPUB_RE
|
37
|
-
file_path.force_encoding 'UTF-8'
|
38
|
-
begin
|
39
|
-
$stderr.puts "update #{file_path}"
|
40
|
-
Celluloid::Actor[:db].async.update file_path
|
41
|
-
title = EPUB::Parser.parse(file_path).title
|
42
|
-
notify %Q|UPDATED: #{title}\n#{file_path}|
|
43
|
-
FileUtils.touch exit_time_file
|
44
|
-
rescue => error
|
45
|
-
$stderr.puts error
|
46
|
-
$stderr.puts error.backtrace if @debug
|
47
|
-
end
|
48
|
-
end
|
49
|
-
added.each do |file_path|
|
50
|
-
next unless file_path =~ EPUB_RE
|
51
|
-
file_path.force_encoding 'UTF-8'
|
52
|
-
begin
|
53
|
-
Celluloid::Actor[:db].async.add file_path
|
54
|
-
title = EPUB::Parser.parse(file_path).title
|
55
|
-
notify %Q|ADDED: #{title}\n#{file_path}|
|
56
|
-
FileUtils.touch exit_time_file
|
57
|
-
rescue => error
|
58
|
-
$stderr.puts error
|
59
|
-
$stderr.puts error.backtrace if @debug
|
60
|
-
end
|
61
|
-
end
|
62
|
-
removed.each do |file_path|
|
63
|
-
next unless file_path =~ EPUB_RE
|
64
|
-
file_path.force_encoding 'UTF-8'
|
65
|
-
begin
|
66
|
-
Celluloid::Actor[:db].async.remove file_path
|
67
|
-
notify %Q|REMOVED:\n#{file_path}|
|
68
|
-
FileUtils.touch exit_time_file
|
69
|
-
rescue => error
|
70
|
-
$stderr.puts error
|
71
|
-
$stderr.puts error.backtrace if @debug
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
ensure
|
76
|
-
pid_file.delete
|
77
|
-
FileUtils.touch exit_time_file
|
78
|
-
end
|
30
|
+
start_watch
|
79
31
|
end
|
80
32
|
|
81
33
|
private
|
@@ -97,26 +49,58 @@ class Watch
|
|
97
49
|
end
|
98
50
|
|
99
51
|
def catch_up
|
52
|
+
locations = @db.locations
|
100
53
|
@directories.each do |dir|
|
101
54
|
Dir["#{dir}/**/*.epub"].each do |file_path|
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
55
|
+
if locations.include? file_path
|
56
|
+
next if File.file? exit_time_file and File.mtime(file_path) < exit_time
|
57
|
+
begin
|
58
|
+
removed = Celluloid::Actor[:db].remove(file_path)
|
59
|
+
Celluloid::Actor[:db].async.add file_path
|
60
|
+
operation = removed.zero? ? 'ADDED' : 'UPDATED'
|
61
|
+
title = EPUB::Parser.parse(file_path).title
|
62
|
+
notify "#{operation}: #{title}\n#{file_path}"
|
63
|
+
FileUtils.touch exit_time_file
|
64
|
+
rescue => error
|
65
|
+
$stderr.puts error
|
66
|
+
$stderr.puts error.backtrace if @debug
|
67
|
+
end
|
68
|
+
else
|
69
|
+
on_file_changed file_path, :add, 'ADDED'
|
113
70
|
end
|
114
71
|
end
|
115
72
|
end
|
116
|
-
|
73
|
+
locations.each do |location|
|
74
|
+
on_file_changed location.dup, :remove, 'REMOVED' unless File.exist? location
|
75
|
+
end
|
76
|
+
rescue => err
|
77
|
+
$stderr.puts err
|
117
78
|
pid_file.unlink
|
118
79
|
end
|
119
80
|
|
81
|
+
def start_watch
|
82
|
+
Listen.to! *@directories, :filter => EPUB_RE do |modified, added, removed|
|
83
|
+
modified.each do |file_path|
|
84
|
+
on_file_changed file_path, :update, 'UPDATED'
|
85
|
+
end
|
86
|
+
added.each do |file_path|
|
87
|
+
on_file_changed file_path, :add, 'ADDED'
|
88
|
+
end
|
89
|
+
removed.each do |file_path|
|
90
|
+
on_file_changed file_path, :remove, 'REMOVED'
|
91
|
+
end
|
92
|
+
end
|
93
|
+
ensure
|
94
|
+
pid_file.delete
|
95
|
+
FileUtils.touch exit_time_file
|
96
|
+
end
|
97
|
+
|
98
|
+
def log_run_parameters
|
99
|
+
$stderr.puts "notify_on_change: #{@notify}"
|
100
|
+
$stderr.puts "daemonize: #{@daemonize}"
|
101
|
+
$stderr.puts "debug: #{@debug}"
|
102
|
+
end
|
103
|
+
|
120
104
|
def write_pid_file
|
121
105
|
if pid_file.exist?
|
122
106
|
pid = pid_file.read.to_i
|
@@ -132,6 +116,19 @@ class Watch
|
|
132
116
|
end
|
133
117
|
end
|
134
118
|
|
119
|
+
def on_file_changed(file_path, operation, label)
|
120
|
+
file_path.force_encoding 'UTF-8'
|
121
|
+
begin
|
122
|
+
Celluloid::Actor[:db].async.__send__ operation, file_path
|
123
|
+
title = operation == :remove ? '' : EPUB::Parser.parse(file_path).title
|
124
|
+
notify %Q|#{label}: #{title}\n#{file_path}|
|
125
|
+
FileUtils.touch exit_time_file
|
126
|
+
rescue => error
|
127
|
+
$stderr.puts error
|
128
|
+
$stderr.puts error.backtrace if @debug
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
135
132
|
def notify(message)
|
136
133
|
$stderr.puts message
|
137
134
|
Notify.notify $PROGRAM_NAME, message if notify?
|
data/lib/epub/search/database.rb
CHANGED
@@ -1,12 +1,17 @@
|
|
1
|
+
require 'monitor'
|
2
|
+
|
1
3
|
module EPUB
|
2
4
|
module Search
|
3
5
|
class Database
|
6
|
+
include MonitorMixin
|
7
|
+
|
4
8
|
DIR_NAME = 'db'
|
5
9
|
FILE_NAME = 'epub-search.db'
|
6
10
|
|
7
11
|
attr_reader :db_dir
|
8
12
|
|
9
13
|
def initialize(db_dir)
|
14
|
+
super()
|
10
15
|
@db_dir = Pathname === db_dir ? db_dir : Pathname.new(db_dir)
|
11
16
|
Groonga::Context.default_options = {:encoding => :utf8}
|
12
17
|
end
|
@@ -49,22 +54,25 @@ module EPUB
|
|
49
54
|
file_path = Pathname.new(file_path) unless file_path.kind_of? Pathname
|
50
55
|
location = file_path.expand_path
|
51
56
|
book = EPUB::Parser.parse(location)
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
57
|
+
contents = book.each_content.select {|content|
|
58
|
+
content.media_type == 'application/xhtml+xml'
|
59
|
+
}
|
60
|
+
contents.each do |content|
|
61
|
+
doc = Nokogiri.XML(content.read)
|
62
|
+
title_elem = doc.search('title').first
|
63
|
+
page_title = title_elem ? title_elem.text : ''
|
64
|
+
body = Nokogiri.XML(doc.search('body').first.to_xml).content
|
65
|
+
synchronize do
|
66
|
+
open do
|
67
|
+
pages.add('location' => location.to_s,
|
68
|
+
'iri' => content.href.to_s,
|
69
|
+
'book_title' => book.title,
|
70
|
+
'page_title' => page_title,
|
71
|
+
'content' => body)
|
72
|
+
end
|
65
73
|
end
|
66
74
|
end
|
67
|
-
|
75
|
+
contents.length
|
68
76
|
end
|
69
77
|
|
70
78
|
# @param [Pathname|String] file_path path of book
|
@@ -73,13 +81,15 @@ module EPUB
|
|
73
81
|
file_path = Pathname.new(file_path) unless file_path.kind_of? Pathname
|
74
82
|
location = file_path.expand_path
|
75
83
|
record_count = 0
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
record
|
82
|
-
|
84
|
+
synchronize do
|
85
|
+
open do
|
86
|
+
records = pages.select {|record|
|
87
|
+
record.location == location.to_path
|
88
|
+
}
|
89
|
+
records.each do |record|
|
90
|
+
record.key.delete
|
91
|
+
record_count += 1
|
92
|
+
end
|
83
93
|
end
|
84
94
|
end
|
85
95
|
record_count
|
@@ -110,6 +120,12 @@ module EPUB
|
|
110
120
|
end
|
111
121
|
end
|
112
122
|
|
123
|
+
def locations
|
124
|
+
open do
|
125
|
+
pages.group_by(&:location).collect {|location, records| location}
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
113
129
|
private
|
114
130
|
|
115
131
|
def open
|
data/lib/epub/search/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: epub-search
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- KITAITI Makoto
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-
|
11
|
+
date: 2013-05-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: epub-parser
|
@@ -275,7 +275,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
275
275
|
version: '0'
|
276
276
|
requirements: []
|
277
277
|
rubyforge_project:
|
278
|
-
rubygems_version: 2.0.
|
278
|
+
rubygems_version: 2.0.2
|
279
279
|
signing_key:
|
280
280
|
specification_version: 4
|
281
281
|
summary: Full text search for EPUB books
|