Using GetPrice | Heaton Research

Using GetPrice

The "GetPrice" class is completely self-contianed. To make use of this class you need only include this class in your project. All required functions and methods are contained in this class. The GetPrice class is shown here:

Listing 1: The Amazon.com Bot (GetPrice.java)

import java.net.*;
import java.io.*;

/**
 * GetPrice
 *
 * Copyright 2006 by Jeff Heaton (http://www.heatonresearch.com/)
 *
 * This is a very simple example program that shows how to use a Bot
 * to look a price on Amazon.com. All you have to do is call the
 * getPrice method with the ISBN of a book, and the program will
 * query Amazon for the current price of that book.
 *
 * This program works as of August 10,2006. However, if Amazon
 * changes the format of their HTML, this program will no longer
 * work.
 *
 * This program is for educational purposes only. It is up to you
 * to make sure that it is legal to use this program for what ever
 * purpose you plan use it for. I offer no warranty nor do I accept
 * any responsibility for the use of this program.
 * 
 * @author Jeff Heaton (http://www.heatonresearch.com)
 *
 */
public class GetPrice
{
  /*
   * How big of a buffer to read the HTTP stream with.
   */
  public static int BUFFER_SIZE = 8192;
  
  /*
   * The title of the Amazon item.
   */
  private String title;
  
  /*
   * The price of the Amazon item.
   */
  private double price;

  /**
   * This method downloads the specified URL into a Java
   * String. This is a very simple method, that you can
   * reused anytime you need to quickly grab all data from
   * a specific URL.
   *
   * @param url The URL to download.
   * @return The contents of the URL that was downloaded.
   * @throws IOException Thrown if any sort of error occurs.
   */
  public String downloadPage(URL url) throws IOException
  {
    StringBuffer result = new StringBuffer();
    byte buffer[] = new byte[BUFFER_SIZE];

    InputStream s = url.openStream();
    int size = 0;

    do
    {
      size = s.read(buffer);
      if (size != -1)
        result.append(new String(buffer, 0, size));
    } while (size != -1);

    return result.toString();
  }

  /**
   * This method is very useful for grabbing information from a
   * HTML page.  
   *
   * @param url The URL to download.
   * @param token1 The text, or tag, that comes before the desired text
   * @param token2 The text, or tag, that comes after the desired text
   * @param count Which occurance of token1 to use, 1 for the first
   * @return The contents of the URL that was downloaded.
   * @throws IOException Thrown if any sort of error occurs.
   */
  private String extract(String str, String token1, String token2, int count)
  {
    int location1, location2;

    location1 = location2 = 0;
    do
    {
      location1 = str.indexOf(token1, location1 + 1);

      if (location1 == -1)
        return null;

      count--;
    } while (count > 0);

    location2 = str.indexOf(token2, location1 + 1);
    if (location2 == -1)
      return null;

    return str.substring(location1 + token1.length(), location2);
  }  
  
  
  
  
  /**
   * This method causes this class to load an item from Amazon. After
   * loading the item, you can use getPrice and getTitle to read information
   * about the item. 
   * 
   * @param isbn The ISBN for the item to price.
   * @throws MalformedURLException Thrown if an invalid URL is produced.
   * @throws IOException Thrown if an error occurs while connecting to the server.
   */
  public void load(String isbn)
    throws MalformedURLException,IOException
  {
    // construct the proper URL for the book
    URL url = new URL("http://www.amazon.com/gp/product/"+isbn);

    // now read the entire contents of the web page
    String result = this.downloadPage(url);

    // find the price, it is enclosed by token1 and token2
    final String priceToken1 = "<b class=\"price\">$";
    final String titleToken1 = "<b class=\"sans\">";
    final String token2 = "</b>";
    
    // Extract the price and convert to a double
    String strPrice = extract(result,priceToken1,token2,0);
    title = extract(result,titleToken1,token2,0);
    strPrice = strPrice.trim();
    title = title.trim();
    price = Double.parseDouble(strPrice);
  }

  /**
   * Get the price of the item.
   * @return The price of the item.
   */
  public double getPrice()
  {
    return price;
  }

  /**
   * Get the title of the item.
   * @return The title of the item.
   */
  public String getTitle()
  {
    return title;
  }
  
  
  /**
   * Just a simpe test function for this class.  If you use this class 
   * as part of a bigger application, you should probably remove this
   * method.
   * @param args Not used.
   */
  public static void main(String args[])
  {
    try
    {
      String isbn = "097732060X";
      GetPrice price = new GetPrice();
      price.load(isbn);
      System.out.println("ISBN Number:" + isbn);
      System.out.println("Title: " + price.getTitle());
      System.out.println("Price: " + price.getPrice());
    }
    catch(Exception e)
    {
      e.printStackTrace();
    }
  }
}

This class also contains a main method that is used to test the "GetPrice" class. In addition to testing the class, it also provides a very good example of how to use the "GetPrice" class.

The main method begins by assigning the desired ISBN number to a string and instantiating an instance of the GetPrice class.

try
{
   String isbn = "097732060X";
  GetPrice price = new GetPrice();

Next the "load" method on the GetPrice class is called. This method will load both the price and title of that ISBN.

  price.load(isbn);

Once the title and price have been loaded, they can be displayed by calling the correct getter functions.

  System.out.println("ISBN Number:" + isbn);
  System.out.println("Title: " + price.getTitle());
  System.out.println("Price: " + price.getPrice());
}

There are several exceptions that can be thrown, while checking the price. They are all caught by the following lines of code.

catch(Exception e)
{
  e.printStackTrace();
}

Most exceptions thrown by "GetPrice" will be related to communication errors.

If all that you want to do is make use of the GetPrice class, and you do not really care how it works, this is all that you need to know. However, if you want to see how the GetPrice class was constructed, the next few sections will cover this.

Copyright 2005-2009 by Heaton Research, Inc.