Today I found a really cool and flexible Web page thumbnailing service at
http://bluga.net/webthumb/ with a nice API.. so I wrote a Ruby library for it. It works well. The service lets you do 250 free requests per month, but over that the price seems very reasonable (compared with the dire alternatives I found).
1 require 'net/http'
2 require 'rubygems'
3 require 'xmlsimple'
4
5 class Nailer
6
7 @@api_baseurl = 'http://webthumb.bluga.net/api.php'
8 @@api_key = 'PUT YOUR API KEY HERE'
9
10 attr_accessor :collection_time, :job_id, :ok
11
12 def initialize(url, width = 1024, height = 768)
13 api_request = %Q{<webthumb><apikey>#{@@api_key}</apikey><request><url>#{url}</url><width>#{width}</width><height>#{height}</height></request></webthumb>}
14
15 result = do_request(api_request)
16
17 if result.class == Net::HTTPOK
18 result_data = XmlSimple.xml_in(result.body)
19 @job_id = result_data['jobs'].first['job'].first['content']
20 @collection_time = Time.now.to_i + result_data['jobs'].first['job'].first['estimate'].to_i
21 @ok = true
22 else
23 @ok = false
24 end
25 end
26
27 def retrieve(size = :small)
28 api_request = %Q{<webthumb><apikey>#{@@api_key}</apikey><fetch><job>#{@job_id}</job><size>#{size.to_s}</size></fetch></webthumb>}
29 result = do_request(api_request)
30 result.body
31 end
32
33 def retrieve_to_file(filename, size = :small)
34 File.new(filename, 'w+').write(retrieve(size.to_s))
35 end
36
37 def ready?
38 return unless Time.now.to_i >= @collection_time
39
40 api_request = %Q{<webthumb><apikey>#{@@api_key}</apikey><status><job>#{@job_id}</job></status></webthumb>}
41 result = do_request(api_request)
42
43 if result.class == Net::HTTPOK
44 @ok = true
45 result_data = XmlSimple.xml_in(result.body)
46 begin
47 @result_url = result_data['jobStatus'].first['status'].first['pickup']
48 @completion_time = result_data['jobStatus'].first['status'].first['completionTime']
49 rescue
50 @collection_time += 60
51 return false
52 end
53 else
54 @ok = false
55 end
56
57 true
58 end
59
60 def ok?
61 @ok == true
62 end
63
64 def wait_until_ready
65 sleep 1 until ready?
66 end
67
68 private
69
70 def do_request(body)
71 api_url = URI.parse(@@api_baseurl)
72 request = Net::HTTP::Post.new(api_url.path)
73 request.body = body
74 Net::HTTP.new(api_url.host, api_url.port).start {|h| h.request(request) }
75 end
76 end
77
78
79
80 url = 'http://www.rubyinside.com/'
81 t = Nailer.new(url)
82
83 if t.ok?
84 t.wait_until_ready
85 t.retrieve_to_file('out1.jpg', :small)
86 t.retrieve_to_file('out2.jpg', :medium)
87 t.retrieve_to_file('out3.jpg', :medium2)
88 t.retrieve_to_file('out4.jpg', :large)
89 puts "Thumbnails saved"
90 else
91 puts "Error"
92 end