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

Snippets has posted 5883 posts at DZone. View Full User Profile

Ruby Client For Amazon Alexa Site Thumbnail (AST) Service

12.05.2006
| 7966 views |
  • submit to reddit
        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 - 2:09am

wholesale jewelry online, supply vast kinds of styles handmade jewelry,mainly engage in wholesale handmade jewelrysuch as wholesale pearl wholesale crystalwholesale gemstone,wholesale costume jewelry,also wholesale fashion jewelry like wholesale swarovski crystal,wholesale beads,wholesale turquoise,wholesale coral,wholesale costume jewelry ,shell jewelry and discount jewelry, jewelry wholesale just on AYpearl jewelry store.

Snippets Manager replied on Mon, 2012/05/07 - 2:32pm

That would explain why it works for me, as I'm in the GMT timezone :)

Snippets Manager replied on Wed, 2007/01/10 - 10:31pm

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") 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 - 2:32pm

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 ;)