#!/usr/bin/perl
# version 4.050717, GPL licensed - http://www.opensource.org/licenses/gpl-license.html
# coded by MrRat - http://www.mrrat.com, with collaborative help from users at the APF Forum - http://www.absolutefreebies.com/phpBB2/
# you can make a donation at http://s1.amazon.com/exec/varzea/pay/T3M26803DZOCMK
# This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
# Do not change the subscription_id. Doing so will violate your license to use this product.
use strict;
my ( $debug, $link_to_apf, $location_of_apf, %associate_ids, %MY_variables, %FORM, %AWS_variables );
######################################################
### basic options
## if you want this script to link to the APF script set the next option to "yes" and put the URL of the APF script in the following option
$link_to_apf = "yes";
$location_of_apf = "/cgi-bin/apf4/amazon_products_feed.cgi";
## set the locale: ca,de,fr,uk, or us and you must include your Amazon.co.uk associate id for that locale
$FORM{locale} = "us";
$associate_ids{ca} = "mrratcom08-20";
$associate_ids{de} = "absolutefreeb-21";
$associate_ids{fr} = "mrratcom0d-21";
$associate_ids{jp} = "mrratcom0f-22";
$associate_ids{uk} = "mrratcom-21";
$associate_ids{us} = "freewarfrommrrat";
## for best effect you should add a Unicode charset META tag to the
of your page
## like this:
### end basic options. that's all you need to change.
######################################################
### advanced options
## between and is the HTML that formats the result. feel free to change it to whatever you want.
## possible AWS_variables are: Title, FormattedPrice, Availability, ASIN, Author, Publisher
## possible MY_variables are: result_link, image_url_small, amazon_site
sub set_html { my $banner_html = qq[$debug
script by MrRat
]; }
## if you are familiar with ResponseGroup in ECS you can change these to get different AWS_variables
$MY_variables{ResponseGroups} = "Images,ItemAttributes,OfferFull";
### end advanced options. the code is below.
######################################################
$MY_variables{subscription_id} = "09FVDRT8TEJ64C2A7Y02";
get_url_input();
initialize_locale();
my $cwd = find_current_directory();
my $xml_result = get_url($cwd);
my ($random_details,$error_msg) = select_random_product($xml_result);
build_product($random_details);
my $html = set_html();
build_the_page($html,$error_msg);
exit;
sub find_current_directory {
my $temp_path;
if($ENV{'PATH_TRANSLATED'}) {
$temp_path = $ENV{'PATH_TRANSLATED'};
} else {
$temp_path = $ENV{'SCRIPT_FILENAME'};
}
if ($temp_path) {
$temp_path =~ s|\\|/|g;
return substr($temp_path,0,rindex($temp_path,"/"));
}
}
sub get_url_input {
my ($form_pair,$form_name,$form_value,$item);
if ($ENV{QUERY_STRING}) {
for $form_pair (split(/&/, $ENV{QUERY_STRING})) {
$form_pair =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$form_pair =~ s/[\;|\`|\\|\"|\||\?|\~|\<|\>|\^|\[|\]|\{|\}|\$]/ /g;
($form_name, $form_value) = split(/=/, $form_pair);
if ($form_name eq $form_value) { $form_value = ""; }
$FORM{$form_name} = $form_value;
}
}
foreach $item (@ARGV) {
($form_name, $form_value) = split(/=/, $item);
$FORM{$form_name} = $form_value;
}
}
sub initialize_locale {
$MY_variables{associate_id} = $associate_ids{$FORM{locale}};
if ($FORM{locale} eq "ca") {
$MY_variables{amazon_site} = "amazon.ca";
$MY_variables{amazon_base_url} = "http://www.amazon.ca/exec/obidos/ASIN/";
} elsif ($FORM{locale} eq "de") {
$MY_variables{amazon_site} = "amazon.de";
$MY_variables{amazon_base_url} = "http://www.amazon.de/exec/obidos/ASIN/";
} elsif ($FORM{locale} eq "fr") {
$MY_variables{amazon_site} = "amazon.fr";
$MY_variables{amazon_base_url} = "http://www.amazon.fr/exec/obidos/ASIN/";
} elsif ($FORM{locale} eq "jp") {
$MY_variables{amazon_site} = "amazon.co.jp";
$MY_variables{amazon_base_url} = "http://www.amazon.co.jp/exec/obidos/ASIN/";
} elsif ($FORM{locale} eq "uk") {
$MY_variables{amazon_site} = "amazon.co.uk";
$MY_variables{amazon_base_url} = "http://www.amazon.co.uk/exec/obidos/ASIN/";
} else {
$MY_variables{amazon_site} = "amazon.com";
$MY_variables{amazon_base_url} = "http://www.amazon.com/exec/obidos/ASIN/";
}
}
sub get_url {
my $cwd = shift;
my ($this_xml_url,$xml_result,$url_options);
my $base_url = "http://webservices.$MY_variables{amazon_site}/onca/xml?Service=AWSECommerceService&AssociateTag=$MY_variables{associate_id}&SubscriptionId=$MY_variables{subscription_id}&Version=2005-03-23&ResponseGroup=$MY_variables{ResponseGroups}";
if ($FORM{Operation}) {
if ($FORM{Operation} eq "ItemSearch") {
if ($FORM{Keywords}) {
$FORM{Keywords} =~ s/\s/\%20/g;
$FORM{Keywords} =~ s/\+/\%20/g;
$url_options = "&Keywords=$FORM{Keywords}";
}
if ($FORM{BrowseNode}) {
$url_options = "&BrowseNode=$FORM{BrowseNode}";
}
$this_xml_url = $base_url . "&Operation=ItemSearch&SearchIndex=$FORM{SearchIndex}&$url_options";
} elsif ($FORM{Operation} eq "ItemLookup") {
$this_xml_url = $base_url . "&Operation=ItemLookup&ItemId=$FORM{ItemId}";
}
}
if ($cwd) {
require $cwd . "/SimpleGet.pl";
$xml_result = get($this_xml_url);
}
return $xml_result;
}
sub select_random_product {
my $xml_result = shift;
my (@Details,$random_details,$error_msg);
if ($xml_result) {
$xml_result =~ s/- ]+)?>(.*?)<\/Item>/push @Details, $1;/gsie;
if (@Details) {
$random_details = $Details[rand @Details];
} else {
$error_msg = "Sorry no results are currently being returned for this query.";
$xml_result =~ s/([^<]+)<\/ErrorMsg>/$error_msg = $1;/esi;
}
}
return ($random_details,$error_msg);
}
sub build_product {
my $random_details = shift;
if ($random_details) {
$random_details =~ s/([^<]+)<\/URL>/$MY_variables{image_url_small} = $1/e;
$random_details =~ s/<([^>]+)>([^<]+)<\/\1>/$AWS_variables{$1} = $2;/gsie;
}
if (!$MY_variables{image_url_small}) { $MY_variables{image_url_small} = "http://g-images.amazon.com/images/G/01/books/icons/books-no-image.gif"; }
if ($link_to_apf eq "yes" and $AWS_variables{ASIN}) {
$MY_variables{result_link} = $location_of_apf . "?Operation=ItemLookup&ItemId=$AWS_variables{ASIN}&locale=$FORM{locale}";
} else {
$MY_variables{result_link} = $MY_variables{amazon_base_url} . $AWS_variables{ASIN} . "/" . $MY_variables{associate_id};
}
}
sub build_the_page {
my ($html,$error_msg) = @_;
if ($error_msg) { $html = $error_msg; }
if ($FORM{input_output} and $FORM{input_output} eq "javascript") {
$html =~ s/"/'/g;
$html =~ s/\n/"\);\ndocument.write\("/g;
$html = qq[document.write("] . $html . qq[");\n];
$html =~ s/(document.write\(")?<\/?SCRIPT[^>]*>("\);)?//gi;
}
print "Content-type: text/html; charset=utf-8\n\n";
print "$html\n";
}