Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Peter Cooperx http://www.petercooper.co.uk/

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Ruby library for Web site thumbnailer service

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

Ruby Client for Amazon Alexa Site Thumbnail (AST) Service

It's scrappy, but it does the job.

   1  
   2  require 'cgi'
   3  require 'openssl'
   4  require 'base64'
   5  require 'open-uri'
   6  
   7  access_id = 'YOUR_ACCESS_ID'
   8  secret_id = 'YOUR_SECRET_ID'
   9  
  10  source_url = ARGV.first
  11  
  12  timestamp = Time.now.strftime("%Y-%m-%dT%H:%M:%SZ")
  13  sig = Base64.encode64(OpenSSL::HMAC::digest(OpenSSL::Digest::Digest.new('SHA1'), secret_id, 'Thumbnail' + timestamp)).strip
  14  
  15  url = "http://ast.amazonaws.com/Xino?Action=Thumbnail&AWSAccessKeyId=" + access_id
  16  url << "&Signature=" + CGI.escape(sig)
  17  url << "&Timestamp=" + CGI.escape(timestamp)
  18  url << "&Url=" +  source_url
  19  
  20  begin
  21    doc = open(url).read
  22  rescue
  23    puts "Could not access AWS"
  24    exit
  25  end
  26  
  27  m = doc.match(/\<aws:thumbnail[^\>]+exists=\"true\"\>(.+?)\<\//i)
  28  
  29  if m && m[1]
  30    thumb_url = m[1]
  31    thumb_url.gsub!(/\&amp;/, '&')
  32    File.open("#{source_url}.jpg", "w") { |f| f.write open(thumb_url).read }
  33    puts "Saved to #{source_url}.jpg"
  34  elsif m && m.match(/exists=\"false\"/)
  35    puts "No thumbnail for #{source_url}"
  36  else
  37    puts "Error"
  38  end

Change OS X's default screenshot image format

At the prompt, type this:

   1  defaults write com.apple.screencapture type image_format


Replace "image_format" with a file format name, like pdf, png, tiff, etc. Then to take screenshots, Cmd+Shift+4, then drag around the area you want to capture.
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS