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

Ruby Client for Amazon Alexa Site Thumbnail (AST) Service (See related posts)

It's scrappy, but it does the job.

require 'cgi'
require 'openssl'
require 'base64'
require 'open-uri'

access_id = 'YOUR_ACCESS_ID'
secret_id = 'YOUR_SECRET_ID'

source_url = ARGV.first

timestamp = Time.now.strftime("%Y-%m-%dT%H:%M:%SZ")
sig = Base64.encode64(OpenSSL::HMAC::digest(OpenSSL::Digest::Digest.new('SHA1'), secret_id, 'Thumbnail' + timestamp)).strip

url = "http://ast.amazonaws.com/Xino?Action=Thumbnail&AWSAccessKeyId=" + access_id
url << "&Signature=" + CGI.escape(sig)
url << "&Timestamp=" + CGI.escape(timestamp)
url << "&Url=" +  source_url

begin
  doc = open(url).read
rescue
  puts "Could not access AWS"
  exit
end

m = doc.match(/\<aws:thumbnail[^\>]+exists=\"true\"\>(.+?)\<\//i)

if m && m[1]
  thumb_url = m[1]
  thumb_url.gsub!(/\&amp;/, '&')
  File.open("#{source_url}.jpg", "w") { |f| f.write open(thumb_url).read }
  puts "Saved to #{source_url}.jpg"
elsif m && m.match(/exists=\"false\"/)
  puts "No thumbnail for #{source_url}"
else
  puts "Error"
end

Comments on this post

jnewland posts on Dec 06, 2006 at 05:50
Dude. This rocks. I've been wanting this for a long, long time. If snippets had 'favorites', consider this faved.

By the way, I can't find the snippets source anymore - I'll trade you the source for a patch for favorites ;)
PhilBogle posts on Jan 10, 2007 at 21:34
When I try to use this code as written, I get an error from Alexa saying the timestamp has expired. I think this is because the timestamp must be converted to GMT.

In other words:
timestamp = Time.now.gmtime.strftime("%Y-%m-%dT%H:%M:%SZ")</timestamp>

Even with this change I'm getting an authentication error; I need to double check my access keys but also the code for computing signatures.
peter posts on Jan 11, 2007 at 15:20
That would explain why it works for me, as I'm in the GMT timezone :)

You need to create an account or log in to post comments to this site.


Click here to browse all 4858 code snippets

Related Posts