=begin
(user/plugin/)booklist.rb
nDiary Plug-in 画像リストの作成
.imageファイルにある画像の(標準では月毎の)リストを作成します。
※書籍(ISBN持ち)のみ
※凝った事をする時はbookstore_x系の設定とlibが必須
option:
total=on 全.imageファイルをリストします。
size=n 最近話題にしたものからn冊までをリストします。
format='...' 'moriyama'で森山さん風、'amazon'でAmazonランキング風
imagesize=m 画像の最大サイズ指定、ndiary.confよりこちら優先
config:
ndiary.conf等内
BOOKSTORE_IMAGESIZE = 70 #画像の最大サイズ指定
#他はuser/filter/bookstore_xを参照して下さい
format:
.imageファイルは1行1件、TAB区切り
date, anchor, image_url, image_width, image_height, etc(ISBN)
8桁日付, 日記アンカー, 画像ファイル, その横, と縦, 他(ISBN等)
以上で十分だと思うのですが。
note:
そういう訳で、表示形式等は適時改造して下さい。
exsample:
reference:
森山さんの日記 http://www.moriyama.com/diary/2003/diary.htm
Amazonのランキング表示 http://www.amazon.co.jp
bk1のランキング表示 → 書影データを自由に使えないので保留
update:
2003.11.28-2003.11.29
2004.01.07-2004.01.07 新キャッシュに対応
=end
def plug(opt)
list_size = nil
list_size = opt['size'].to_i if opt.has_key?('size')
format = 'standard'
format = opt['format'] if opt.has_key?('format')
image_size = nil
if @diary.respond_to?('config') then
image_size = @diary.config['BOOKSTORE_IMAGESIZE'].to_i unless @diary.config['BOOKSTORE_IMAGESIZE'].nil?
@bk1_id = @diary.config['BOOKSTORE_BK1_ID'].to_s #'p-ippo57167'
@amazon_id = @diary.config['BOOKSTORE_AMAZON_ID'].to_s #'ipposjunkbox-22'
@rakuten_id = @diary.config['BOOKSTORE_RAKUTEN_ID'].to_s
@no_image_file = @diary.config['NO_IMAGE_FILE'].to_s
@flag_net = @diary.config['nonet'] ? false : true
@expire = @diary.config['CACHE_EXPIRE']
@cache_base_dir = @diary.config['CACHE_DIRECTORY'].to_s
else
@bk1_id, @amazon_id, @rakuten_id = '', '', ''
end
@no_image_file = nil if @no_image_file.empty?
@expire = 12 if @expire.nil?
@cache_base_dir = @diary.logDirectory.chomp('/') + '/bookcache' if @cache_base_dir.to_s == ''
@cache_base_dir.chomp!('/')
image_size = opt['imagesize'].to_i if opt.has_key?('imagesize')
@flag_bk1_image = @diary.config['BOOKSTORE_BK1_IMAGE']
@url_bk1_image = @diary.config['BOOKSTORE_BK1_IMAGEURL']
@user_lib_dir = @diary.config['USERLIB_DIRECTORY'].to_s
@user_lib_dir = '../user/lib' if @user_lib_dir.to_s == ''
@user_lib_dir.chomp!('/')
@flag_cacheserver = @diary.config['CACHE_SERVER'] #true
#-----各位の環境に合わせて適時改変-----ココカラ-----
if format != 'standard'
require "#{@user_lib_dir}/cookie" #必須・webagent用
require "#{@user_lib_dir}/webagent" #必須・webagent用
require "#{@user_lib_dir}/cache" #必須・キャッシュ用
require "#{@user_lib_dir}/cachebib" #必須・キャッシュ用
require "#{@user_lib_dir}/amazon" #以下は各書店用・不要な書店分は削除可
require "#{@user_lib_dir}/bk1"
require "#{@user_lib_dir}/cache_ippo" if @flag_cacheserver
#贋書店(cache-server)への接続
require "#{@user_lib_dir}/cachebib_simple"
#未設定書店はbookstoreに準じさせる
@cache_dir = {} #書誌情報の保存フォルダの指定
@cache_dir['amazon'] = "#{@cache_base_dir}/amz"
@cache_dir['bk1'] = "#{@cache_base_dir}/bk1"
end
#-----各位の環境に合わせて適時改変-----ココマデ-----
filelist = []
if opt['total'] == 'on' || !list_size.nil?
#@logDirectory直下だけ探せばいいので、修正した方が効率がいいかも
Dir::find(@logDirectory){ |file|
next unless /\/\d{6}\.image$/i =~ file
filelist << file
}
else
if self.kind_of?(LastDiary) or self.kind_of?(Topics)
thismonth = @diaries[0][0..5]
else #if self.kind_of?(PastDiary)
thismonth = @targetMonth
end
filelist << @logDirectory + thismonth + '.image'
end
list = []
filelist.sort.reverse.each{ |file|
lines = File.exists?(file) ? open(file).readlines : []
# lines.sort.reverse.each{ |line|
lines.reverse.each{ |line|
#データの書式
#date, anchor, url, width, height, etc = line.split("\t")
data = line.chomp.split("\t")
next if data[5] !~ /^ISBN/ && data[5] !~ /^BOOK/ #書籍(ISBN)のみの抽出
list << data if list_size.nil? || list_size > list.size
}
}
list.reverse!
str = []
list.each{ |line|
date, anchor, url, width, height, etc = line
isbn, shop = etc, nil
if isbn =~ /^ISBN(.+)$/
isbn = $1.strip
elsif isbn =~ /^BOOK:(.+?):(.+)$/
shop, isbn = $1.strip, $2.strip
end
unless image_size.nil?
width, height = width.to_i, height.to_i
l = 1
max = width > height ? width : height
while (max / l) > image_size
l += 1
end
width, height = width / l, height / l
end
shops = [] #-----書店にネット接続の準備-----
case format
when 'ippo'
shops = ['amazon', 'bk1']
when 'moriyama'
shops = ['amazon', 'bk1']
when 'amazon'
shops = ['amazon']
end
cachebibs, downloadlist = [], [] #-----全書店分cachebibの作成-----
shops.each_index{ |i|
bookstore = shops[i]
book = nil
begin
case bookstore
when 'amazon'
book = CacheBib_amazon.new()
book.affiliate_code = @amazon_id unless @amazon_id.empty?
when 'bk1'
book = CacheBib_bk1.new()
book.affiliate_code = @bk1_id unless @bk1_id.empty?
book.flag_review = false #ここを入れないと重くなる(書評読)
unless @flag_bk1_image.nil?
book.flag_image = true
book.dir_image = @flag_bk1_image
book.url_image = @url_bk1_image unless @url_bk1_image.nil?
end
else
$stderr.puts "WAR; unknown-bookstore; code:#{bookstore}"
next
end
rescue Exception
$stderr.puts "WAR; define-error; code:#{bookstore}"
next
end
book.expire = @expire
flag_net = @flag_net
#ネットへの接続を制限するなら例えば以下の様に。
#flag_net = false if bookstore == 'amazon'
book.flag_net = flag_net
book.cache_dir = @cache_dir[bookstore]
if shop.nil?
book.set_cache_id(isbn)
else
book.set_cache_id(isbn, :id)
end
cachebibs << book
# downloadlist << i if book.expire_over?
}
case format
when 'ippo'
dat = []
amazon, bk1 = cachebibs
if shop.nil?
amazon.get_data(isbn)
bk1.get_data(isbn)
else
amazon.get_data_from_id(isbn) if shop == amazon.shop_id
bk1.get_data_from_id(isbn) if shop == bk1.shop_id
end
img = %Q||
img << %Q|
|
img << %Q||
dat << img
# img = ''
# img << %Q|amz| if shop.nil? || shop == amazon.shop_id
# img << ' '
# img << %Q|bk1| if shop.nil? || shop == bk1.shop_id
# dat << img
str << dat
when 'moriyama'
dat = []
amazon, bk1 = cachebibs
if shop.nil?
amazon.get_data(isbn)
bk1.get_data(isbn)
else
amazon.get_data_from_id(isbn) if shop == amazon.shop_id
bk1.get_data_from_id(isbn) if shop == bk1.shop_id
end
img = %Q||
img << %Q|
|
img << %Q||
dat << img
img = %Q||
img << %Q|bk1|
img << ' | '
img << %Q|amazon|
img << %Q||
dat << img
str << dat
when 'amazon'
amazon = cachebibs.first
if shop.nil?
amazon.get_data(isbn)
else
amazon.get_data_from_id(isbn) if shop == amazon.shop_id
end
img = %Q|
|
img << %Q| |
img << %Q||
img << %Q||
img << %Q|#{amazon.title} |
img << %Q|#{info["publisher"]} |
# ??? なんでこれだけamazon.publisherじゃ駄目なんだろう
img << %Q| |
|
str << img
else
img = %Q||
img << %Q|
|
img << %Q||
str << img
end
}
case format
when 'ippo'
line1, line2 = [], []
# str.each{ |up, down|
# line1 << up
# line2 << down
# }
str.each{ |up| line1 << up }
str = '' + "\n"
str << '| ' + line1.join("<\/td>\n | ") + ' | ' + "\n"
# str << '
' + "\n"
# str << '| ' + line2.join("<\/td>\n | ") + ' | ' + "\n"
str << '
' + "\n"
when 'moriyama'
line1, line2 = [], []
str.each{ |up, down|
line1 << up
line2 << down
}
str = '' + "\n"
str << '| ' + line1.join("<\/td>\n | ") + ' | ' + "\n"
str << '
' + "\n"
str << '| ' + line2.join("<\/td>\n | ") + ' | ' + "\n"
str << '
' + "\n"
when 'amazon'
str = '' + "\n" + str.reverse.join("\n") + "\n" + '
' + "\n"
else
str = '' + "\n" + str.join("\n") + "\n" + '
' + "\n"
end
return str
end