#$Id$
#2003.2.14 一歩修正
=begin
(amz:4-06-182231-4) みたいな (amz:isbn) という形式の文字列があると
amazon から書誌データやら書影やらを引っ張ってきて置き換えます。
表示項目を変えたい場合はスクリプトを適当にいじくってください。:-)
ndiary.conf の LOG_DIRECTORY で指定したディレクトリに amzcache というディレクトリを
作っておくと取得したデータをキャッシュしておくようになります。
同様にキャッシュの有効期限は AMAZON_EXPIRE で日数単位で指定してください。
また、
PROXY = 'proxy.hoge.com:8080'
と指定しておくと、情報取得時プロクシを経由するようになります。
# review-link(書評リンク)
ndiary.conf に以下の様な設定を加えるか、180行目ぐらいのとこに直接設定。
REVIEWER_CODE = '一歩'
REVIEWLINK_LINK = true #書店リンクのうちに書評リンクも含める
=end
class Module
def attr_my_accessor( name )
module_eval %-
def #{name.id2name}()
get_data
@#{name.id2name}
end
-
end
end
class Amazon
require 'net/http'
attr_accessor :isbn
attr_my_accessor :title
attr_my_accessor :author
attr_my_accessor :publisher
attr_my_accessor :size
attr_my_accessor :pubdate
attr_my_accessor :price
attr_my_accessor :desc
attr_my_accessor :image_url
attr_my_accessor :image_width
attr_my_accessor :image_height
attr_reader :cache_dir
attr_accessor :expire
attr_accessor :proxy
def initialize
@expire = 7
@proxy = nil
end
def load_cache
if @flag or
(File::directory?(@cache_dir.to_s) and File::readable?(@cache_dir + @isbn) and
File::mtime(@cache_dir + @isbn) < Time::now.to_i - 3600 * 24 * @expire) then
return
end
@flag = true
read_cache
end
def read_cache
begin
open(@cache_dir + @isbn){ |f|
@title, @author, @publisher, @size, @pubdate, @price, @desc, @image_url,@image_width, @image_height = f.read.split(/\t/)
}
@image_url = @image_url.to_s.sub(%r"^http://images.amazon.com/", 'http://images-jp.amazon.com/')
rescue
@flag = nil
end
end
def save_cache
return unless File::directory?(@cache_dir.to_s)
begin
open(@cache_dir + @isbn, 'w'){ |f|
f.write [ @title, @author, @publisher, @size, @pubdate, @price, @desc, @image_url,@image_width, @image_height ].join("\t")
}
rescue
#
end
end
def httpconnect
if @proxy then
proxy, port = @proxy.split(':')
port = port.to_i
else
proxy = port = nil
end
Net::HTTP::Proxy(proxy, port).new( 'www.amazon.co.jp', 80 )
end
def cache_dir=(dir)
dir << '/' unless dir[-1,1] == '/'
@cache_dir = dir
end
def large_image_url
get_data
@image_url.sub('MZZZZZZZ','LZZZZZZZ')
end
def get_data
load_cache
return if @flag
isbn = @isbn.delete('-')
path = "/exec/obidos/ASIN/#{isbn}"
http = httpconnect
response , = http.get2(path)
if response.code == '302' then
path = response.header['location'].sub('http://www.amazon.co.jp', '')
response , = http.get2(path)
end
if %r|
(.+)| =~ response.body then
@author = $1.gsub(/<[^>]+>/, '')
end
if %r|^Amazon.co.jp:(.+?)$| =~ response.body then
@title = $1.strip
end
if %r|価格:(¥[\d,]+)
| =~ response.body then
@price = $1
end
if %r|^\(([\d\/]+)\)| =~ response.body then
@pubdate = $1[0..6]
end
if %r"(?:内容(「.+?」データベースより)|出版社/著者からの内容紹介)
+(.+?) *
" =~ response.body then
@desc = $1.gsub(/<\/?([!\w]+)[^>]*>/i, '')
end
if %r|サイズ\(cm\): (\d+)| =~ response.body then
@size = $1
@size << " #{$2}p" unless $2.nil?
end
if %r|^([^>]+)\n ; \nISBN|m =~ response.body then
@publisher = $1
end
if @title.nil? then
read_cache
else
save_cache
@flag = true
end
end
end
class Filter
def amazon(str, type)
reviewer_code = @diary.config['REVIEWER_CODE'].to_s
flag_reviewlinklink = @diary.config['REVIEWLINK_LINK']
flag_rl_url = @diary.config['PARAGRAPH_ANCHOR']
reviewlink_url = 'http://ippo.s5.xrea.com/x/rl/index.cgi'
=begin
#ndiary.confを触るのが好みでない場合、ここで直接定義して下さい。
reviewer_code = '一歩'
flag_reviewlinklink = true
=end
case type
when :P, :UL, :DL
str.gsub!(/[\((]amz:(\d-[\d-]{9}-\w)[\))]/){
isbn = $1
rl_str = []
rl_str << "ISBN#{isbn}"
rl_str << "reviewer=#{reviewer_code}" unless reviewer_code.empty?
rl_str = ''
amz = Amazon::new
amz.cache_dir = @diary.logDirectory + 'amzcache'
amz.isbn = isbn
if @diary.respond_to?('config') then
amz.expire = @diary.config['AMAZON_EXPIRE'] || 7 # キャッシュの有効日数
amz.proxy = @diary.config['PROXY']
end
body = desc = img = ''
unless amz.title.nil?
unless amz.image_url.nil? then
img = ""
end
desc = "
『#{amz.desc}』" unless amz.desc.to_s.empty?
body = "#{img}「#{amz.title}」 #{amz.author}
サイズ: #{amz.size}cm 発行: #{amz.pubdate} 価格: #{amz.price}
ISBN#{amz.isbn}#{desc}
"
body << rl_str
body << "【 rl 】" if flag_reviewlinklink
end
body
}
when :AFTER_P, :AFTER_UL, :AFTER_DL
if flag_rl_url
str.gsub!(//) {
tagcode = $1
if str =~ //
urlcode = $1
tagcode += ':url=' + urlcode
end
t = ""
}
end
end
end
end