768x90 Getting Online Shouldn't be Tough- $7.49 .com Domains at Go Daddy

Snapshot Web Page trough Ruby

Some times, newbie question is valuable for me. Thanks for some one who asked about Snapshot a webpage in ruby by providing web url. Let see it :

  • Sign Up a free account HERE
  • Get Your API Key, after logged in.
  • Create a New File in your Model with name snapweb.rb, than paste code below inside :
require 'net/http'
require 'rubygems'
require 'xmlsimple'

class Snapweb

@@api_baseurl = 'http://webthumb.bluga.net/api.php'
@@api_key = 'fc52f8bdb9e5b86f8150a145d5c8fd97'

attr_accessor :collection_time, :job_id, :ok

def initialize(url, width = 1024, height = 768)
#for safety url you can uncomment it :
#url = url.gsub(/&/, '& a m p ;')
#please delete space in '& a m p ;'

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, 'wb+').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

  • After that in your controller, put it :

class TeapociController < ApplicationController

def index

web_pict = Snapweb.new("http://teapoci.blogspot.com")

if web_pict.ok?
web_pict.wait_until_ready web_pict.retrieve_to_file("#{RAILS_ROOT}/public/images/teapoci_1.jpg", :small)
web_pict.retrieve_to_file("#{RAILS_ROOT}/public/images/teapoci_2.jpg", :medium)
web_pict.retrieve_to_file("#{RAILS_ROOT}/public/images/teapoci_3.jpg", :medium2)
web_pict.retrieve_to_file("#{RAILS_ROOT}/public/images/teapoci_4.jpg", :large)

flash[:notice] = "Thumbnails saved"

else
flash[:notice] = "Error"
end

end
end

  • Now you have the screenshot images at folder /public/images/

Enjoy & Good Luck!!!

Thanks to : Josh

1 Response to "Snapshot Web Page trough Ruby"

Anonymous said...

thank you bos, this is what i'm looking for :). sekali lagi, terima kasih.
akhirnya bisa menikmati long weekend ^_^.

 
powered by Blogger