#!/usr/bin/perl -w

# This program shows how to use LWP::Simple to make an Amazon Web
# Services (AWS) REST request, and how to process the response with
# XML::XPath.

use strict;
use MT::Template::Context;
use LWP::Simple qw($ua get);
#use LWP::Simple;
use XML::XPath;
use Encode;

MT::Template::Context->add_tag(Rest => \&rest);

# Retrieve command line arguments and combine them with escaped space characters.

sub rest{
#die "Usage: $0 asin=<ASIN>\n"
#    unless @ARGV;
#my $itemid = @ARGV;
my $ctx = shift;
my $args = shift;
my $itemid = $args->{asin};
my $medium_img = "";
my $image = "";
if(defined($args->{img})){
	$medium_img = $args->{img};
}
# Define parts of the REST request.
my $baseurl = "http://xml-jp.amznxslt.com/onca/xml";
my $service = "AWSECommerceService";
my $associatetag = "htthooeuqorg-22";
my $accesskey = "hogehoge";
my $operation = "ItemLookup";
my $idtype = "ASIN";
my $contenttype = "text/xml";
my $page = "1";
my $responsegroup = "ItemIds,ItemAttributes,Images,Offers";
my $version = "2006-03-08";

# Assemble the REST request URL.
my $request =
    "$baseurl?" .
    "Service=$service&" .
    "AssociateTag=$associatetag&" .
    "AWSAccessKeyId=$accesskey&" .
    "Operation=$operation&" .
    "IdType=$idtype&" .
    "ItemId=$itemid&" .
    "ContentType=$contenttype&" .
    "Page=$page&" .
    "ResponseGroup=$responsegroup&" .
	"Version=$version" ;

# Send the request using HTTP GET.
my $ua = new LWP::UserAgent;
$ua->timeout(30);
my $response = get($request);
#my $response = LWP::Simple->get($request);
# Process XML response with XPath.
my $xp = XML::XPath->new(xml => $response);

if ( $xp->find("//Error") )
{
    print "There was an error processing your request:\n", 
          "  Error code: ", $xp->findvalue("//Error/Code"), "\n",
          "  ", $xp->findvalue("//Error/Message"), "\n\n";
}
else
{
        my $title=encode("euc-jp",$xp->findvalue("//Title"));
        my $detail=$xp->findvalue("//DetailPageURL");
        if ($medium_img) {
		$image=$medium_img;
	}else{
#20070428	$image=$xp->findvalue("//MediumImage/URL");
		$image=$xp->findvalue("//Item/MediumImage/URL");
	}
        my $manufacture=encode("euc-jp",$xp->findvalue("//Manufacturer"));
        my $author=encode("euc-jp",$xp->findvalue("//Author").$xp->findvalue("//Artist").$xp->findvalue("//Actor"));
        my $price=encode("euc-jp",$xp->findvalue("//FormattedPrice"));
        my $release=encode("euc-jp",$xp->findvalue("//ReleaseDate").$xp->findvalue("//PublicationDate"));
        my $asin=$xp->findvalue("//ASIN");

    return <<EOT;
<div id=awsasin>
<table width="90%">
<tr>
 <td> <a href="$detail">
  <img src="$image" border="0" alt="$title">
  </a>
 </td>
 <td>
  ¡ÚÂêÌ¾¡Û<a href="$detail"><strong>$title</strong></a><br>
  ¡Úºî¼Ô¡Û$author<br>
  ¡ÚÀ©ºî¡Û$manufacture   ¡Ú²Á³Ê¡Û$price<br>
  ¡ÚÈ¯Çä¡Û$release ¡ÚASIN¡Û $asin<br>
 </td>
</tr>
<tr> </tr>
</table>
</div>
EOT

}
}
1;
__END__

