#!/usr/local/bin/perl -w # # # # Information Retrieval Final Project # # # Yan Gu # # and # # Zhongtian Yan # # Shopping Bot Implementation # # # # # # # # #use strict; #use Carp; use HTML::LinkExtor; use HTTP::Request; use HTTP::Response; use HTTP::Status; use LWP::RobotUA; use URI::URL; #use CGI::Carp 'fatalsToBrowser'; URI::URL::strict( 1 ); # insure that we only traverse well formed URL's require 'cgi-lib.pl'; require 'cookie.lib'; $| = 1; print "Content-type: text/html\n\n"; my $ROBOT_NAME = 'YanBot/1.0'; my $ROBOT_MAIL = 'yan@cs.jhu.edu'; my $robot = new LWP::RobotUA $ROBOT_NAME, $ROBOT_MAIL; $robot->delay(0); #--------------------------------------------------------------- # # #--------------------------------------------------------------- my $AMAZON ="amazon"; my $BOOKPOOL ="bookpool"; my $BMILLION="booksamillion"; my $ACTION = ""; my $QUERY_BOOK =""; my $METHOD = ""; my $thisBot = "http://www.ugrad.cs.jhu.edu/cgi-bin/cgiwrap/yan/shopbot.pl"; my $query_html = "query.html"; # hash of hash {BOOK NAME}{STORE NAME} # for price, author, link, etc information my %price_hash = (); my %link_hash = (); my %review_hash = (); my %shipping_hash = (); my %ordered_items = (); # hash, key is book name, value is author my %author_hash = (); my %usr_address_id =(); my %usr_staus = (); $usr_status{$AMAZON} = "new"; $usr_status{$BMILLION} = "new"; $usr_status{$BOOKPOOL} = "new"; #$BOOK_ID{$store}{$book} = id my %BOOK_ID = (); # all book ids used in actual stores #$user_log{email}{city} = $city; #$user_log{email}{phone} = $phone; my $ID = ""; my $Total = 0; # ... # my %user_log = (); my $Full_Name = " "; my $Address_1 = " "; my $Address_2 = " "; my $City = " "; my $State = " "; my $Zip =" "; my $Phone = " "; my $Country ="United States"; my $LName = " "; my $FName = " " ; #my %BOOK_ID =(); my %BOOK_CNT = (); my %Cookie_Store = (); # # # # my %store_cgi = ( "http://www.amazon.com", "http://www.amazon.com/exec/obidos/search-handle-form/ref=s_b_rs/102-3244886-3801613", "http://www.booksamillion.com", # "http://www.booksamillion.com/ncom/books", " http://www.booksamillion.com/cat/books?&type=title", "http://www.bookpool.com", "http://www.bookpool.com/.x/t5y8k4kufr/ss/1" # "http://www.amazon.com/exec/obidos/search-handle-form/ref=s_sf_b_as/103-7797859-3411850" ); # for stores that use put my %store_formdata = ("http://www.amazon.com", "ix=books&rank=%2Bsalesrank\&fqp=keywords%01"); # # # # my %book_basket = (); open USER, ">>user.log"; open STDERR, ">>err.log"; &load_user_log(); &ReadParse (*req); my $where= $req{'nextstep'}; if (!defined ($where)) { $where = "newsearch"; } if($where eq "newsearch"){ &generate_search_form(); } elsif($where eq "doSearch"){ &search_book(); } elsif($where eq "fillAddr"){ &get_address(); } elsif($where eq "post_data"){ &post_data(); } elsif($where eq "payment"){ &select_payment(); } elsif($where eq "get_payment"){ &get_payment; } elsif($where eq "confirm"){ &confirm(); } elsif($where eq "buyBooks"){ &buy_books();} elsif($where eq "SearchMore"){ &generate_search_form(); }elsif($where eq "PayNow") { # pay &pay_money(); } elsif($where eq "go_to_order"){ &dummy_request_step1; } exit(0); sub pay_money{ &restore_book_order; # restore %BOOK_ID print "
These are the books in your shopping basket"; my $i = 1; print "
| $i. | $book | $value2 | "; $i++; } } print "
Welcome to Book Shopbot"; print "
"; } # search in all book stores sub search_book{ my $index = $req{'index'}; my $keyword = $req{'keywords'}; $keyword =~ s/\+/%2B/g; foreach my $store (keys %store_cgi){ my $base = $store; # print "the base is $base\n"; my $url = $store_cgi{$base}; my $request = new HTTP::Request; if($base eq "http://www.bookpool.com") { &search_bookpool($base, $request, $url, $keyword,$BOOKPOOL); } elsif($base eq "http://www.amazon.com") { &search_amazon($base, $request, $url, $keyword, $AMAZON); } elsif($base eq "http://www.booksamillion.com") { &search_booksamillion($base, $request, $url, $keyword, $BMILLION); } } &print_results; } sub buy_books{ my $num_item = &find_book_id(); # print STDERR $req{'item-row-sum'}, ":", $num_item,"\n"; # my $index = $req{'item-row-sum'} + 1; # $req{'item-row-sum'} += $num_item; &restore_book_order; &restore_cookie; print "These are the books you just select"; print "
"; } sub store_cookie{ my $cookie = ""; foreach my $store (keys %Cookie_Store){ $cookie =$cookie.$store."::".$Cookie_Store{$store}."&&"; } $cookie =~ s/&&$//; print ""; } sub store_book_order{ my $index = shift; foreach my $store (keys %BOOK_ID){ while( my($book, $id) = each %{$BOOK_ID{$store}}){ my $name1 ="book\@info\@bot\@"."$index"; my $name2 = "book\@cnt\@bot\@"."$index"; my $value1 = $store."\@".$book."\@".$id."\@selected"; my $value2 = $BOOK_CNT{$store}{$id}; print ""; print ""; # print "---$index : $store: $book: $value1\n"; print STDERR "$name1*** $name2**$value1**$value2\n"; $index++; } } } sub print_results { my $book = ""; my $i = 0; my $store = ""; &restore_book_order; &restore_cookie; my @all = keys %price_hash; if($#all <0) { print "
Sorry, Search Failed For \"$req{'keywords'}\"
Search Result for \"$req{'keywords'}\"