#!/usr/local/bin/perl
use strict;
use NKF;
use LWP::Simple;
use Image::Magick;
my $query = $ENV{'QUERY_STRING'} || "";
my @query = split(/&/, $query);
my ($ASIN, $line, $total, $mod, $cd) = ();
foreach $line (@query) {
if ($line =~ /^ASIN=(.+)$/) {
$ASIN = $1;
if ($ASIN =~ /\%2FASIN\%2F([0-9A-Z]+)\%2F/i) {
$ASIN = $1;
} elsif ($ASIN =~ /\%2Fproduct\%2F([0-9A-Z]+)\%2F/i) {
$ASIN = $1;
} elsif ($ASIN =~ /\%2Fdp\%2F([0-9A-Z]+)\%2F/i) {
$ASIN = $1;
} elsif ($ASIN !~ /^[0-9A-Z]+$/) {
$ASIN = "";
}
}
}
if ($ASIN =~ /^978(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)(\d)([0-9X])$/) {
$total = $1 * 10;
$total = $total + ($2 * 9);
$total = $total + ($3 * 8);
$total = $total + ($4 * 7);
$total = $total + ($5 * 6);
$total = $total + ($6 * 5);
$total = $total + ($7 * 4);
$total = $total + ($8 * 3);
$total = $total + ($9 * 2);
$mod = $total % 11;
$cd = 11 - $mod;
if ($cd == 11) {
$cd = 0;
} elsif ($cd == 10) {
$cd = 'X';
}
$ASIN = $1 . $2 . $3 . $4 . $5 . $6 . $7 . $8 . $9 . $cd;
}
if ($ASIN eq '') {
&form;
} else {
&asin($ASIN);
}
exit;
sub form {
print "Content-type: text/html; charset=euc-jp\r\n\r\n";
print <<"FORM";
Amazon Web Service for TDS
Amazon Web Service for TDS
FORM
return 0;
}
sub asin($) {
my $asin = shift;
my ($line, $product, $manu, $imageuri);
my @author = ();
my @table = ();
my $tag = 'sakanya-22';
my $token = 'D18GAWEG2HI6AF';
my $type = 'lite';
my $output = 'xml';
my $locale = 'jp';
my $uri = 'http://xml-jp.amznxslt.com/onca/xml3?';
$uri = $uri . 't=' . $tag;
$uri = $uri . '&dev-t=' . $token;
$uri = $uri . '&type=' . $type;
$uri = $uri . '&f=' . $output;
$uri = $uri . '&locale=' . $locale;
$uri = $uri . '&AsinSearch=' . $asin;
my $mode = 'blended';
my $encstr = '日本語';
$encstr = &uri_encode($encstr);
my $search = 'http://www.amazon.co.jp/gp/search?';
$search = $search . 'index=' . $mode;
$search = $search . '&tag=' . $tag;
# $search = $search . '&encoding-string-jp=' . $encstr;
$search = $search . '&_encoding=UTF8';
$search = $search . '&keywords=';
my $content = get("$uri");
# $content = Jcode->new($content, 'utf8')->euc;
$content = NKF::nkf("-W8 -e", $content);
my @content = split(/\n/, $content);
foreach $line (@content) {
if ($line =~ /(.*)<\/ProductName>/) {
$product = $1;
} elsif ($line =~ /(.*)<\/Author>/) {
push(@author, $1);
} elsif ($line =~ /(.*)<\/Artist>/) {
push(@author, $1);
} elsif ($line =~ /(.*)<\/Manufacturer>/) {
$manu = $1;
} elsif ($line =~ /(.*)<\/ImageUrlMedium>/) {
$imageuri = $1;
}
}
my $imagetmp = '/tmp/image.tmp';
my $wgetarg = "-q -t 3 -O $imagetmp";
system("/usr/local/bin/wget $wgetarg '$imageuri'");
my $image = new Image::Magick;
$image->Read("$imagetmp");
my ($width, $height) = $image->Get('columns', 'height');
if (($width == 1) & ($height == 1)) {
$imageuri = "http://images-jp.amazon.com/images/G/09/x-locale/detail/thumb-no-image.gif";
$width = 50;
$height = 60;
}
push(@table, "\n");
push(@table, "\n");
push(@table, "\n");
push(@table, "\n");
push(@table, "\n");
push(@table, "\n");
push(@table, " | \n");
push(@table, "\n");
push(@table, "\n");
push(@table, "$product\n");
push(@table, "\n");
push(@table, " | \n");
push(@table, "
\n");
push(@table, "\n");
push(@table, "\n");
if ($#author eq "-1") {
push(@table, " \n");
} else {
foreach $line (@author) {
my $uline = NKF::nkf("-E -w8", $line);
my $encauth = &uri_encode($uline);
my $searchuri = $search . $encauth;
push(@table, "$line \n");
}
}
push(@table, " | \n");
push(@table, "
\n");
push(@table, "\n");
push(@table, "\n");
if ($manu eq "") {
push(@table, " \n");
} else {
my $umanu = NKF::nkf("-E -w8", $manu);
my $encmanu = &uri_encode($umanu);
my $searchuri = $search . $encmanu;
push(@table, "$manu \n");
}
push(@table, " | \n");
push(@table, "
\n");
push(@table, "
\n");
print "Content-type: text/html\r\n\r\n";
print <<"ASIN1";
Amazon Web Service for TDS
Amazon Web Service for TDS
ASIN2
print "@table";
print "
\n";
print "\n";
print "
\n";
print "\n";
print "\n\n";
return 0;
}
sub uri_encode($) {
my $str = shift;
$str =~ s/([^\w])/'%'.unpack('H2', $1)/eg;
# $str =~ s/([^\w ])/'%'.unpack('H2', $1)/eg;
# $str =~ tr/ /+/;
return $str;
}
sub uri_decode($) {
my $str = shift;
$str =~ tr/+/ /;
$str =~ s/%([0-9A-Fa-f][0-9A-Fa-f])/pack('H2', $1)/eg;
return $str;
}