#!/usr/bin/perl -w
use Encode;
$token = "";
$skey = "";
$locale = "fr";
$watch_back_days = 30;
$outfile = "";
require "$ENV{HOME}/.amznwatchrc";
my $out;
my $isopen = 0;
if ($outfile) {
$out = F;
open $out, ">$outfile" || die "cannot open $out";
$isopen = 1;
}
else {
$out = STDOUT;
}
binmode $out, ":utf8";
use Net::Amazon;
#use Log::Log4perl qw(:easy);
#Log::Log4perl->easy_init($DEBUG);
%uas = ();
sub ua {
my $locale = shift @_;
#print STDERR "UA: $locale\n";
if (! exists $uas{$locale}) {
my $ua = Net::Amazon->new(
token => $token,
secret_key => $skey,
locale => $locale,
);
#print STDERR "Made ua{$locale}\n";
$uas{$locale} = $ua;
}
#print STDERR "Return ua{$locale}\n";
return $uas{$locale};
}
my @now = localtime(time - 86400 * $watch_back_days);
my $now = sprintf "%04d%02d%02d", $now[5]+1900, $now[4]+1, $now[3];
&startRSS();
foreach $catalogue (keys %items) {
my ($catalogue_, $locale_) = split /:/, $catalogue;
$locale_ = $locale unless $locale_;
#print STDERR "$catalogue_/$locale_\n";
foreach $item (keys %{$items{$catalogue}}) {
my $val = $items{$catalogue}->{$item};
if ($val =~ /^\d+$/) {
my $resp = &ua($locale_)->search(browsenode => $val, mode => $catalogue_, AssociateTag => 'example-21');
if (!$resp->is_success()) {
print STDERR "Error for $val: ", $resp->message(), "\n";
next;
}
for my $property (sort { &convertDate(&getDate($b)) <=>
&convertDate(&getDate($a)) } $resp->properties) {
my $src = "source = [$catalogue] (browsenode) '$val'";
&property2item($property) if &convertDate(&getDate($property))> $now;
}
}
else {
$search_type = $catalogue_ eq "books" ? "power" : "keyword";
my $resp = &ua($locale_)->search($search_type => $val, mode => $catalogue_, AssociateTag => 'example-21');
if (!$resp->is_success()) {
print STDERR "Error for [$catalogue] ($search_type) '$val': ", $resp->message(), "\n";
next;
}
for my $property (sort { &convertDate(&getDate($b)) <=>
&convertDate(&getDate($a)) } $resp->properties) {
my $src = "source = [$catalogue] ($search_type) '$val'";
&property2item($property, $src) if &convertDate(&getDate($property)) > $now;
}
}
sleep 1;
}
}
&endRSS();
sub getDate {
my $p = shift @_;
return $p->publication_date() if exists $p->{publication_date};
return $p->ReleaseDate() if exists $p->{ReleaseDate};
}
sub convertDate {
my $date = shift @_;
my %m = (
"January" => 1, "February" => 2, "March" => 3, "April" => 4,
"May" => 5, "June" => 6, "July" => 7, "August" => 8,
"September" => 9, "October" => 10, "November" => 11, "December" => 12 );
if (my ($day, $mon, $year) = ($date =~ /(\d+) (\w+), (\d+)/)) {
return sprintf "%04d%02d%02d", $year, $m{$mon}, $day;
}
elsif ($date =~ /^\d+$/) {
return sprintf "%04d0101", $date;
}
elsif ($date =~ /(\d{4})-(\d{2})-(\d{2})/) {
return sprintf "$1$2$3";
}
else {
return 0;
}
}
sub startRSS {
print $out "