This repository has been archived by the owner on Mar 8, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mozshot.rb
executable file
·301 lines (280 loc) · 8.42 KB
/
mozshot.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env ruby
#
# MozShot - Web site thumbnail service by gtkmozembed.
#
# Copyright (C) 2005 Tatsuki Sugiura <[email protected]>
# Released under the License same as Ruby.
#
#
# This was based on MozSnapshooter written by Mirko Maischberger.
# http://mirko.lilik.it/Ruby-GNOME2/moz-snapshooter.rb
#
# Origianl idea by Andrew McCall - <andrew at textux.com>
# http://www.hackdiary.com/archives/000055.html
#
# And I refered many similar implementations. Thanks for all!
#
begin
require 'timestamp'
rescue LoadError
# ignore
end
require 'gtkmozembed'
require 'thread'
require 'timeout'
class MozShot
class InternalError < StandardError; end
def initialize(useropt = {})
if ENV['MOZILLA_FIVE_HOME']
Gtk::MozEmbed.set_comp_path(ENV['MOZILLA_FIVE_HOME'])
end
@opt = { :mozprofdir => "#{ENV['HOME']}/.mozilla/mozshot",
:winsize => [800, 800], :imgsize => [],
:timeout => 30, :imgformat => "png",
:keepratio => true, :shot_timeouted => false,
:retry => 0 }
@opt.merge! useropt
@window = nil
@moz = nil
@mutex = Hash.new {|h, k| h[k] = Mutex.new }
if File.directory? "#{opt[:mozprofdir]}/default"
@mozprof = "proc-#{$$}"
puts "Using profile #{@mozprof}"
require 'fileutils'
begin
FileUtils.cp_r "#{opt[:mozprofdir]}/default",
"#{opt[:mozprofdir]}/#{@mozprof}"
rescue Errno::EEXIST
# ignore.... HMmmmmmmmmmmm............
end
begin
File.unlink "#{opt[:mozprofdir]}/#{@mozprof}/lock"
rescue Errno::ENOENT
# ignore
end
# Signal trap will not works...?
trap(:INT, "FileUtils.rm_rf('#{opt[:mozprofdir]}/#{@mozprof}')".untaint )
trap(:QUIT, "FileUtils.rm_rf('#{opt[:mozprofdir]}/#{@mozprof}')".untaint )
trap(:TERM, "FileUtils.rm_rf('#{opt[:mozprofdir]}/#{@mozprof}')".untaint )
trap(:ABRT, "FileUtils.rm_rf('#{opt[:mozprofdir]}/#{@mozprof}')".untaint )
else
@mozprof = 'default'
end
Gtk.init
Gtk::MozEmbed.set_profile_path opt[:mozprofdir], @mozprof
@gtkthread = Thread.new { Gtk.main }
end
attr_accessor :opt, :gtkthread, :mozprof
def join
@gtkthread.join
end
def renew_mozwin(useropt = {})
topt = opt.dup.merge! useropt
@mutex[:mozwin].synchronize {
unless @moz && @window
w = Gtk::Window.new
w.title = "MozShot -- Initalized"
w.decorated = false
w.has_frame = false
w.border_width = 0
m = Gtk::MozEmbed.new
m.chrome_mask = Gtk::MozEmbed::ALLCHROME
w << m
@moz.nil? or @moz.destroy
@window.nil? or @window.destroy
@window = w
@moz = m
end
}
@window.show_all
@window.move(0,0)
@window.resize(topt[:winsize][0], topt[:winsize][1])
end
def moz_load_url(url, useropt = {})
shotopt = opt.dup.merge! useropt
q = Queue.new
@mutex[:load].synchronize {
renew_mozwin(shotopt)
@moz.stop_load
sig_handle_net = set_sig_handler("net_stop", q)
sig_handle_title = set_sig_handler("title", q)
begin
puts "Loading: #{url}, opt: #{shotopt.inspect}"
@moz.location = url
timeout(shotopt[:timeout]){
sigs = Hash.new
while !(sigs["net_stop"] && sigs["title"])
sigs[q.pop] = true
end
puts "Load Done."
}
rescue Timeout::Error
puts "Load Timeouted."
# TODO
Gtk::Window.toplevels.each { |w|
# I can't close modal dialog....
w.modal? and raise InternalError,
"MozShot gone to wrong state. pelease restart process..."
}
if shotopt[:retry].to_i > 0
puts "timeouted. retring (remain #{shotopt[:retry]})"
shotopt[:retry] -= 1
q.clear
retry
elsif shotopt[:shot_timeouted]
puts "option :shot_timeouted is set, forceing screenshot..."
else
raise
end
end
@moz.signal_handler_disconnect(sig_handle_net)
@moz.signal_handler_disconnect(sig_handle_title)
}
end
def screenshot_file(uri, filename, useropt = {})
File.open(filename, "w") {|f|
f << screenshot(uri, useropt)
}
filename
end
def screenshot(url, useropt = {})
shotopt = opt.dup.merge! useropt
pixbuf = nil
@mutex[:shot].synchronize {
moz_load_url(url, shotopt)
sleep 0.2
pixbuf = getpixbuf(@moz.window, shotopt)
}
if shotopt[:imgsize] && !shotopt[:imgsize].empty? &&
shotopt[:imgsize] != shotopt[:winsize]
width, height = *shotopt[:imgsize]
if shotopt[:keepratio]
ratio = shotopt[:winsize][0].to_f / shotopt[:winsize][1]
if width.to_i.zero? || !height.to_i.zero? && height * ratio < width
width = height * ratio
elsif height.to_i.zero? || !width.to_i.zero? && width / ratio < height
height = width / ratio
end
end
pixbuf = pixbuf.scale(width, height, Gdk::Pixbuf::INTERP_HYPER)
end
buf = pixbuf.save_to_buffer(opt[:imgformat])
pixbuf = nil
buf
end
def set_sig_handler(signame, queue)
!signame || signame.empty? and return nil
@moz.signal_connect(signame) {
begin
Gtk::timeout_add(100) {
queue.push signame
false
}
rescue => e
puts e.class, e.message, e.backtrace
end
}
end
def getpixbuf(gdkw, shotopt = {})
x, y, width, height, depth = gdkw.geometry
pb = Gdk::Pixbuf.from_drawable(nil, gdkw, 0, 0, width, height)
#puts "new pixbuf: #{pb.inspect}"
#GC.trace_object(pb)
pb
end
def cleanup
@moz and moz_load_url("about:blank")
#GC.start
end
def shutdown
Gtk.main_quit
if @mozprof != 'default'
FileUtils.rm_rf("#{opt[:mozprofdir]}/#{@mozprof}")
end
join
end
def ping
@mutex[:shot].synchronize {
true
}
end
end
if __FILE__ == $0
$0 = "#{File.basename(__FILE__, '.rb') } #{ENV['DISPLAY']}"
ms = MozShot.new
if ARGV.length == 0 && !ENV["MOZSHOT_DAEMON_SOCK"]
puts "Usage: $0 <URL> [outputfile (default='mozshot.png')]"
elsif ENV["MOZSHOT_DAEMON_SOCK"]
require 'drb'
require 'rinda/rinda'
sockpath = ms.mozprof == 'default' ? '' : "#{ms.opt[:mozprofdir]}/#{ms.mozprof}/drbsock"
begin
File.unlink sockpath
rescue Errno::ENOENT
# ignore
end
DRb.start_service("drbunix:#{sockpath}", ms)
tsuri = ENV["MOZSHOT_DAEMON_SOCK"]
#ts = Rinda::TupleSpaceProxy.new(DRbObject.new_with_uri(tsuri))
ts = DRbObject.new_with_uri(tsuri)
ms.renew_mozwin
i = 0
loop {
puts "waiting for request..."
req = ts.take [:req, nil, Symbol, Hash]
ts.write [:stat, req[1], :accept, {:pid => $$, :display => ENV['DISPLAY'], :timestamp => Time.now}], 600
puts "took request ##{i}: #{req.inspect}"
ret = {:req => req[3], :server_signature => "#{$$}/#{ENV['DISPLAY']}@#{Socket.gethostname}" }
def ret.merge(hash)
self[:timestamp] = Time.now
super
end
begin
if req[2] == :shot_buf
buf = ms.screenshot(req[3][:uri], req[3][:opt]||{})
buf or raise "[BUG] Unknown Error: screenshot() returned #{buf.inspect}"
ts.write([:ret, req[1], :success, ret.merge(:image => buf)], 300)
elsif req[2] == :shot_file
filename = ms.screenshot_file(req[3][:uri], req[3][:filename],
req[3][:opt]||{})
filename or raise "[BUG] Unknown Error: screenshot_file() returned #{filename.inspect}"
ts.write [:ret, req[1], :success, ret.merge(:filename => filename)], 300
#elsif req[2] == :shutdown
# ts.write [:ret, req[1], :accept, "going shutdown"]
# puts "shutdown request was accepted, going shutdown."
# break
else
raise "Unknown request"
end
rescue MozShot::InternalError => e
ts.write [:ret, req[1], :error, ret.merge(:err => e)], 3600
#raise e
exit!
rescue Timeout::Error, StandardError => e
ts.write [:ret, req[1], :error, ret.merge(:err => e)], 3600
end
begin
ts.take [:stat, req[1], nil, nil], 0
rescue Rinda::RequestExpiredError
# ignore
end
ms.cleanup
STDOUT.flush
i += 1
if i > 60
puts "max request exceeded, exitting..."
$stdout.flush
DRb.stop_service
#Thread.new{ sleep 3; puts "shutdown timeouted!"; exit! }
#break
ms.shutdown # Hmmm....
exit!
end
}
else
ms.screenshot_file ARGV[0], (ARGV[1]|| "mozshot.png")
end
ms.shutdown
end
# vim: set sw=2:
# vim: set sts=2: