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-1 of 1 total  RSS 

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
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS