;# mt-bk1.pl ;# (C) 2004 KOUNO Takeshi ;# All Right Reserved. package MT::Plugin::bk1; ;#==================================================================== ;# ;# In the Templates, "Main Index", "Category Archive", ;# "Individual Entry Archive", "Date-Based Archive" and so on: ;# ;# <$MTEntryBody bk1="1"$> ;# ;# And you can write "bibid:02273467" or "ISBN:4106100037" in an Entry. ;# Please edit ;#==================================================================== ;# Replae HTML my $DISP_HTML =<<'__EOF__';
bk1 $ProductName
$Authors
$Manufacturer / $OurPrice / $DateOfIssue
$Basket
__EOF__ ;# For use cache my $CACHE_DIR = "./bk1_cache"; # /path/to/bk1_cache my $EXPIRE = 24; # hour(s) ;#==================================================================== use strict; use MT::Template::Context; use LWP::UserAgent; use XML::Simple; use Jcode; use vars qw($VERSION); my $VERSION = '1.3'; MT::Template::Context->add_global_filter(bk1 => sub { my $s = shift; my $AID = shift; if ( $AID ){ $s =~ s/(bibid:)(\d{8})/to_html($1,$2,$AID)/ge; $s =~ s/(ISBN:)([0-9-]{9,12}[\d|Xx])/to_html($1,$2,$AID)/ge; } $s; } ); sub to_html { my $text = shift; my $id = shift; my $AID = shift; my $url = "http://cgi.bk1.jp/xml.cgi?bibid=$id&aid=$AID"; my $cache = "$CACHE_DIR/$AID.$id.xml"; if(!-e $CACHE_DIR){ mkdir $CACHE_DIR, 0777 or die "mkdir missed:$!"; } ;# Get XML my $r; unless (-f $cache && -M _ < ($EXPIRE / 24)) { my $ua = new LWP::UserAgent; $ua->agent("mt-bk1"); $ua->timeout(10); $r = $ua->mirror($url, $cache); return $text.$id.$r->status_line if $r->is_error; } my $ProductInfo; $ProductInfo = XMLin($cache); my($ProductName,$SeriesName,$Authors,$Manufacturer,$bibid,$ISBN,$Availability,$ImageUrlSmall,$ImageUrlLarge,$OurPrice,$DateOfIssue,$DetailURL); $ProductName = $ProductInfo->{Details}->{ProductName}; if (ref($ProductName) eq 'HASH') { $ProductName = ""; } $SeriesName = $ProductInfo->{Details}->{SeriesName}; if (ref($SeriesName) eq 'HASH') { $SeriesName = ""; } $Authors = $ProductInfo->{Details}->{Authors}; if (ref($Authors) eq 'HASH') { $Authors = ""; } $Manufacturer = $ProductInfo->{Details}->{Manufacturer}; if (ref($Manufacturer) eq 'HASH') { $Manufacturer = ""; } $ISBN = $ProductInfo->{Details}->{ISBN}; if (ref($ISBN) eq 'HASH') { $ISBN = ""; } $bibid = $ProductInfo->{Details}->{BIBID}; if (ref($bibid) eq 'HASH') { $bibid = ""; } $Availability = $ProductInfo->{Details}->{Availability}; if (ref($Availability) eq 'HASH') { $Availability = ""; } $ImageUrlSmall = $ProductInfo->{Details}->{ImageUrlSmall}; if (ref($ImageUrlSmall) eq 'HASH') { $ImageUrlSmall = ""; } $OurPrice = $ProductInfo->{Details}->{OurPrice}; if (ref($OurPrice) eq 'HASH') { $OurPrice = ""; } $DateOfIssue = $ProductInfo->{Details}->{DateOfIssue}; if (ref($DateOfIssue) eq 'HASH') { $DateOfIssue = ""; } $DetailURL = $ProductInfo->{Details}->{url}; if (ref($DetailURL) eq 'HASH') { $DetailURL = ""; } $DetailURL =~ s/%26/&/g; ;# Basket my $BasketImage = 'http://www.bk1.co.jp/images/bk1/ic-kago.gif'; my $Basket = ""; ;# Replace my $html = $DISP_HTML; $html =~ s/\$bibid/$bibid/g if $html =~ /\$bibid/; $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/\$OurPrice/$OurPrice/g if $html =~ /\$OurPrice/; $html =~ s/\$DateOfIssue/$DateOfIssue/g if $html =~ /\$DateOfIssue/; $html =~ s/\$DetailURL/$DetailURL/g if $html =~ /\$DetailURL/; $html =~ s/\$Basket/$Basket/g if $html =~ /\$Basket/; ;# 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'; $html = Jcode->new($html, "utf8")->$charset(); return $html; } 1; __END__ ;#==================================================================== ;# Changelog ;#==================================================================== ;# 2004/2/11 10.26.00 ;# 1.30 +UTF8 ;# http://lacoste.sub.jp/blog/archives/000015.php ;# 2004/2/08 23.34.00 ;# 1.20 +Modify XHTML1.0 ;# http://lacoste.sub.jp/blog/archives/000012.php ;# 2004/1/25 17.49.00 ;# 1.10 +In case of null, mkdir ;# ;# 2004/1/4 20.59.00 ;# 1.00 +ISBN ;# http://takeshi.sub.jp/blog/archives/000090.html ;# 2004/1/1 15:50:58 ;# 0.95 +Cache ;# http://www.asmate.net/weblog/archives/000056.html ;# 2004/1/1 14:06:43 ;# 0.90 Released ;# http://takeshi.sub.jp/blog/archives/000077.html ;# ;#==================================================================== ;# Special thanx to... ;#==================================================================== ;# Yutaka/@smate.NET ;# Daiji Hirata ;# lacoste