chupa-text-decomposer-webkit 1.0.3 → 1.0.4

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: 8517897f7ddbaa789f62ac121f66f875eb6a1dfe
4
- data.tar.gz: 3783319afd02620126d691e9b4c3cce5fa04d8e4
3
+ metadata.gz: 0522e247250744ce0a2df3317cc3226948f152a6
4
+ data.tar.gz: 1162b7e926467f66728378fb4b64cb6928afef96
5
5
  SHA512:
6
- metadata.gz: 8c097c1800d2cbafb859216bc2218ceab2f2005ae8b0c2f2d82eaf42d6c326a1d48411c14e14fdafd2ce54d5d603a658557e0fbfdd04031f02494e3d0c749a12
7
- data.tar.gz: 9465dd3692b81e47fdb5d10147a66f4cc6744561db76b14157d66bcb7f2e3793e0ea4777cd1c0e6b16d7ffda6dc3784dc9f0e8f57faa04432e68e7f1867afa92
6
+ metadata.gz: 7f7030778edcfe73c3a7ef5211c48d7218efc7e10f38cbf6f4a6c9a1e3374c202501669503ded21b5db483b18e48b3406a0890fbb725859d3aca6338a59cd3c2
7
+ data.tar.gz: 0b6aa0901239e94e35d65d70054dcd5d0d58e584034f17208d989ffc51848f3782069b04e45890fb7661697511480d4d1f43fefb9565ff948c5d05cbf5a5b4a2
@@ -22,7 +22,7 @@ end
22
22
 
23
23
  Gem::Specification.new do |spec|
24
24
  spec.name = "chupa-text-decomposer-webkit"
25
- spec.version = "1.0.3"
25
+ spec.version = "1.0.4"
26
26
  spec.homepage = "https://github.com/ranguba/chupa-text-decomposer-webkit"
27
27
  spec.authors = ["Kouhei Sutou"]
28
28
  spec.email = ["kou@clear-code.com"]
data/doc/text/news.md CHANGED
@@ -1,5 +1,13 @@
1
1
  # News
2
2
 
3
+ ## 1.0.4: 2017-07-12
4
+
5
+ ### Improvements
6
+
7
+ * Added more logs.
8
+
9
+ * Supported proxy.
10
+
3
11
  ## 1.0.3: 2017-07-11
4
12
 
5
13
  ### Improvements
@@ -19,7 +19,15 @@ require "webkit2-gtk"
19
19
  module ChupaText
20
20
  module Decomposers
21
21
  class WebKit < Decomposer
22
+ module LogTag
23
+ private
24
+ def log_tag
25
+ "[decomposer][webkit]"
26
+ end
27
+ end
28
+
22
29
  include Loggable
30
+ include LogTag
23
31
 
24
32
  registry.register("webkit", self)
25
33
 
@@ -50,126 +58,163 @@ module ChupaText
50
58
  end
51
59
 
52
60
  def decompose(data)
53
- data.screenshot = create_screenshot(data.source)
61
+ screenshoter = Screenshoter.new(data)
62
+ screenshoter.run
54
63
  data[AVAILABLE_ATTRIBUTE_NAME] = !data.screenshot.nil?
55
64
  yield(data)
56
65
  end
57
66
 
58
- private
59
- def create_screenshot(data)
60
- screenshot = nil
61
-
62
- @@view_context ||= WebKit2Gtk::WebContext.new(ephemeral: true)
63
- view = WebKit2Gtk::WebView.new(context: @@view_context)
64
- window = Gtk::OffscreenWindow.new
65
- window.set_default_size(800, 600)
66
- window.add(view)
67
- window.show_all
68
-
69
- status = {
70
- finished: false,
71
- screenshot: nil,
72
- }
73
- prepare_screenshot(data, view, status)
74
- debug do
75
- "#{log_tag}[load][html] #{data.uri}"
76
- end
77
- view.load_html(data.body, data.uri.to_s)
67
+ class Screenshoter
68
+ include Loggable
69
+ include LogTag
78
70
 
79
- main_context = GLib::MainContext.default
80
- timeout(compute_timeout_second, view, main_context, status) do
81
- until status[:finished]
82
- main_context.iteration(true)
83
- end
71
+ def initialize(data)
72
+ @data = data
73
+ @@view_context ||= create_view_context
74
+ @main_loop = GLib::MainLoop.new(nil, false)
75
+ @timeout_second = compute_timeout_second
84
76
  end
85
77
 
86
- window.destroy
78
+ def run
79
+ view = WebKit2Gtk::WebView.new(context: @@view_context)
80
+ window = Gtk::OffscreenWindow.new
81
+ window.set_default_size(800, 600)
82
+ window.add(view)
83
+ window.show_all
87
84
 
88
- status[:screenshot]
89
- end
85
+ setup_callbacks(view)
90
86
 
91
- def prepare_screenshot(data, view, status)
92
- view.signal_connect("load-changed") do |_, load_event|
93
- debug do
94
- "#{log_tag}[load][#{load_event.nick}] #{view.uri}"
87
+ timeout(view) do
88
+ debug do
89
+ "#{log_tag}[load][HTML] #{@data.uri}"
90
+ end
91
+ view.load_html(@data.source.body, @data.source.uri.to_s)
92
+ @main_loop.run
95
93
  end
96
94
 
97
- case load_event
98
- when WebKit2Gtk::LoadEvent::FINISHED
99
- view.get_snapshot(:full_document, :none) do |_, result|
100
- status[:finished] = true
101
- snapshot_surface = view.get_snapshot_finish(result)
102
- unless snapshot_surface.width.zero?
103
- png = convert_snapshot_surface_to_png(data, snapshot_surface)
104
- status[:screenshot] = Screenshot.new("image/png",
105
- [png].pack("m*"),
106
- "base64")
107
- end
95
+ window.destroy
96
+ end
97
+
98
+ private
99
+ def create_view_context
100
+ context = WebKit2Gtk::WebContext.new(ephemeral: true)
101
+ http_proxy = ENV["http_proxy"]
102
+ https_proxy = ENV["https_proxy"]
103
+ ftp_proxy = ENV["ftp_proxy"]
104
+ if http_proxy or https_proxy or ftp_proxy
105
+ proxy_settings = WebKit2Gtk::NetworkProxySettings.new
106
+ if http_proxy
107
+ proxy_settings.add_proxy_for_scheme("http", http_proxy)
108
+ end
109
+ if https_proxy
110
+ proxy_settings.add_proxy_for_scheme("https", https_proxy)
111
+ end
112
+ if ftp_proxy
113
+ proxy_settings.add_proxy_for_scheme("ftp", ftp_proxy)
108
114
  end
115
+ context.set_network_proxy_settings(:custom, proxy_settings)
109
116
  end
117
+ context
110
118
  end
111
- view.signal_connect("load-failed") do |_, _, failed_uri, error|
112
- status[:finished] = true
113
- error do
114
- message = "failed to load URI: #{failed_uri}: "
115
- message << "#{error.class}(#{error.code}): #{error.message}"
116
- "#{log_tag}[load][failed] #{message}"
119
+
120
+ def setup_callbacks(view)
121
+ view.signal_connect("load-changed") do |_, load_event|
122
+ debug do
123
+ "#{log_tag}[load][#{load_event.nick}] #{view.uri}"
124
+ end
125
+
126
+ case load_event
127
+ when WebKit2Gtk::LoadEvent::FINISHED
128
+ debug do
129
+ "#{log_tag}[screenshot][start] #{view.uri}"
130
+ end
131
+ view.get_snapshot(:full_document, :none) do |_, result|
132
+ @main_loop.quit
133
+ snapshot_surface = view.get_snapshot_finish(result)
134
+ debug do
135
+ size = "#{snapshot_surface.width}x#{snapshot_surface.height}"
136
+ "#{log_tag}[screenshot][finish] #{view.uri}: #{size}"
137
+ end
138
+ unless snapshot_surface.width.zero?
139
+ png = convert_snapshot_surface_to_png(snapshot_surface)
140
+ @data.screenshot = Screenshot.new("image/png",
141
+ [png].pack("m*"),
142
+ "base64")
143
+ end
144
+ end
145
+ end
146
+ end
147
+ view.signal_connect("load-failed") do |_, _, failed_uri, error|
148
+ @main_loop.quit
149
+ error do
150
+ message = "failed to load URI: #{failed_uri}: "
151
+ message << "#{error.class}(#{error.code}): #{error.message}"
152
+ "#{log_tag}[load][failed] #{message}"
153
+ end
154
+ true
117
155
  end
118
- true
119
156
  end
120
- end
121
157
 
122
- def convert_snapshot_surface_to_png(data, snapshot_surface)
123
- screenshot_width, screenshot_height = data.expected_screenshot_size
158
+ def convert_snapshot_surface_to_png(snapshot_surface)
159
+ screenshot_width, screenshot_height = @data.expected_screenshot_size
124
160
 
125
- screenshot_surface = Cairo::ImageSurface.new(:argb32,
126
- screenshot_width,
127
- screenshot_height)
128
- context = Cairo::Context.new(screenshot_surface)
129
- context.set_source_color(:white)
130
- context.paint
161
+ screenshot_surface = Cairo::ImageSurface.new(:argb32,
162
+ screenshot_width,
163
+ screenshot_height)
164
+ context = Cairo::Context.new(screenshot_surface)
165
+ context.set_source_color(:white)
166
+ context.paint
131
167
 
132
- ratio = screenshot_width.to_f / snapshot_surface.width
133
- context.scale(ratio, ratio)
134
- context.set_source(snapshot_surface)
135
- context.paint
168
+ ratio = screenshot_width.to_f / snapshot_surface.width
169
+ context.scale(ratio, ratio)
170
+ context.set_source(snapshot_surface)
171
+ context.paint
136
172
 
137
- png = StringIO.new
138
- screenshot_surface.write_to_png(png)
139
- png.string
140
- end
173
+ png = StringIO.new
174
+ screenshot_surface.write_to_png(png)
175
+ png.string
176
+ end
177
+
178
+ def timeout(view)
179
+ timeout_id = GLib::Timeout.add_seconds(@timeout_second) do
180
+ timeout_id = nil
181
+ error do
182
+ message = "timeout to load URI: #{@timeout_second}s: #{view.uri}"
183
+ message << ": loading" if view.loading?
184
+ "#{log_tag}[load][timeout] #{message}"
185
+ end
186
+ if view.loading?
187
+ view.signal_connect("close") do
188
+ @main_loop.quit
189
+ error do
190
+ "#{log_tag}[load][closed] #{view.uri}"
191
+ end
192
+ end
193
+ view.try_close
194
+ else
195
+ @main_loop.quit
196
+ end
197
+ GLib::Source::REMOVE
198
+ end
141
199
 
142
- def timeout(second, view, main_context, status)
143
- timeout_id = nil
144
- timeout_source = GLib::Timeout.source_new_seconds(second)
145
- timeout_source.set_callback do
146
- timeout_id = nil
147
- status[:finished] = true
148
- error do
149
- message = "timeout to load URI: #{second}s: #{view.uri}"
150
- "#{log_tag}[load][timeout] #{message}"
200
+ begin
201
+ yield
202
+ ensure
203
+ GLib::Source.remove(timeout_id) if timeout_id
151
204
  end
152
- GLib::Source::REMOVE
153
205
  end
154
- timeout_id = timeout_source.attach(main_context)
155
- yield
156
- GLib::Source.remove(timeout_id) if timeout_id
157
- end
158
206
 
159
- def compute_timeout_second
160
- default_timeout = 5
161
- timeout_string =
162
- ENV["CHUPA_TEXT_DECOMPOSER_WEBKIT_TIMEOUT"] || default_timeout.to_s
163
- begin
164
- Integer(timeout_string)
165
- rescue ArgumentError
166
- default_timeout
207
+ def compute_timeout_second
208
+ default_timeout = 5
209
+ timeout_string =
210
+ ENV["CHUPA_TEXT_DECOMPOSER_WEBKIT_TIMEOUT"] || default_timeout.to_s
211
+ begin
212
+ Integer(timeout_string)
213
+ rescue ArgumentError
214
+ default_timeout
215
+ end
167
216
  end
168
217
  end
169
-
170
- def log_tag
171
- "[decomposer][webkit]"
172
- end
173
218
  end
174
219
  end
175
220
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chupa-text-decomposer-webkit
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kouhei Sutou
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-07-11 00:00:00.000000000 Z
11
+ date: 2017-07-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chupa-text