// GetPrice // // Copyright 2004 by Jeff Heaton (http://www.jeffheaton.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 December 14,2004. 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. // import java.net.*; import java.io.*; public class GetPrice { double getPrice(String isbn) throws MalformedURLException,IOException { byte buffer[] = new byte[1024]; String result = ""; // construct the proper URL for the book URL url = new URL("http://www.amazon.com/exec/obidos/tg/detail/-/"+isbn); InputStream is = url.openStream(); // now read the entire contents of the web page boolean done = false; while(!done) { int i = is.read(buffer); if( i!=-1 ) result = result + new String(buffer,0,i); else done = true; } // find the price, it is enclosed by token1 and token2 final String token1 = "$"; final String token2 = ""; int indexToken1 = result.indexOf(token1); if( indexToken1==-1 ) return 0; indexToken1+=token1.length(); int indexToken2 = result.indexOf(token2,indexToken1); if( indexToken2==-1 ) return 0; // Extract the price and convert to a double String strPrice = result.substring(indexToken1,indexToken2); double price = Double.parseDouble(strPrice); return price; } public static void main(String args[]) { try { String isbn = "0782140408"; GetPrice price = new GetPrice(); System.out.println("The price for ISBN#" + isbn + " is: " + price.getPrice("0782140408") ); } catch(Exception e) { e.printStackTrace(); } } }