#!/usr/bin/perl

# perl -MCPAN -e 'force install "XML::Amazon"'
# perl -MCPAN -e 'force install "Net::Amazon"'
# perl -MCPAN -e 'force install "Cache::File"'
# perl -MCPAN -e 'force install "XML::Simple"'
# perl -MCPAN -e 'force install "Cache::File"'

#use utf8;
#use encoding 'utf8';
# use encoding "latin-1";

use Data::Dumper;

use strict;
use Encode;
use Cache::File;
use Net::Amazon;
use Net::Amazon::Request::Artist;
use Lingua::DE::Sentence;

my @AmazonSenctences		= ();
my $MaxAmazonRecursive		= 5;
my $MaxAmazonSentences		= 20;
my $AmazonTempPath			= "/tmp/amazon";

my $RandomSen			= &AmazonGetSentences("Harry Potter");


sub AmazonGetSentences(){
	
	my $SearchQuery		= shift;
	my @RandomSentences	= ();

	my $cache = Cache::File->new( 
		cache_root        => '/tmp/mycache',
		default_expires   => '20160 min',		# 60 * 24 * 14 = 20160
	);

	my $ua = Net::Amazon->new(
		token => "1YRE8RQWEYZ6AZ1E65R2",
		locale      => 'de',					#  locale => 'us'
		cache       => $cache,
	);

	my $resp = $ua->search(
		artist => "Harry Potter",
	);

	if ( $resp->is_success() ) {

		for my $property ($resp->properties()) {

			my $lastASIN = $property->ASIN();
			&AmazonGetProductDescriptions( $lastASIN, 0 );
			
		}; # for my $property ($resp->properties()) {

	}; # if ( $resp->is_success() ) {

	for ( my $i=0; $i<=$MaxAmazonSentences ;$i++ ) {
		
		next if ( $i > $#AmazonSenctences);
		
		# hole die sätze zufällig aus dem Array
		my $Sentence	= @AmazonSenctences[ int(rand($#AmazonSenctences)) ];
		my $ustring		= Encode::decode( 'iso-8859-1', $Sentence );
		push(@RandomSentences, "$ustring\n");

	}; # for ( my $i=0; $i<=$MaxAmazonSentences ;$i++ ) {
	
	return \@RandomSentences;

}; # sub AmazonGetSentences(){





sub AmazonGetProductDescriptions(){
	
	my $ASIN					= shift;
	my $AmazonRecursiveCount	= shift;

	$AmazonRecursiveCount++;

	# breche ab, wenn mehr als 10 Mal erfolglos versucht wurde, content abzuholen
	return 0 if ( $AmazonRecursiveCount >= $MaxAmazonRecursive );

	my $cache = Cache::File->new( 
		cache_root        => '/tmp/mycache',
		default_expires   => '20160 min',		# 60 * 24 * 14 = 20160
	);

	my $ua = Net::Amazon->new(
		token => "1YRE8RQWEYZ6AZ1E65R2",
		locale      => 'de',			#  locale => 'us'
		cache       => $cache,
	);

	my $resp = $ua->search(
		asin  => ($ASIN ),
	);
		
	if ( $resp->is_success() ) {
	
		for my $property ($resp->properties()) {
			
			my $string = "";
			if ( $property->ProductDescription() ){			# produktbeschreibungne können abgeholt werden, weil sie existieren

				$string				= $property->ProductDescription();
				$string				=~ s/<.*?>//ig;
				eval { 
					my $NewSentences = &get_sentences($string);
					push(@AmazonSenctences, @{$NewSentences} );
				};
							
			} else {										# produktbeschreibungne können NICHT abgeholt werden, weil sie NICHT existieren
				
				for ( $property->similar_asins() ) {
					
					$ASIN = $_;
					&AmazonGetProductDescriptions( $ASIN, $AmazonRecursiveCount );

				}; # for ( $property->similar_asins() ) {

			}; # if ( $property->ProductDescription() ){	
		
		}; # for my $property ($resp->properties()) {

	} else {
	
		# der request ist fehlgeschlagen
		#	sleep 5;
		#	&AmazonGetProductDescriptions($ASIN);

	}; # if ( $resp->is_success() ) {

	return 1;

}; # sub AmazonGetProductDescriptions(){


sub temp(){

		# erstelle den temppath
	mkdir $AmazonTempPath;
	
	my $TempString = time() . rand(100000) . $$;
	my $PathString = md5_hex( $TempString );

	open(BIN,">$AmazonTempPath/$PathString") or warn;
	binmode(BIN, ":utf8"); # erstmal utf8 einschalten
		print BIN @AmazonSenctences;
	close BIN;

	@AmazonSenctences = ();
	
	open(RBIN,"<$AmazonTempPath/$PathString") or warn;
	binmode(RBIN, ":latin-1"); # erstmal utf8 einschalten
		@AmazonSenctences = <RBIN>;
	close RBIN;

};