#!/usr/bin/perl # Amazon/Google Ad Replacement # Version 2.040412 # 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 = "coolstufftoow-21"; # Formating Variables $ad_format = "728x90_as"; $color_border = "336699"; $color_bg = "ffffff"; $color_link = "0000FF"; $color_url = "008000"; $color_text = "000000"; $displayads=3; 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(); # Parse Ad Format $ad_format =~ m|(.*)x(.*)_as|; $AdWidth = $1; $AdHeight = $2; 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 => 4, 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"; } # 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 = "Please visit Amazon.com and support our site."; $xml_result =~ s/([^<]+)<\/ErrorMsg>/$error_msg = $1;/esi; } print "Content-type: text/html; charset=utf-8\n\n"; @color=((hex substr($color_bg, 0, 2)), (hex substr($color_bg, 2, 2)), (hex substr($color_bg, 4, 2))); $color_bordertext=getTextHex($color); $googleads=4; $AdAdjustHeight=$AdHeight-14; $AdSpaceHeight=$AdHeight-14; $AdWidth=$AdWidth-1; $AdSpaceWidth=$AdWidth; if ($AdWidth < "470" && $AdHeight < "61"){$googleads=2;} if ($AdWidth < "121" && $AdHeight < "241"){$googleads=2;} if ($AdWidth < "181" && $AdHeight < "151"){$AdWidth =$AdWidth-$googleads;$googleads=1;$AdSpaceWidth=$AdWidth;} if ($AdWidth > "336" && $AdHeight < "151"){$AdWidth =$AdWidth-$googleads;$AdDirection="H";$AdSpaceWidth = sprintf("%d", $AdWidth/$googleads);} else {$AdDirection="V";} if ($AdWidth < "181" && $AdHeight < "151"){$AdDirection="H";} if ($AdDirection eq "V"){$AdSpaceHeight=sprintf("%d", $AdSpaceHeight/$googleads-2);$AdSpaceWidth=$AdSpaceWidth-1;$AdWidth=$AdWidth-1;$DebugErrors.=":IAmV!:";} $starthtml=set_start(); $endhtml=set_end(); # Create Ads $html .= $starthtml; for($x=1;$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"; } # Start Truncated Product Name $short_filename = $ProductName; @word = split /\(/, $short_filename; $short_filename = $word[0]; $ProductName = $short_filename; $html .= set_html(); if ($AdDirection eq "V"){$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 "$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_start { my $banner_html = qq[
]; } sub set_end { my $banner_html = qq[
AGAR
]; } sub set_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"; if ($use_cache eq "yes") { 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}; 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 = $@; } } $xml_cache{$value} = $xml_result; $xml_cache{$cache_time} = time; untie %xml_cache; close(LOCK); } $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"; }