## mtawsasin.pl
## 2004.05.17 Hisaaki Shibata <shibata@luky.org>
## 2006.09.20 Hisaaki Shibata <shibata@luky.org> unuse Jcode,use Encode

## This is from Amazon.pm by Negi<Negi@KU3G.org>
## Ref: http://www.ku3g.org/negi/diary/?20040428#200404281

#package MT::Plugin::AwsAsin;

#use strict;
use Encode;
use MT::Template::Context;
use SOAP::Lite;
$SOAP::Constants::DO_NOT_USE_CHARSET=1;
#use Jcode;
use DB_File;
use Fcntl;

MT::Template::Context->add_tag(AwsAsin => \&aws_asin);

use vars qw($AMAZON_TAG $AMAZON_DEVTOKEN $WSDL $CACHE_DIR %data %img_url);

# You should change variable from here.
$AMAZON_TAG ||= 'hogehoge';
$AMAZON_DEVTOKEN ||= 'HOGEHOGE';
$WSDL ||= "http://soap.amazon.co.jp/schemas3/AmazonWebServices.wsdl";
$CACHE_DIR ||= "/var/www/blog/cache";
$img_url{"Book"} = "http://images-jp.amazon.com/images/G/09/icons/books/comingsoon_books.gif";
$img_url{"DVD"} = "http://images-jp.amazon.com/images/G/09/icons/dvd/comingsoon_dvd.gif";
$img_url{"Music"}  = "http://images-jp.amazon.com/images/G/09/icons/music/comingsoon_music.gif";
# You should change variable until here.

%data = ();

#main routine of this plugin
sub aws_asin{
    my $ctx = shift;
    my $args = shift;

    (defined($args->{asin})) || return "MTAwsAsin: asin not defined.\n\nUsage:<MTAwsAsin asin=\"B0000DCVE2\">";
    my $asin = $args->{asin};
    $s = get($asin);

    my $medium_img = $data{medium_img};
    if(defined($args->{img})){
    	$medium_img = $args->{img};
    }else{
    	#start Fixme: Too much Internet access is needed.
    	#The image file should be cached.
    	if(`identify $data{medium_img} 2>&1|grep -E "1x1|missing"`){
    		my $try_med_img = $data{medium_img};
		$try_med_img =~ tr/09.M/01.M/;
		$medium_img = $try_med_img;
        	if(`identify $try_med_img 2>&1|grep "1x1|missing"`){
	    		$medium_img = $img_url{$data{catalog}};
		}
    	}
    	#end Fixme
    }
    #You should change output HTML with following here-document.
    return <<EOT;
<div id=awsasin>
<table width="90%">
<tr> 
 <td> <a href="$data{url}">
  <img src="$medium_img" border="0" alt="cover">
  </a>
 </td>
 <td>
  【題名】<a href="$data{url}"><strong>$data{title}</strong>$data{keyphrases}$data{features}</a><br>
  【作者】 $data{authors}$data{artists}<br>
  【制作】 $data{manufacture}$data{distributor}   　【価格】 $data{price}<br>
  【発売】 $data{release} 　　【ASIN】 $data{asin}<br>
 </td>
</tr>
<tr> </tr>
</table>
</div>
EOT
}

#get book specs from the cache DB file.
#if cache is not present, query with SOAP.
sub get{
    my $asin = shift;
#    my %data = ();
    $asin =~ tr/\-//d;
    if( -s "$CACHE_DIR/$asin"){
	tie %data, DB_File, "$CACHE_DIR/$asin", O_RDONLY, 0644, $DB_HASH;
	return \%data;
    }else{
	my $text;
	my $res = query($asin);

	tie %data, DB_File, "$CACHE_DIR/$asin", O_RDWR|O_CREAT, 0644, $DB_HASH;
	$data{title}
		= encode("euc-jp",$res->{Details}->[0]->{ProductName});
	$data{keyphrases}
		= encode("euc-jp",$res->{Details}->[0]->{KeyPhrases});
	$data{features}
		= encode("euc-jp",$res->{Details}->[0]->{Features});
	$data{medium_img}
		= $res->{Details}->[0]->{ImageUrlMedium};
	$data{large_img}
		= $res->{Details}->[0]->{ImageUrlLarge};
	$data{small_img}
		= $res->{Details}->[0]->{ImageUrlSmall};
	$data{url}
		= $res->{Details}->[0]->{Url};
	my @authors = map {encode("euc-jp",$_)}
		@{$res->{Details}->[0]->{Authors}};
	$data{authors} = join(",", @authors);
	my @artists = map {encode("euc-jp",$_)}
		@{$res->{Details}->[0]->{Artists}};
	$data{artists} = join(",", @artists);
	$data{manufacture}
		= encode("euc-jp",$res->{Details}->[0]->{Manufacturer});
	$data{distributor}
		= encode("euc-jp",$res->{Details}->[0]->{Distributor});
	$data{price}
	        = encode("euc-jp",$res->{Details}->[0]->{OurPrice});
	$data{release}
	        = $res->{Details}->[0]->{ReleaseDate};
	$data{asin}
	        = $res->{Details}->[0]->{Asin};
	$data{media}
	        = $res->{Details}->[0]->{Media};
	$data{catalog}
	        = $res->{Details}->[0]->{Catalog};

	untie %data;
	tie %data, DB_File, "$CACHE_DIR/$asin", O_RDONLY, 0644, $DB_HASH;

	open(LOG, ">> $CACHE_DIR/log");
	print LOG $res,"\n",%data,"\n\n";
	close(LOG);

	return \%data;
    }
}


#quote book specs with SOAP via AWS
sub query{
    my $asin = shift;
    my $s = SOAP::Lite->service($WSDL);
    my $res = $s ->
	AsinSearchRequest(SOAP::Data->name('AsinSearchRequest')
	  ->type('AsinRequest')
		->value(\SOAP::Data->value(
		     SOAP::Data->name('asin'	=> $asin),
		     SOAP::Data->name('page'	=> 1),
		     SOAP::Data->name('tag'	=> $AMAZON_TAG),
		     SOAP::Data->name('type'	=> 'lite'),
		     SOAP::Data->name('devtag'	=> $AMAZON_DEVTOKEN),
		     SOAP::Data->name('locale'	=> 'jp'),
		     SOAP::Data->name('format'	=> 'xml'),
		     SOAP::Data->name('version'	=> '1.0'),
		)
	  )
	);
    return $res;
}
1;
__END__

=head1 NAME

mtaws.pl - Movable Type Plugin to make "Book Spec" HTML Table with Amazon Webservices via SOAP.

=head1 SYNOPUS

<MTAwsAsin asin="B0000UX55W">

=head1 ABSTRACT

This script is tested under MT-2.661.
Please place this script {$MT-dir}/plugin/ .

At first, you should do followings to keep book spec cache and log.
$ mkdir {$CACHE_DIR}
$ chmod 744 {$CACHE_DIR}
$ chown www-data:www-data {$CACHE_DIR}

It is better to make StyleSheet.css with <div awsasin>.

このPluginはAmazon Web ServiceのSOAPインタフェースを使って本、DVD、CDなどの
紹介情報を取り込み、HTML Table形式で出力するものです。
MT-2.661でテストを行っています。
MT-3.22jでもテストを行いました。/2006.09.20

本スクリプトを {$MT-dir}/plugin/ディレクトリに置いて下さい。
また、上記ソース上の変数をご自分の環境に合わせて修正して下さい。

AWS経由で取得した情報はBerkley DB形式で{$CACHE_DIR}に置くので、
初期設定時に
mkdir {$CACHE_DIR}
chmod 744 {$CACHE_DIR}
chown www-data:www-data {$CACHE_DIR}
して下さい。

スタイルシートで<div awsasin>の設定をすると、美しく出るでしょう。

=head1 REQUIRE

Perl modules;
	SOAP::Lite
	Encode
	#Jcode
	DB_File
	Fcntl

System commands;
	identify - from ImageMagick

=head1 FILES

{$CACHE_DIR}/ASIN
	"Book specs" cache files to reduce AWS access.
	Each file name is same with each ASIN Id.
	AWSのアクセスを減らすための商品仕様キャッシュファイル。
	ASIN Idと同じファイル名が商品毎に作成される。

{$CACHE_DIR}/log
	SOAP access log file.
	SOAPアクセス時のlog file

=head1 CUSTOM VARIABLES

下記変数をご自分の環境に合わせて修正下さい。

	$AMAZON_TAG ||= 'htthooeuqorg-22';

	$AMAZON_DEVTOKEN ||= 'D2UG79KH8I4U0T';

	$WSDL ||= "http://soap.amazon.co.jp/schemas3/AmazonWebServices.wsdl";

	$CACHE_DIR ||= "/var/www/blog/cache";

	$img_url{"Book"} = "http://www.uriuri.com/comingsoon_books.gif";

	$img_url{"DVD"} = "http://www.uriuri.com/comingsoon_dvd.gif";

	$img_url{"Music"}  = "http://www.uriuri.com/comingsoon_music.gif";

=head1 STYLE SHEET EXSAMPLE

#awsasin {
  color: #666;
  background-color: #fee;
  border-style: solid;
  border-width: 0px 3px 3px 0px;
  font-size: smaller;
}

=head1 LICENSE

Now not defined.
Please refer to http://hoop.euqset.org/prog/MTAwsAsin/index.html

=head1 AUTHOR

Masahito OHTSUKA <negi@ku3g.org>
Hisaaki SHIBATA <shibata@luky.org>

