#!/usr/bin/perl # Amazon/Google Ad Replacement # Version 1.040206 # GPL licensed - http://www.opensource.org/licenses/gpl-license.html # Coded by Geoff Lampe # www.bestdealsdiscounts.com # # Based on Amazon Products Feed Banner # # version 1.030612, coded by MrRat - http://www.mrrat.com, GPL licensed - http://www.opensource.org/licenses/gpl-license.html # # Caching functionality borrowed from # # version 3.030823b, coded by MrRat - http://www.mrrat.com, GPL licensed - http://www.opensource.org/licenses/gpl-license.html # 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. # required options my $associate_id = "coolstufftoown16-20"; # cache settings - file location and number of requests to cache my $use_cache = "yes"; my $cache_file = "apf_xml_cache"; my $cache_max_size = "78"; # 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 my $link_to_apf = "no"; my $location_of_apf = "/cgi-bin/amazon_products_feed.cgi"; # set the locale: "us" or "uk" and if "uk" then you must include your Amazon.co.uk associate id my $locale = "us"; my $uk_associate_id = "coolstufftoown"; # Formating Variables $ad_format = "728x90_as"; $color_border = "336699"; $color_bg = "ffffff"; $color_link = "0000FF"; $color_url = "008000"; $color_text = "000000"; $displayads=3; #if ($MY_variables{script_name} =~ /agar.cgi/) { $debug_state = "on"; $cache_file = "apf_xml_cache.test"; } # skip to the bottom "sub set_html" to edit the HTML that displays the result # for best effect you should add a Unicode charset META tag to the of your page # like this: # that's all you need to change. the code is below. my $developer_token = "D20C431GU1EXU3"; my (%browse_ids, $search, $mode, $browse_id, $item_id, $output_type, $xml_result, @Details, $random_details, $result_link, $this_xml_url, $error_msg); # setup initial variables get_url_input(); if ($locale eq "uk") { $amazon_site = "Amazon.co.uk"; $amazon_server = "xml-eu"; $associate_id = $uk_associate_id; %browse_ids = ( "books_uk" => 1025612, "music" => 694208, "classical" => 229817, "dvd_uk" => 655852, "vhs_uk" => 573400, "electronics_uk" => 560800, "kitchen_uk" => 3147411, "software_uk" => 1025614, "video_games_uk" => 1025616, "toys_uk" => 595314 ); } else { $amazon_site = "Amazon.com"; $amazon_server = "xml"; %browse_ids = ( baby => 540988, books => 1000, classical => 85, dvd => 404276, electronics => 493964, garden => 468250, kitchen => 491864, magazines => 599872, music => 301668, pc_hardware => 565118, photo => 508048, software => 491286, toys => 491290, tools => 468240, vhs => 404274, videogames => 471280 ); } if (!$mode) { $mode = ((keys %browse_ids)[int rand keys %browse_ids]); } if (!$browse_id) { $browse_id = $browse_ids{$mode}; }; $mode =~ s/_/-/g; if ($search) { $search =~ s/\s/\%20/g; $this_xml_url = "http://$amazon_server.amazon.com/onca/xml3?t=$associate_id&dev-t=$developer_token&KeywordSearch=$search&mode=$mode&type=lite&page=1&sort=+salesrank&f=xml&locale=$locale"; } elsif ($item_id) { $this_xml_url = "http://$amazon_server.amazon.com/onca/xml3?t=$associate_id&dev-t=$developer_token&AsinSearch=$item_id&type=lite&f=xml&locale=$locale"; } elsif ($textstream) { $textstream =~ s/\s/\+/g; $this_xml_url = "http://$amazon_server.amazon.com/onca/xml3?t=$associate_id&dev-t=$developer_token&TextStreamSearch=$textstream&mode=$mode&type=lite&f=xml&locale=$locale"; } else { $this_xml_url = "http://$amazon_server.amazon.com/onca/xml3?t=$associate_id&dev-t=$developer_token&BrowseNodeSearch=$browse_id&mode=$mode&type=lite&page=1&sort=+salesrank&f=xml&locale=$locale"; } $debug .=$this_xml_url."
\n"; # request the XML eval 'use LWP::Simple qw($ua get)'; if ($@) { print "Content-type: text/html\n\n"; print "Unable to use LWP::Simple and this script cannot function without it.\n"; exit; } $ua->timeout(15); $xml_result = get_url($this_xml_url); # get all the products and shuffle them $xml_result =~ s/]+)?>(.*?)<\/Details>/push @Details, $1;/gsie; if (@Details) { $good=1; } else { $error_msg = "Sorry no results are currently being returned for this query."; $xml_result =~ s/([^<]+)<\/ErrorMsg>/$error_msg = $1;/esi; } print "Content-type: text/html; charset=utf-8\n\n"; # I might not even need this $html .=''; ## Pick Contrasting Color to border @color=((hex substr($color_bg, 0, 2)), (hex substr($color_bg, 2, 2)), (hex substr($color_bg, 4, 2))); $color_bordertext=getTextHex($color); $debug .= "Bordertext: ".$color_bordertext."
\n"; if ($ad_format eq "728x90_as"){$googleads=3;$starthtml=set_728x90_as_start();$endhtml=set_728x90_as_end();} if ($ad_format eq "468x60_as"){$googleads=1;$starthtml=set_468x60_as_start();$endhtml=set_468x60_as_end();} if ($ad_format eq "120x600_as"){$googleads=3;$starthtml=set_120x600_as_start();$endhtml=set_120x600_as_end();} if ($ad_format eq "300x250_as"){$googleads=3;$starthtml=set_300x250_as_start();$endhtml=set_300x250_as_end();} $html .= $starthtml; for($x=0;$x<=$googleads;$x++) { #print "Hello!!!
"; $Details[$x] =~ s/<([^>]+)>([^<]+)<\/\1>/${$1} = $2;/gsie; if ($link_to_apf eq "yes") { $result_link = $location_of_apf . "?input_search_type=AsinSearch&input_item=$Asin&input_locale=$locale"; } else { $result_link = "http://www.$amazon_site/exec/obidos/ASIN/$Asin/ref=nosim/$associate_id?dev-t=$developer_token"; } if ($ad_format eq "728x90_as"){$html .= set_728x90_as_html();} if ($ad_format eq "468x60_as"){$html .= set_468x60_as_html();} if ($ad_format eq "120x600_as"){$html .= set_120x600_as_html();} if ($ad_format eq "300x250_as"){$html .= set_300x250_as_html();} } $html .= $endhtml; # display result if ($error_msg) { $html = $error_msg; } if ($output_type 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 "Debug Codes:
$debug

\n"; print "$html\n"; exit; # the end - subs below # yep, it's my standard input parser sub get_url_input { my (%FORM,$form_pair,$form_name,$form_value,$item); 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/[^\w|\d|\=|\,|\(|\)|\-|\:]/ /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; } if (%FORM) { $search = $FORM{input_string}; if ($FORM{input_mode} or $FORM{input_id}) { $mode = $FORM{input_mode}; } if ($FORM{input_id}) { $browse_id = $FORM{input_id}; } if ($FORM{input_item}) { $item_id = $FORM{input_item}; } if ($FORM{input_output}) { $output_type = $FORM{input_output}; } if ($FORM{input_locale}) { $locale = $FORM{input_locale}; } if ($FORM{input_stream}) { $textstream = $FORM{input_stream}; } if ($FORM{ad_format}) { $ad_format = $FORM{ad_format}; } if ($FORM{color_border}) { $color_border = $FORM{color_border}; } if ($FORM{color_bg}) { $color_bg = $FORM{color_bg}; } if ($FORM{color_link}) { $color_link = $FORM{color_link}; } if ($FORM{color_url}) { $color_url = $FORM{color_url}; } if ($FORM{color_text}) { $color_text = $FORM{color_text}; } } } # between the qq[ and ]; is the HTML that formats the result. feel free to change it to whatever you want # possible variables are: $result_link, $Asin, $ProductName, $Catalog, $ReleaseDate, $Manufacturer, $ImageUrlSmall, # $ImageUrlMedium, $ImageUrlLarge, $ListPrice, $OurPrice, $UsedPrice sub set_728x90_as_start { my $banner_html = qq[
]; } sub set_728x90_as_end { my $banner_html = qq[
AGAR
]; } sub set_728x90_as_html { my $banner_html = qq[
$ProductName
Buy now for only $OurPrice!
]; } sub set_468x60_as_start { my $banner_html = qq[
]; } sub set_468x60_as_end { my $banner_html = qq[
AGAR
]; } sub set_468x60_as_html { my $banner_html = qq[ $ProductName
Buy now for only $OurPrice! ]; } sub set_120x600_as_start { my $banner_html = qq[
]; } sub set_120x600_as_end { my $banner_html = qq[
AGAR
]; } sub set_120x600_as_html { my $banner_html = qq[
$ProductName
Buy now for only $OurPrice!
]; } sub set_300x250_as_start { my $banner_html = qq[
]; } sub set_300x250_as_end { my $banner_html = qq[
AGAR
]; } sub set_300x250_as_html { my $banner_html = qq[

$ProductName
Buy now for only $OurPrice!
]; } sub get_url { my $value = $_[0]; my $skip = $_[1]; my (%xml_cache,$xml_result,$dbm_error); my $cache_expire = time() - 3600; my $cache_time = $value . "_time"; $debug .= "Checking on Cache... "; if ($use_cache eq "yes") { $debug .= "Cache should be used!
"; eval 'use Fcntl'; $dbm_error = $@; eval 'use DB_File'; $dbm_error .= $@; if (!$dbm_error) { if (-s $cache_file > (7000 * $cache_max_size)) { unlink $cache_file; } eval 'tie(%xml_cache,"DB_File",$cache_file,O_RDONLY)'; $dbm_error = $@; } } if ($xml_cache{$value} and $xml_cache{$cache_time} > $cache_expire) { $xml_result = $xml_cache{$value}; $debug .= "cache status = cached request size " . length($xml_result) . ", total requests cached " . keys(%xml_cache) . "\n"; untie %xml_cache; } else { untie %xml_cache; eval 'use LWP::Simple qw($ua get)'; if ($@) { print "Content-type: text/html\n\n"; print "Unable to use LWP::Simple and this script cannot function without it.\n"; exit; } my $ua = new LWP::UserAgent; $ua->timeout(30); $xml_result = get($value); $xml_result =~ s/\<//<$1em>/gi; $xml_result =~ s/<(\/?)b>/<$1strong>/gi; $xml_result =~ s/
/
/gi; $xml_result =~ s/<\/?p>/

/gi; if (!$xml_result and $skip ne "skip_ok") { $MY_variables{error_msg} = "Sorry, we are currently unable to process your request in a timely manner.
Please try again later.\n"; } if ($use_cache eq "yes" and !$dbm_error) { my $count=0; open(LOCK, ">$cache_file.lock"); until (flock(LOCK,2) or $count > 50) { sleep .10; ++$count; } if ($count > 50) { $dbm_error = "lock failed"; } if (!$dbm_error) { eval 'tie(%xml_cache,"DB_File",$cache_file,O_CREAT|O_RDWR)'; $dbm_error = $@; if (keys(%xml_cache) > $cache_max_size) { undef %xml_cache; $debug .= "cleared cache\n"; } } } $xml_cache{$value} = $xml_result; $xml_cache{$cache_time} = time; untie %xml_cache; close(LOCK); $debug .= "cache status = not cached request size " . length($xml_result) . "\n"; } $xml_result =~ s/([^<]+)<\/ErrorMsg>/if ($skip ne "skip_ok") { $MY_variables{error_msg} .= $1; }/esi; return $xml_result; } sub getTextHex($color){ $debug.= "Number: ".(0.3*$color[0] + 0.59*$color[1] + 0.11*$color[2])."
\n"; return ((0.3*$color[0] + 0.59*$color[1] + 0.11*$color[2]) <= 128) ? "FFFFFF" : "000000"; }