メイン

mtawsasin.pl 暫定公開

下記のようなものが出力できるMTのPluginを暫定公開します。

cover 【題名】 アライヴIV~地獄の交響曲DVD
【作者】
【制作】 BMGファンハウス  【価格】 ¥ 5,880
【発売】 2004/04/21   【ASIN】 B0001M6IGC

小汚い部分があるので、腕に覚えがある方は、手直しして送って下さい。
ダウンロード後、mv mtawsasin mtawsasin.plして、然るべきところに置いて下さい。
mtawsasin

## mtawsasin.pl
## 2004.05.10 Hisaaki Shibata 

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

#package MT::Plugin::AwsAsin;

#use strict;
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 ||= 'htthooeuqorg-22';
$AMAZON_DEVTOKEN ||= 'D2UG79KH8I4U0T';
$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:";

    my $asin = $args->{asin};
    $s = get($asin);
    my $medium_img = $data{medium_img};

    #start Fixme: Too much Internet access is needed.
    #The image file should be cached.
    if(`identify $data{medium_img}|grep '1x1'`){
    	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|grep '1x1'`){
	    $medium_img = $img_url{"Music"};
	    $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>
  【題名】<strong> <a href="$data{url}">$data{title}</a></strong><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}
		= jcode($res->{Details}->[0]->{ProductName},'utf8')->euc;
	$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 {jcode($_,'utf8')->euc}
		@{$res->{Details}->[0]->{Authors}};
	$data{authors} = join(",", @authors);
	my @artists = map {jcode($_,'utf8')->euc}
		@{$res->{Details}->[0]->{Artists}};
	$data{artists} = join(",", @artists);
	$data{manufacture}
		= jcode($res->{Details}->[0]->{Manufacturer},'utf8')->euc;
	$data{distributor}
		= jcode($res->{Details}->[0]->{Distributor},'utf8')->euc;
	$data{price}
	        = jcode($res->{Details}->[0]->{OurPrice}, 'utf8')->euc;
	$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



=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 
. このPluginはAmazon Web ServiceのSOAPインタフェースを使って本、DVD、CDなどの 紹介情報を取り込み、HTML Table形式で出力するものです。 MT-2.661でテストを行っています。 本スクリプトを {$MT-dir}/plugin/ディレクトリに置いて下さい。 また、上記ソース上の変数をご自分の環境に合わせて修正して下さい。 AWS経由で取得した情報はBerkley DB形式で{$CACHE_DIR}に置くので、 初期設定時に mkdir {$CACHE_DIR} chmod 744 {$CACHE_DIR} chown www-data:www-data {$CACHE_DIR} して下さい。 スタイルシートで
の設定をすると、美しく出るでしょう。 =head1 REQUIRE Perl modules; SOAP::Lite 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 Hisaaki SHIBATA