Amazon Hacks Free Open Book

Amazon Hacks

Previous Section Next Section

Hack 84 Work Around Products Without Images

figs/expert.giffigs/hack84.gif

Those few items that don't have images can be pesky if your application depends on them. The key to weeding out the bad ones is byte size.

Amazon's product database contains millions of items, so it's not surprising that a few of them don't have images. The lack of images can be a problem if your application relies on them. AWS responses don't tell you if an image doesn't exist; in fact, AWS always returns an image URL, but in some cases the image is a single-pixel transparent GIF instead of a picture of the product. For many applications, using the invisible, single-pixel GIF won't be a problem. But if you're watching every pixel and your application works within a strict design, you'll want to find a way to work around this problem.

Although AWS responses don't directly say whether a product has an image or not, they do let you know indirectly. The image at the other end of the image URL will be considerably smaller if it's a single-pixel GIF. With a quick HTTP request, you can find the image byte size and know if it's big or small.

84.1 What You Need

The function can be used in any ASP script; these run on Windows servers running IIS. You'll also need the Microsoft XML Parser, which is usually installed by default when you install Internet Explorer.

84.2 The Code

Create a file called hasImage.asp with the following code:

Function hasImage(asin_in)

    strIURL = "http://images.amazon.com/images/P/" & asin_in
    strIURL = strIURL & ".01.THUMBZZZ.jpg"
        
    On Error Resume Next
        
    Set xmlhttp = Server.CreateObject("Msxml2.SERVERXMLHTTP")
    xmlhttp.Open "GET", strIURL, false
    xmlhttp.Send(Now)
    If Err.Number <> 0 Then
        strResponseBody = ""
        Err.Clear
    Else
        strResponseBody = xmlhttp.responseBody
    End If
        
    On Error GoTo 0
        
    Set xmlhttp = Nothing

    If Len(strResponseBody) > 403 Then
        hasImage = 1
    Else
        hasImage = 0
    End If

End Function

The function requests an image, looks at the byte size, and returns true or false based on what it finds. It just so happens that the number of characters in the single-pixel GIF file is 403. This script assumes that anything greater than that must be a real product image.

84.3 Running the Hack

The hard part in testing this script is finding products without images. Only a small percentage of products don't have images, and there's no way to specifically seek them out. Still, to test it out, add the following lines to hasImage.asp:

    response.write "Asin 1873176945 "
    If hasImage("1873176945") Then
        response.write "has an image."
    Else
        response.write "doesn't have an image!"
    End If

    response.write "<br>"

    response.write "Asin 0596004478 "
    If hasImage("0596004478") Then
        response.write "has an image."
    Else
        response.write "doesn't have an image!"
    End If

Browse to the page on your server:

http://example.com/hasImage.asp

And you should see:

Asin 1873176945 doesn't have an image!
Asin 0596004478 has an image.

Then you can confirm the results by browsing to the product detail pages.

    Previous Section Next Section
    Index: [SYMBOL][A][B][C][D][E][F][G][H][I][J][L][M][N][O][P][Q][R][S][T][U][V][W][X][Z]


         Main Menu
    Main Page
    Table of content
    Copyright
    Credits
    Foreword
    Preface
    Chapter 1. Browsing and Searching
    Chapter 2. Controlling Your Information
    Chapter 3. Participating in the Amazon Community
    Chapter 4. Selling Through Amazon
    Chapter 5. Associates Program
    Chapter 6. Amazon Web Services
    6.1 Hacks #76-100
    6.2 What Are Web Services?
    6.3 Why Expose an API?
    6.4 What You Need
    6.5 What You Can Do
    6.6 Making Requests
    6.7 The RESTful Way: XML/HTTP
    6.8 SOAP Web Services
    6.9 Working With Responses
    Hack 76 View XML Responses in a Browser
    Hack 77 Embed Product Details into a Web Page with PHP
    Hack 78 Program AWS with PHP
    Hack 79 Program AWS with Python
    Hack 80 Program AWS with Perl
    Hack 81 Loop Around the 10-Result Limit
    Hack 82 Program XML/HTTP with VBScript
    Hack 83 Transform AWS Results to HTML with XSLT
    Hack 84 Work Around Products Without Images
    Hack 85 Syndicate a List of Books with RSS
    Hack 86 Import Data Directly into Excel
    Hack 87 Program AWS with SOAP and VB.NET
    Hack 88 Program AWS with SOAP::Lite and Perl
    Hack 89 Program AWS with NuSOAP and PHP
    Hack 90 Create a Wireless Wish List
    Hack 91 Make Product Titles Shorter
    Hack 92 Encode Text for URLs
    Hack 93 Cache Amazon Images Locally
    Hack 94 Cache AWS Responses Locally
    Hack 95 Create an Amazon AIM Bot
    Hack 96 Compare International Sales
    Hack 97 Program AWS with Mozilla
    Hack 98 Search or Browse Amazon with Watson
    Hack 99 Add Cover Art to Your Digital Music Collection
    Hack 100 Using All Consuming's SOAP and REST Interfaces
    Colophon
    Index


    More Books
    PHP Hacks
    Processing Xml With Java - A Guide To Sax, Dom, Jdom, Jaxp, And Trax
    The Koran (Holy Qur'an)
    Macromedia Flash 8 Bible
    Search Engine Optimization for Dummies
    YouTube Traffic
    PHP 5 for Dummies
    Harry Potter and The Chamber of Secrets
    Harry Potter and the Sorcerer's Stone
    The Pilgrim's Progress
    Wireless Hacks
    Flash Hacks. 100 Industrial-Strength Tips & Tools
    PayPal Hacks. 100 Industrial-Strength Tips and Tools
    Amazon Hacks
    Pdf Hacks
    The Da Vinci Code
    Google Hacks
    The Holy Bible
    Windows XP For Dummies
    Harry Potter and the Half-Blood Prince
    Seo Book
    Upgrading and Repairing Networks
    Macromedia Dreamweaver 8 UNLEASHED
    Windows XP Annoyances
    Windows XP Hacks
    Microsoft Windows XP Power Toolkit
    Teach Yourself MS Office In 24Hours
    iPod & iTunes Missing Manual
    PC Hacks 100 Industrial-Strength Tips and Tools
    PC Overclocking, Optimization, and Tuning - 2th Edition
    PC Hardware In A Nutshell 3rd Edition
    PC Hardware in a Nutshell, 2nd Edition
    Upgrading and Repairing PCs
    Google for Dummies
    MySQL Cookbook
    Teach Yourself Macromedia Flash 8 In 24 Hours
    PHP CookBook
    Sams Teach Yourself JavaScript in 24 Hours
    PHP5 Manual
    Free Games Paper Airplanes
    500 Juegos Gratis 500 Giochi Gratis 500 Jeux Gratuits 500 Jogos Gratis 500 Kostenlose Spiele