DZone 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
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!(/\&/, '&')
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
Lucy0422 Lucy replied on Wed, 2009/02/18 - 1:09am
Snippets Manager replied on Mon, 2012/05/07 - 1:32pm
Snippets Manager replied on Wed, 2007/01/10 - 9:31pm
timestamp = Time.now.gmtime.strftime("%Y-%m-%dT%H:%M:%SZ") 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.Snippets Manager replied on Mon, 2012/05/07 - 1:32pm