# Blosxom Plugin: bk1
# Author(s): Fukazawa Tsuyoshi
# Version: 2005-04-23
# Blosxom Home/Docs/Licensing: http://www.blosxom.com/
package bk1;
use strict;
use LWP::UserAgent;
use Jcode;
use FileHandle;
use vars qw($VERSION);
# --- Configurable variables -----------
;# Breeder ID
my $AID = '(あなたのブリーダーID)';
;# Replae HTML
my $DISP_HTML =<<'__EOF__';
$ProductName
$Authors
$Manufacturer / $TotalPrice ($DateOfIssue)
発送可能時間:$Availability
__EOF__
# --- Plug-in package variables --------
# For use cache
my $CACHE_DIR = "$blosxom::plugin_state_dir/bk1_cache";
my $EXPIRE = 24; # hour(s)
my $VERSION = '1.0';
my $fh = new FileHandle;
#====================================================================
sub start {
if (!-e $CACHE_DIR) {
my $mkdir_r = mkdir($CACHE_DIR, 0755);
warn $mkdir_r
? "blosxom : bk1 plugin > \$CACHE_DIR, $CACHE_DIR, created.\n"
: "blosxom : bk1 plugin > mkdir missed:$!";
$mkdir_r or return 0;
}
1;
}
sub story {
my ($pkg, $path, $filename, $story_ref, $title_ref, $body_ref) = @_;
$$body_ref =~ s/bibid:(\d{7,8})/to_html($1)/ge;
1;
}
sub to_html {
my $bibid = substr("0" . shift, -8, 8); # 05/04/23: 7バイトのbibidの頭に0を付ける
my $url = "http://cgi.bk1.jp/xml.cgi?bibid=$bibid&aid=$AID";
my $cache = "$CACHE_DIR/$bibid.xml";
;# Get XML
my $rtn;
unless (-f $cache && -M _ < ($EXPIRE / 24)) {
my $ua = new LWP::UserAgent;
$ua->agent("mt-bk1");
$ua->timeout(10);
$rtn = $ua->mirror($url, $cache);
return $bibid.$rtn->status_line if $rtn->is_error;
}
$fh->open("< $cache");
my @buf = <$fh>;
$fh->close();
my $buf = join('', @buf);
my($ProductName,$SeriesName,$Authors,$Manufacturer,$ISBN,$Availability,$ImageUrlSmall,$ImageUrlLarge,$OurPrice,$DateOfIssue,$DetailURL,$TotalPrice,$rBibid);
# 05/04/23: Bibidも取り込む
if($buf =~ /(\d{8})<\/BIBID>/){
$rBibid = $1;
}
if($buf =~ /([^<]*)<\/ProductName>/){
$ProductName = $1;
}
if($buf =~ /([^<]*)<\/SeriesName>/){
$SeriesName = $1;
}
if($buf =~ /([^<]*)<\/Authors>/){
$Authors = $1;
}
if($buf =~ /([^<]*)<\/Manufacturer>/){
$Manufacturer = $1;
}
if($buf =~ /([^<]*)<\/ISBN>/){
$ISBN = $1;
}
if($buf =~ /([^<]*)<\/Availability>/){
$Availability = $1;
}
if($buf =~ /([^<]*)<\/ImageUrlSmall>/){
$ImageUrlSmall = $1;
}
if($buf =~ /([^<]*)<\/ImageUrlLarge>/){
$ImageUrlLarge = $1;
}
if($buf =~ /([^<]*)<\/TotalPrice>/){
$TotalPrice = $1;
}
if($buf =~ /([^<]*)<\/OurPrice>/){
$OurPrice = $1;
}
if($buf =~ /([^<]*)<\/DateOfIssue>/){
$DateOfIssue = $1;
}
# if($buf =~ m//){
# $DetailURL = $1;
# $DetailURL =~ s/%26/&/g;
# }
# DetailURLが旧版のままなので念のため修正
$DetailURL = "http://www.bk1.co.jp/product/$rBibid/$AID";
;# Basket
my $BasketImage = 'http://www.bk1.co.jp/images/bk1/ic-kago.gif';
my $Basket = "
";
;# Replace
my $html = $DISP_HTML;
$html =~ s/\$bibid/$rBibid/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/\$ImageUrlLarge/$ImageUrlLarge/g if $html =~ /\$ImageUrlLarge/;
$html =~ s/\$OurPrice/$OurPrice/g if $html =~ /\$OurPrice/;
$html =~ s/\$TotalPrice/$TotalPrice/g if $html =~ /\$TotalPrice/;
$html =~ s/\$DateOfIssue/$DateOfIssue/g if $html =~ /\$DateOfIssue/;
$html =~ s/\$DetailURL/$DetailURL/g if $html =~ /\$DetailURL/;
$html =~ s/\$Basket/$Basket/g if $html =~ /\$Basket/;
return $html;
}
1;
__END__
# このプラグインは、smashmedia(http://takeshi.sub.jp/blog/)で公開されているMovable Type用
# bk1プラグイン mt-bk1.plの正規表現バージョンを、blosxom 2.0用に移植したものです。
# 素晴らしいツールを制作して下さった河野さんに感謝。
# 05/04/23:
# 商品へのURLをリニューアル対応版に修正
# bibidの頭に0を付けなくても受け付けるように修正
# ISBNは受け付けないように修正