#$Id$ # 2003/11/10 微修正 =begin (amz:4-06-182231-4) みたいな (amz:isbn) という形式の文字列があると amazon から書誌データやら書影やらを引っ張ってきて置き換えます。 表示項目を変えたい場合はスクリプトを適当にいじくってください。:-) ndiary.conf の LOG_DIRECTORY で指定したディレクトリに amzcache というディレクトリを 作っておくと取得したデータをキャッシュしておくようになります。 同様にキャッシュの有効期限は AMAZON_EXPIRE で日数単位で指定してください。 また、 PROXY = 'proxy.hoge.com:8080' と指定しておくと、情報取得時プロクシを経由するようになります。 使用に関しては amazon の規約を確認の上自己責任でお願いします。 =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' class Book attr_accessor :isbn attr_accessor :title attr_accessor :author attr_accessor :publisher attr_accessor :size attr_accessor :pubdate attr_accessor :price attr_accessor :desc attr_accessor :image_url attr_accessor :image_width attr_accessor :image_height end 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 @flag = 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).to_i < 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/) } if @image_url then @image_url = @image_url.sub(%r"^http://images.amazon.com/", 'http://images-jp.amazon.com/') end 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}" path = "/exec/obidos/ASIN/#{isbn}?val=authorized" http = httpconnect response , = http.get2(path) if response.code == '302' then path = response.header['location'].sub(%r!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.sub(/\(\d+\)$/, '') end if %r|(¥[\d,]+)
| =~ response.body then @price = $1 end if %r|\((\d\d\d\d/\d\d(?:/\d\d)?)\)$| =~ response.body then @pubdate = $1 end if %r![^<]*内容[^<]*
\n([^\n]*)! =~ response.body then @desc = $1.gsub(/<\/?([!\w]+)[^>]*>/i, '') end if %r|サイズ\(cm\): ([\d x]+)| =~ response.body then @size = $1 @size << " #{$2}p" unless $2.nil? end if %r|^出版社: (.+)| =~ response.body then @publisher = $1 end if @title.nil? then read_cache else save_cache @flag = true end end end class Filter @flag_imagefiles = true @imagefiles = {} if @imagefiles.nil? && @flag_imagefiles def amazon(str, type) case type when :P, :UL, :DL str.gsub!(/[\((]amz:(\d-[\d-]{9}-\w)[\))]/){ isbn = $1 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 image_width, image_height = amz.image_width.to_i*2/3, amz.image_height.to_i*2/3 img = "\"#{amz.title}\"" if @flag_imagefiles @imagefiles[@diary.date] = [amz.image_url, image_width, image_height] if @imagefiles[@diary.date].nil? end 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}
" end body } when :HTML if @diary.kind_of?(PastDiary) && @flag_imagefiles imagefile_write(@diary.date[0..5]) #bookstore_x内で定義 end end end end if __FILE__ == $0 amz =Amazon::new amz.cache_dir = '.' amz.isbn = '4-06-198321-0' puts amz.title,amz.author,amz.pubdate,amz.price end