# mt-amazon.pl # (C) 2005,2006 Katsuhiro Konishi # All Right Reserved. # This software is provided as-is. You may use it for commercial or # personal use. If you distribute it, please keep this notice intact. package MT::Plugin::amazon; #==================================================================== # # In the Templates, "Main Index", "Category Archive", # "Individual Entry Archive", "Date-Based Archive" and so on: # # <$MTEntryBody amazon="1"$> # # And you can write "ISBN:4106100037" or "ASIN:B00076QHQ8" in an Entry. # Please edit #==================================================================== # associates ID # You can get your associates ID from http://www.amazon.co.jp/associates/ my $AID = 'fulufurufiary-22'; # # Developer Token # You can get your Developer Token from http://www.amazon.co.jp/webservices/ my $TOKEN = 'D14I5B8E9D2EE7'; # # Template HTML # my $DISP_HTML =<<'__EOF__';
$ProductName
$Authors
$Manufacturer / $ListPrice / $DateOfIssue

__EOF__ # DO DONT edit under this line #==================================================================== use strict; use MT::Template::Context; use Net::Amazon; use Jcode; use vars qw($VERSION); my $VERSION = '0.02'; if (MT->can('add_plugin')) { eval { require MT::Plugin; my $plugin = new MT::Plugin({ name => 'mt-Amazon Plugin', description => 'ISBN Filter For Amazon', doc_link => 'http://fulufuru.haun.org/mt/log/000648.html', version => $VERSION }); MT->add_plugin($plugin); }; } MT::Template::Context->add_global_filter(amazon => sub { my($s, $code, $ctx) = @_; if ( $] > 5.007 ){ utf8::encode($s) if utf8::is_utf8($s) ; } $s =~ s/ISBN:\s*([0-9-]{9,12}[\d|Xx])/&to_html($1)/ige; $s =~ s/ASIN:\s*([0-9a-z]{10})/&to_html($1)/ige; #utf8::decode($s) if ! utf8::is_utf8($s) ; return $s; } ); sub to_html { my $isbn = shift; my ($ASIN,$ProductName,$SeriesName,$Authors,$Manufacturer,$ISBN,$Availability,$ImageUrlSmall,$ImageUrlMedium,$ImageUrlLarge,$OurPrice,$ListPrice,$DateOfIssue,$DetailURL,$TotalPrice); $isbn =~ s/-//g; my $ua = Net::Amazon->new( token => $TOKEN, affiliate_id => $AID, locale => "jp", ); my $response = $ua->search( asin => "$isbn"); if ( $response->is_success() ){ for ($response->properties()){ my $catalog = $_->Catalog() ; if ( $catalog eq "Book" ){ $ProductName = $_->ProductName(); $ISBN = $_->isbn(); $Authors = join(",",$_->authors()); } elsif ( $catalog eq "DVD" ){ $ProductName = $_->title(); $Authors = join(",",$_->starring()); } elsif ( $catalog eq "Music" ){ $Authors = join(",",$_->artists()); $ProductName = $_->ProductName(); } else { # $Authors = join(",",$_->authors()); $ProductName = $_->ProductName(); } $ASIN = $_->Asin(); $Manufacturer = $_->Manufacturer(); $DateOfIssue = $_->ReleaseDate(); $ImageUrlSmall = $_->ImageUrlSmall(); $ImageUrlMedium = $_->ImageUrlMedium(); $ImageUrlLarge = $_->ImageUrlLarge(); $OurPrice = $_->OurPrice(); $ListPrice = $_->ListPrice(); $DetailURL = $_->url(); # $DetailURL = "http://www.amazon.co.jp/exec/obidos/redirect?path=ASIN/$ASIN&tag=$AID&link_code=as2&camp=247&creative=1211"; # $DetailURL = "http://www.amazon.co.jp/exec/obidos/$ASIN/$AID # $DetailURL = "http://www.amazon.co.jp/exec/obidos/$ASIN/$AID/ref=nosim } }else{ $ISBN = $isbn; $ProductName = "Sorry No Data on Amazon.co.jp"; } if ( $] > 5.007 ){ if ( utf8::is_utf8($ProductName) ){ utf8::encode($ProductName); utf8::encode($ImageUrlSmall); utf8::encode($DetailURL); utf8::encode($Authors); utf8::encode($Manufacturer); utf8::encode($ListPrice); } }elsif ( 5.006 <= $] && $] < 5.007 ){ $ProductName = eval "no utf8; \$ProductName =~ /^(.*)\$/; \$1"; $ImageUrlSmall = eval "no utf8; \$ImageUrlSmall =~ /^(.*)\$/; \$1"; $DetailURL = eval "no utf8; \$DetailURL =~ /^(.*)\$/; \$1"; $Authors = eval "no utf8; \$Authors =~ /^(.*)\$/; \$1"; $Manufacturer = eval "no utf8; \$Manufacturer =~ /^(.*)\$/; \$1"; $ListPrice = eval "no utf8; \$ListPrice =~ /^(.*)\$/; \$1"; } ;# Replace my $html = $DISP_HTML; if ( $] > 5.007 ){ if ( utf8::is_utf8($html) ){ utf8::encode( $html ); } } $html =~ s/\$ProductName/$ProductName/g if $html =~ /\$ProductName/; $html =~ s/\$Authors/$Authors/g if $html =~ /\$Authors/; $html =~ s/\$Manufacturer/$Manufacturer/g if $html =~ /\$Manufacturer/; $html =~ s/\$ISBN/$ISBN/g if $html =~ /\$ISBN/; $html =~ s/\$Availability/$Availability/g if $html =~ /\$Availability/; $html =~ s/\$ImageUrlSmall/$ImageUrlSmall/g if $html =~ /\$ImageUrlSmall/; $html =~ s/\$ImageUrlMedium/$ImageUrlMedium/g if $html =~ /\$ImageUrlMedium/; $html =~ s/\$ImageUrlLarge/$ImageUrlLarge/g if $html =~ /\$ImageUrlLarge/; $html =~ s/\$OurPrice/$OurPrice/g if $html =~ /\$OurPrice/; $html =~ s/\$ListPrice/$ListPrice/g if $html =~ /\$ListPrice/; $html =~ s/\$DateOfIssue/$DateOfIssue/g if $html =~ /\$DateOfIssue/; $html =~ s/\$DetailURL/$DetailURL/g if $html =~ /\$DetailURL/; ;# Convert use MT::ConfigMgr; my $cfg = MT::ConfigMgr->instance; my $charset = {'Shift_JIS'=>'sjis','ISO-2022-JP'=>'jis','EUC-JP'=> 'euc', 'UTF-8'=>'utf8'}->{$cfg->PublishCharset} || 'utf8'; if ( $charset != 'utf8') { $html = Jcode->new($html, "utf8")->$charset(); } return $html; } 1; __END__