# $Id: //ncom_2_4/stores/farm/lib/AMZN.pm#40 $

package AMZN;
use LWP::UserAgent;
use HTTP::Request::Common;
use lib "/ncom/stores/farm/lib";
use FarmAgent;
use strict;

sub link {
	my ($isbn) = shift;
	# Hack to farm their Harry Potter DVD & VHS pids
	if ( $isbn eq '0790767341' ) {
		$isbn = 'B000062TU1';
	} elsif ( $isbn eq '079076735X' ) {
		$isbn = 'B00003CXI1';
	} elsif ( $isbn eq '0790767325' ) {
		$isbn = 'B00003CXI0';
	# Hack to farm Lord of the Rings DVDs.
	} elsif ( $isbn eq '0780638379' ) {
		$isbn = 'B00003CWT6';
	} elsif ( $isbn eq '0780637003' ) {
		$isbn = 'B000065U6Q';
	}

	my ($link) = "http://www.amazon.com/exec/obidos/ASIN/" . $isbn;
	return $link;
}

sub name {
	my ( $pretty ) = shift;
	return "Amazon" if ( $pretty eq "pretty" );
	return "Amazon.com";
}

sub pf_link {
	my ($isbn) = shift;
	my ($link) = 'http://www.pricefarmer.com/cgi-bin/link?site=amzn&isbn=' . $isbn;
	return $link;
}

sub affiliate_link {
	my ($isbn) = shift;
	my ($link) = &link( $isbn );
	$link     .= '/pricefarmerco-20';	# Comment out to disable affiliation.
	# $link   .= '/marchingcom';   # Comment out to disable affiliation.
	return $link;
}

sub avail {
	my ($days) = shift;
	my $avail;

	if ($days == 3) {
		$avail = "2-3 days";
	} elsif ($days == 1) {
		$avail = "24 hours";
	} elsif ($days == 14) {
		$avail = "1-2 weeks";
	} elsif ($days == 35) {
		$avail = "3-5 weeks";
	} elsif ($days == 42) {
		$avail = "4-6 weeks";
	} elsif ($days == 800) {
		$avail = "Advance order";
	} elsif ($days == 900) {
		$avail = "N/A";
	} elsif ($days == 1000) {
		$avail = "N/A";
	} else {
		$avail = $days . " days";
	}
	return $avail;	
}

sub fetch {
	my ($isbn,$master_list_price,$timeout) = @_;
	my ($link,$page,$list_price,$our_price,$club_price,$days_to_ship,$avail,$cover,$rating);
	my ($our_discount,$club_discount);
	my ($res,$ua);
	# Set a default timeout of 30 secs
	$timeout = $timeout || 30;

	if ($isbn) {
		$link = &link($isbn);
		# getstore($link,'amzn.html');
		# $page = get($link);
		# Get the page
		$ua = LWP::UserAgent->new;

		# Set the timeout
		$ua->timeout($timeout);

		# Set the user agent
		$ua->agent(FarmAgent::agent());

		$res = $ua->request (GET $link);

		# Test for success
		if ($res->is_success) {
			$page = $res->content;
		} elsif ( $res->content =~ /\<title\>Amazon\.com\sError\sPage\<\/title\>/i ) {
			return ('amzn',$isbn,0,0,0,0,0,1000,'N');
		} else {
			return 0;
		}

		$page = lc( $page );

		if ( $page =~ /list price.*?\$([0-9.,]+)/s ) {
			$list_price = $1;
			$list_price =~ s/\,//g;
		} else {
			$list_price = 0;
		}
	
		# /s = Treat string as single line. 
		# That is, change ``.'' to match any character whatsoever,
		# even a newline, which it normally would not match.
		if ( $page =~ /<b>price:<\/b>.*?\$([0-9.,]+)/s ) {
			$our_price = $1;
			$our_price =~ s/\,//g;
			$club_price = $our_price;
		} elsif ( $page =~ /our price:.*?\$([0-9.,]+)/s ) {
			$our_price = $1;
			$our_price =~ s/\,//g;
			$club_price = $our_price;
		} elsif ( $page =~ /value priced at:.*?\$([0-9.,]+)/s ) {
			$our_price = $1;
			$our_price =~ s/\,//g;
			$club_price = $our_price;
		} else {
			$our_price = $club_price = 0;
		}

	#	if ( $page =~ m/\$([0-9.,]+)\s+sourcing/ ) {
	#		print STDERR "$isbn sourcing fee: $1.\n";
	#	} else {
	#		print STDERR "$isbn found no sourcing fee.\n";
	#	}

		if ( $page =~ m/(\d(\.\d)?)\s+out\s+of\s+5\s+stars/ ) {
			$rating = $1;
		} else {
			$rating = 0;
		}

		if ( $page =~ /availability:.*?\s+(.*)/ ) {
			$avail = $1;
		} elsif ( $page =~ /usually\s+ships(.*)/ ) {
			$avail = $1;
		} else {
			$avail = "";
		}

		#	print STDERR "\$avail: $avail.\n";

		# Expected problem: Amazon claims (as of 6 August 2001) that the
		# stock quote can be of the form "In stock in X days". They have
		# yet to actually display stock quotes in this form, but they might
		# very well begin doing it soon. If they do we will need to add a
		# regex before the /In stock/i one.

		if ($avail =~ /in \d+ to (\d+) day/) {
			$days_to_ship = $1;
		} elsif ($avail =~ /in \d+ to (\d+) week/) {
			$days_to_ship = $1 * 7;
		} elsif ($avail =~ /in \d+\-(\d+) day/) {
			$days_to_ship = $1;
		} elsif ($avail =~ /in \d+\-(\d+) week/) {
			$days_to_ship = $1 * 7;
		} elsif ($avail =~ /in \d+ to (\d+) business day/) {
			$days_to_ship = $1;
		} elsif ($avail =~ /in \d+\-(\d+) business day/) {	
			$days_to_ship = $1;
		} elsif ($avail =~ /24 hour/ ) {
			$days_to_ship = 1;
		} elsif ($avail =~ /in stock/) {
			$days_to_ship = 1;
		} elsif ($avail =~ /ready\s+to\s+ship/ ) {
			$days_to_ship = 1;
		} elsif ($avail =~ /back order/) {
			$days_to_ship = 35;
		} elsif ($page =~ /and we will ship it to you when it arrive/) {
			$days_to_ship = 800;
		} elsif ($page =~ /will ship it to you as soon as it is available/) {
			$days_to_ship =800;
		} else {
			$days_to_ship = 900;
		}

		if ($page =~ /\/\/images\.amazon\.com\/images\/p\//) {
			$cover = "Y";
		} else {
			$cover = "N";
		}
	
		$our_discount = 0;
		$club_discount = 0;

		# BAMM has a list for this item -- use it
		if (($master_list_price > 0) && ($our_price > 0)) {
			$our_discount = int(.5 + (100 * (1 - ($our_price / $master_list_price))));
		} elsif (($list_price > 0) && ($our_price > 0)) {
			$our_discount = int(.5 + (100 * (1 - ($our_price / $list_price))));
		}

		$club_discount = $our_discount;

	}

	return ('amzn',$isbn,$list_price,$our_price,$club_price,$our_discount,$club_discount,$days_to_ship,$cover,$rating);
}	

1;
