Search for Books on Amazon





Search for Books on Amazon

figs/expert.gif figs/hack57.gif

With the Amazon Web Services Developer's Kit, perform keyword searches and return the results to IRC.

After familiarizing yourself with the previous hacks, you should be well aware of the power of Web Services. Amazon provides a Web Services Developer's Kit, available from http://www.amazon.com/gp/aws/landing.html. You can download this for free and use it to search Amazon for books or other items. To use it from a Java program, you will also need to download the Apache Axis SOAP implementation from http://ws.apache.org/axis.

When you have installed Axis and Amazon Web Services, you can choose to either use Amazon's WSDL directly or use the more friendly com.amazon.soap.axis package. This hack will show you how to use this package to search for books on Amazon.

Amazon doesn't provide a Java package to use their Web Services, so you must generate the com.amazon.soap.axis package yourself. To do this, you must download the Amazon Web Services WSDL file from http://soap.amazon.com/schemas2/AmazonWebServices.wsdl. Now change to the kit/AmazonWebServices/JavaCodeSample directory and execute the client.axis.sh or client.axis.bat script. This will automatically generate the package for you. Make sure all of the JAR files in axis/lib are included in your classpath, along with your new com.amazon.soap.axis package and pircbot.jar.

The IRC bot described in this hack will respond to the !amazon command and use any parameters as keywords when performing a search of Amazon's books. The top three results, if any, will then be returned to the channel.

1 The Code

Save the following as AmazonBot.java :

import java.net.*;

import com.amazon.soap.axis.*;

import org.jibble.pircbot.*;



public class AmazonBot extends PircBot {



    private AmazonSearchService service = new AmazonSearchServiceLocator( );



    public AmazonBot(String name) {

        setName(name);

    }

    

    public void onMessage(String channel, String sender, String login,

            String hostname, String message) {

        

        message = message.trim( );

        String[] parts = message.split("\\s+", 2);

        // Check for the !amazon command and keywords.

        if (parts.length == 2 && parts[0].toLowerCase( ).equals("!amazon")) {

            String keywords = parts[1];

            String results = getBooks(keywords);

            if (results == null) {

                results = "Cannot find any matches.";

            }

            // Send the results back to the channel.

            sendMessage(channel, results);

        }

    }

    

    public String getBooks(String keywords) {



        String topMatches = "Top matches:";



        try {

            AmazonSearchPort port = service.getAmazonSearchPort( );

            KeywordRequest request = new KeywordRequest( );

            request.setKeyword(URLEncoder.encode(keywords, "utf-8"));

            // Search only for books.

            request.setMode("books");

            request.setType("lite");

            ProductInfo results = port.keywordSearchRequest(request);

        

            Details[] details = results.getDetails( );

            for (int i = 0; i < details.length && i < 3; i++) {

                Details current = details[i];

                 topMatches += " " + Colors.BOLD + (i + 1) + ". " + 

                Colors.NORMAL + current.getProductName( );

                // If the book has an author, include it in brackets.

                if (current.getAuthors( ) != null) {

                    topMatches += " (" + current.getAuthors( )[0] + ")";

                }

            }

        }

        catch (Exception e) {

            return null;

        }

        

        return topMatches;

    }

    

}

Now create a main method in AmazonBotMain.java to launch the bot:

public class AmazonBotMain {

    

    public static void main(String[] args) throws Exception {

        AmazonBot bot = new AmazonBot("AmazonBot");

        bot.setVerbose(true);

        bot.connect("irc.freenode.net");

        bot.joinChannel("#irchacks");

    }

    

}

2 Running the Hack

To compile the bot, you will need to have the Axis JAR files in your classpath, along with pircbot.jar. The com.amazon.soap.axis hierarchy can be placed in the current directory, so you won't have to explicitly include it in the classpath.

You can add the relevant entries to your system classpath or specify them on the command line when you compile the bot, for example:

% javac -classpath .:pircbot.jar:axis-1_1/lib/axis.jar: \

axis-1_1/lib/axis-ant.jar: \

axis-1_1/lib/commons-discovery.jar:axis-1_1/lib/commons-logging.jar: \

axis-1_1/lib/jaxrpc.jar:axis-1_1/lib/log4j-1.2.8.jar:axis-1_1/lib/saaj.jar:\

axis-1_1/lib/wsdl4j.jar *.java

You can then run the bot like so:

% java -classpath .:pircbot.jar:axis-1_1/lib/axis.jar: \

axis-1_1/lib/axis-ant.jar: \

axis-1_1/lib/commons-discovery.jar:axis-1_1/lib/commons-logging.jar: \

axis-1_1/lib/jaxrpc.jar:axis-1_1/lib/log4j-1.2.8.jar:axis-1_1/lib/saaj.jar:\

axis-1_1/lib/wsdl4j.jar AmazonBotMain

3 Using the Bot

As soon as the bot joins your channel, it is ready to start looking up books on Amazon. You can pass whatever keywords you want to it, including parts of the title or even the name of an author. Only the first three results will be shown, for example:

<Jibbler> !amazon hacks

<AmazonBot> Top matches: 1. Google Hacks: 100 Industrial-Strength Tips & Tools

(Tara Calishain) 2. The Hack Counter-Hack Training Course: A Desktop Seminar from 

Ed Skoudis, with Video (Edward Skoudis) 3. eBay Hacks: 100 Industrial-Strength Tips and 

Tools (David A. Karp)

Here is another example, which returns three books by the author Eric Dymock:

<Jibbler> !amazon eric dymock

<AmazonBot> Top matches: 1. The Audi File: All Models Since 1888 

(Eric Dymock) 2. Jim Clark: Racing Legend (Eric Dymock) 3. The Renault File: All Models 

Since 1898 (An Eric Dymock Motor Book) (Eric Dymock)

If the bot cannot find any results at all, it will let you know that it could not find any matches.

4 Hacking the Hack

At the moment, the bot uses the getProductName and getAuthors methods to display the name of each book and the first author when it displays the results of each search. There are several other methods in the Details class that you may like to include in your results. Here are some of the most useful ones:


getIsbn

Returns the ISBN for the book. This is a unique identifier and can be used to look up the same product at a later date.


getAgeGroup

If your IRC channel is designed for use by youngsters, you can use this method to determine whether each search result is suitable for them.


getImageUrlLarge

Returns a URL pointing to a large image of the book.


getListPrice

Shows the list price for the book.


getOurPrice

Shows the price of the book on Amazon.


getSalesRank

Shows how popular the book is (a lower value means it is more popular).

Using the getListPrice method, you can add prices to your results. For example:

<Jibbler> !amazon irc hacks

<AmazonBot> Top matches: 1. $24.95 IRC Hacks (Paul Mutton)

Amazon Web Services provide an easy way to get information from the Amazon web site. Without these, making such a bot would have been much more complicated.


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows