;# 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__';
__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 = "