#!usr/local/bin/perl use CGI; use LWP; use LWP::UserAgent; use HTML::Parse; use HTML::FormatText; use strict; #turn on more error checking use CGI::Carp qw(fatalsToBrowser set_message); BEGIN { sub handle_errors { my $msg = shift; print "Oh Gosh!!"; print "Got an error: $msg"; } set_message(\&handle_errors); } #use CGI::Carp qw(fatalsToBrowser); #GLOBAL VARIABLES my @book; #array storing all books my @pairs; #variables storing information on a single book my $book; my $price; my $format; my $publisher; my $pub_date; my $req; my $isbn; my $buffer; my $titleValue; my $name; my $value; my $title; #END OF GLOBAL VARIABLES read ( STDIN, $buffer, $ENV{CONTENT_LENGTH} ); @pairs = split ( /&/, $buffer ); foreach $titleValue(@pairs) { ($name, $value) = split(/=/, $titleValue); $value =~ tr/+/ /; $title = $value; } #$title = 'Java'; my $url = "http://www.bookplace.co.uk/bookplace/results.asp?SEARCH_FIELD=KEYWORD&TAG=BHSLFX8X99X97X163DLT9W&CID=&SEARCH_TEXT=$title&FORM_OB=%24RANK&x=23&y=14"; # Create a user agent object my $ua = new LWP::UserAgent; $ua->agent("CueCat/0.1 " . $ua->agent); # Create a request my $req = new HTTP::Request GET => $url; $req->content_type('application/x-www-form-urlencoded'); # Pass request to the user agent and get a response back my $res = $ua->request($req); my $content = $res->content; my @content = split("\n", $content); my $start = 0; foreach (@content) { if(//) { $start++; } if($start > 0) { s/\ / /g; my $foo = HTML::FormatText->new->format(parse_html($_)); if($foo !~ /^\s+$/) { if($foo !~ /IMAGE/) { if($foo !~ /TABLE NOT SHOWN/) { push(@book, $foo); } } } if(/Pub\. Date/) { $start = 0; } } } my ( @Title, @Author, @Publisher, @Isbn, @PubData, @ListPrice ); my ( $Title, $Author, $Publisher, $Isbn, $PubData, $ListPrice ); my @Lines; my @eachBook; my $var; my $counter = 0; my $i = 0; foreach $book ( @book ) { if ($book =~ m/This title has a full description. Click the Title to view the details./ ) { next; } if ($book =~ m/Add this item to your basket. You can buy the contents of your basket at any time by pressing 'Checkout' above./ ) { next; } push ( @Lines, $book ); } foreach $var ( @Lines ) { @eachBook->[$i][$counter] = $var; $counter++; if ( $var =~ m/---------------------------------------------------------------------/ ) { $i++; $counter = 0; } } print "Content-type: text/html\n\n"; my $k = 0; for ( $i = 0; $i < @eachBook; $i++ ) { for ( $k = 0; $k < @eachBook; $k++ ) { print "@eachBook->[$i][$k]\n"; } } print "The end\n";