require 'net/http' require 'rubygems' require 'xmlsimple' class Nailer @@api_baseurl = 'http://webthumb.bluga.net/api.php' @@api_key = 'PUT YOUR API KEY HERE' attr_accessor :collection_time, :job_id, :ok def initialize(url, width = 1024, height = 768) api_request = %Q{<webthumb><apikey>#{@@api_key}</apikey><request><url>#{url}</url><width>#{width}</width><height>#{height}</height></request></webthumb>} result = do_request(api_request) if result.class == Net::HTTPOK result_data = XmlSimple.xml_in(result.body) @job_id = result_data['jobs'].first['job'].first['content'] @collection_time = Time.now.to_i + result_data['jobs'].first['job'].first['estimate'].to_i @ok = true else @ok = false end end def retrieve(size = :small) api_request = %Q{<webthumb><apikey>#{@@api_key}</apikey><fetch><job>#{@job_id}</job><size>#{size.to_s}</size></fetch></webthumb>} result = do_request(api_request) result.body end def retrieve_to_file(filename, size = :small) File.new(filename, 'w+').write(retrieve(size.to_s)) end def ready? return unless Time.now.to_i >= @collection_time api_request = %Q{<webthumb><apikey>#{@@api_key}</apikey><status><job>#{@job_id}</job></status></webthumb>} result = do_request(api_request) if result.class == Net::HTTPOK @ok = true result_data = XmlSimple.xml_in(result.body) begin @result_url = result_data['jobStatus'].first['status'].first['pickup'] @completion_time = result_data['jobStatus'].first['status'].first['completionTime'] rescue @collection_time += 60 return false end else @ok = false end true end def ok? @ok == true end def wait_until_ready sleep 1 until ready? end private def do_request(body) api_url = URI.parse(@@api_baseurl) request = Net::HTTP::Post.new(api_url.path) request.body = body Net::HTTP.new(api_url.host, api_url.port).start {|h| h.request(request) } end end url = 'http://www.rubyinside.com/' t = Nailer.new(url) if t.ok? t.wait_until_ready t.retrieve_to_file('out1.jpg', :small) t.retrieve_to_file('out2.jpg', :medium) t.retrieve_to_file('out3.jpg', :medium2) t.retrieve_to_file('out4.jpg', :large) puts "Thumbnails saved" else puts "Error" end
If you're using it, you should change :
File.new(filename, 'w+').write(retrieve(size.to_s))
to:
File.new(filename, 'wb+').write(retrieve(size.to_s))
in retrieve_to_file method
data = retrieve(size.to_s) File.open(filename, 'wb') do |f| f.binmode f.write data end
works somewhat better for me. Otherwise I was getting empty, corrupted and partially corrupted images being written to the file.
Put this immediately after the line with `def initialize`:
url = url.gsub(/&/, '&')
Has anyone else noticed that your server will basically stall until the screenshot(s) have finished running? in my case, I am seeing about 20 seconds / screenshot.
Is this just my server, or is there some way of modifying the script so that it runs more "in the background" while still allowing other processes to run?
when we change it to ready? it still waits for the api to take the shot and add it to the system, would rather it do this in the background...
def retrieve_to_file(filename, size = :small) f = File.new(filename, 'w+') size = f.write(retrieve(size.to_s)) f.close return size end
explicitly close the file or it will miss the last couple bytes sometimes.
You need to create an account or log in to post comments to this site.
Thanks
Dan