# $Id: bookstore.rb,v 1.12 2003/08/21 12:38:37 not Exp $ # 2003/09/21 修正 # 2003/11/3 修正 =begin usage: 文中の(ISBN4-04-873259-5)ないし(ISBN:4-04-873259-5)を 各オンライン書店へのリンクに変換 書店名・書影の有無・出荷状況・書評数等を書店毎に表示 書誌情報を表示 「ruby diary -nonet」というオプションで実行するとネット接続をせずに キャッシュから生成 ndiary.conf に以下のような行を追加すると表示するオンライン書店と その順番を指定 BOOKSTORE_SHOP_LIST = 'amazon bk1 yahoo asahiya jbook kinokuniya rakuten skysoft' 洋書の場合の指定 # ISBN0-xxx BOOKSTORE_SHOP_LIST_0 = 'amazon amazon.com skysoft a1books powells kinokuniya maruzen' # ISBN1-xxx BOOKSTORE_SHOP_LIST_1 = 'amazon_uk bookshop' 指定できるオンライン書店(ほとんどが未だ完全実装せずですが) amazon amazon.co.jp #ISBN4(?),0 bk1 bk1 #ISBN4 yahoo Yahoo! Books #ISBN4 asahiya 旭屋書店 #ISBN4 jbook Jbook #ISBN4 kinokuniya 紀伊國屋書店 #ISBN4,0(!) rakuten 楽天ブックス #ISBN4 skysoft skysoft #ISBN4,0(!) amazon.com amazon.com #ISBN0 a1books a1books #ISBN0 powells powells.com #ISBN0 maruzen 丸善 #ISBN0 amazon.co.uk amazon.co.uk #ISBN1 bookshop The Internet Bookshop #ISBN1 esbooks eS!BOOKS #? boople boople #未 rcom 読書共同体(否書店) #未 rl 書評リンク(否書店) #試 アソシエイト・プログラムに参加している場合は ndiary.conf に以下の行を追加 もちろん自分の ID に直して # bk1 BOOKSTORE_BK1_ID = 'p-not00114' # amazon.co.jp BOOKSTORE_AMAZON_ID = 'notsfragmen-22' # 楽天 BOOKSTORE_RAKUTEN_ID = '0001fc8b.1fe901a6' キャッシュの更新時間間隔とキャッシュファイルのフォルダ位置 CACHE_EXPIRE = 12 CACHE_DIRECTORY = '../log/bookcache/' 書評リンク(review-link)との連動では ndiary.conf に以下の行を追加 REVIEWER_CODE = '一歩' #自分のIDで REVIEWLINK_URL = 'http://ippo.s5.xrea.com/diary/' #fileの存在URL,未対応 REVIEWLINK_LINK = true #書店リンクのうちに書評リンクも含める reference: 松下さん(http://homepage3.nifty.com/akima/) (http://www.alles.or.jp/~akihisa/)のオンライン書店へのリンク 細井さん(http://www.fan.gr.jp/~hosoi/diary/diary.html)による洋書リンク (注:現在bookstore_x内では取り除いてます) 他・amazon.rb, webagent.rb等 customize: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ソース94行目あたりの require の指定ファイルとキャッシュフォルダは各位で適時改変 アフィリエイトは直接ソースの76行目あたりを修正しても設定可能 (無指定だと一歩のアフィリエイトになる場合があります) !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! note: キャッシュ用フォルダは(各書店分も含め)あらかじめ作成してください バグ)reviewlinkのcodeが日本語だと化ける場合があります =end class Filter BOOKSTOREX = Regexp::compile(/[\((]ISBN[:|:]?\s?(\d-[\d-]{9}[-]?[\w]?)[\))]/) unless defined?(BOOKSTOREX) def bookstore_y(str, type) unless @flag_initial_bookstore_x #-----initialize----- @flag_initial_bookstore_x = true if @diary.respond_to?('config') then @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 @shop_list = @diary.config['BOOKSTORE_SHOP_LIST'].to_s @shop_list0 = @diary.config['BOOKSTORE_SHOP_LIST_0'].to_s @shop_list1 = @diary.config['BOOKSTORE_SHOP_LIST_1'].to_s @reviewer_code = @diary.config['REVIEWER_CODE'].to_s #'一歩' @reviewlink_url = @diary.config['REVIEWLINK_URL'] @flag_reviewlinklink = @diary.config['REVIEWLINK_LINK'] #true @flag_net = @diary.config['nonet'] @expire = @diary.config['CACHE_EXPIRE'] @cache_base_dir = @diary.config['CACHE_DIRECTORY'].to_s else @bk1_id, @amazon_id, @rakuten_id = '', '', '' end @shop_list = 'amazon bk1 yahoo asahiya jbook kinokuniya rakuten skysoft reviewlink' if @shop_list.empty? @shop_list0 = 'amazon amazon.com skysoft a1books powells kinokuniya maruzen' if @shop_list0.empty? @shop_list1 = 'amazon_uk bookshop' if @shop_list1.empty? @expire = 12 if @expire.nil? @cache_base_dir = @diary.logDirectory.chomp('/') + '/bookcache' if @cache_base_dir.to_s == '' @cache_base_dir.chomp!('/') #-----各位の環境に合わせて適時改変-----ココカラ----- require '../user/lib/cookie' #必須・webagent用 require '../user/lib/webagent' #必須・webagent用 require '../user/lib/cache' #必須・キャッシュ用 require '../user/lib/amazon' #以下は各書店用・不要な書店分は削除可 require '../user/lib/bk1' require '../user/lib/yahoo' #? require '../user/lib/asahiya' require '../user/lib/jbook' require '../user/lib/kinokuniya' require '../user/lib/rakuten' #affiliate未対応 require '../user/lib/skysoft' # require '../user/lib/amazon_com' #? # require '../user/lib/a1books' #? # require '../user/lib/powells' #? # require '../user/lib/maruzen' #? # require '../user/lib/amazon_co_uk' #? # require '../user/lib/bookshop' #? require '../user/lib/esbooks' # require '../user/lib/boople' #??? # require '../user/lib/rcom' require '../user/lib/cache_bib_tmp' #未設定書店をbookstoreに準じさせる # require '../user/lib/rl' #このファイルの下部に実装。分けるべきか? @cache_dir = {} #-----書誌情報の保存フォルダの指定----- @cache_dir['amazon'] = "#{@cache_base_dir}/amz" @cache_dir['bk1'] = "#{@cache_base_dir}/bk1" @cache_dir['yahoo'] = "#{@cache_base_dir}/yho" @cache_dir['asahiya'] = "#{@cache_base_dir}/ash" @cache_dir['jbook'] = "#{@cache_base_dir}/jbk" @cache_dir['kinokuniya'] = "#{@cache_base_dir}/kin" @cache_dir['rakuten'] = "#{@cache_base_dir}/rak" @cache_dir['skysoft'] = "#{@cache_base_dir}/sky" @cache_dir['amazon_com'] = "#{@cache_base_dir}/amc" @cache_dir['a1books'] = "#{@cache_base_dir}/a1b" @cache_dir['powells'] = "#{@cache_base_dir}/pwl" @cache_dir['maruzen'] = "#{@cache_base_dir}/mrz" @cache_dir['amazon_uk'] = "#{@cache_base_dir}/amu" @cache_dir['bookshop'] = "#{@cache_base_dir}/bsp" @cache_dir['esbooks'] = "#{@cache_base_dir}/esb" @cache_dir['boople'] = "#{@cache_base_dir}/bpl" @cache_dir['rcom'] = "#{@cache_base_dir}/rcm" #-----各位の環境に合わせて適時改変-----ココマデ----- @bookstore_name = {} #-----提示する書店文字列の指定----- @bookstore_name['amazon'] = 'amazon' @bookstore_name['bk1'] = 'bk1' @bookstore_name['yahoo'] = 'Yahoo!' @bookstore_name['asahiya'] = '旭屋' @bookstore_name['jbook'] = 'Jbook' @bookstore_name['kinokuniya'] = '紀伊國屋' @bookstore_name['rakuten'] = '楽天' @bookstore_name['skysoft'] = 'skysoft' @bookstore_name['amazon_com'] = 'amazon.com' @bookstore_name['a1books'] = 'A1Books' @bookstore_name['powells'] = 'Powells.com' @bookstore_name['maruzen'] = '丸善' @bookstore_name['amazon_uk'] = 'amazon.co.uk' @bookstore_name['bookshop'] = 'The Internet Bookshop' @bookstore_name['esbooks'] = 'eS!' @bookstore_name['boople'] = 'boople' @bookstore_name['rcom'] = '読共' @bookstore_name['reviewlink'] = 'rl' end case type when :P, :UL, :DL str.gsub!(BOOKSTOREX){ mes_str = '
' #-----ISBN確認----- isbn1 = $1 checkbook = CacheBib.new() newisbn = checkbook.isbn_check(isbn1) if newisbn != isbn1 mes_str << "Warrning; ISBN-error; #{isbn1} -> #{newisbn}\n" $stderr.puts "WAR; ISBN-error; #{isbn1} -> #{newisbn}" end isbn1 = newisbn isbn2 = isbn1.delete('-') @bookstore_list = @shop_list.split(/\s+/) @bookstore_list = @shop_list0.split(/\s+/) if isbn2 =~ /^0/ @bookstore_list = @shop_list1.split(/\s+/) if isbn2 =~ /^1/ reviewlink = CacheBib_reviewlink.new() #-----書評リンク用----- reviewlink.isbn = isbn1 # reviewlink.affiliate_code = @reviewer_code unless @reviewer_code.empty? # reviewlink.url_head = @reviewlink_url unless @reviewlink_url.nil? # anchor = '' # if @diary.paragraphAnchor # if @diary.anchorEachTopic # anchor = "_t#{@diary.instance_eval{@cntTopic}}_#{@diary.instance_eval{@cntParagraph + 1}}" # else # anchor = "_#{@diary.instance_eval{@cntParagraph + 1}}" # end # end # reviewlink.url = "#{@diary.date2monthlyfilelink(@diary.date, true)}#{anchor}" # $stderr.puts 'CHK; reviewlink; ' + reviewlink.tag_info # rl_str = reviewlink.tag rl_str = '' infolist = [] #-----書店にネット接続/キャッシュ読----- @bookstore_list.each{ |bookstore| infolist << nil } =begin if flag_ippodb book = CacheBib_ippodb.new() book.expire = @expire flag_net = @flag_net book.cache_dir = @cache_dir[bookstore] book.bookstore_list = @bookstore_list infolist = book.get_data(isbn1, flag_net) # info["url_bibinfo"] = book.url_bibinfo # info['word_delivery'] = book.word_delivery end =end @bookstore_list.each_index{ |i| bookstore = @bookstore_list[i] book, info = nil, infolist[i] # next if flag_overload || (info['expire'] < @expire) begin case bookstore when 'amazon' book = CacheBib_amazon.new() book.affiliate_code = @amazon_id unless @amazon_id.empty? flag_image = true when 'bk1' book = CacheBib_bk1.new() book.affiliate_code = @bk1_id unless @bk1_id.empty? when 'yahoo' book = CacheBib_yahoo.new() when 'asahiya' book = CacheBib_asahiya.new() when 'jbook' book = CacheBib_jbook.new() when 'kinokuniya' book = CacheBib_kinokuniya.new() when 'rakuten' book = CacheBib_rakuten.new() book.affiliate_code = @rakuten_id unless @rakuten_id.empty? when 'skysoft' book = CacheBib_skysoft.new() when 'amazon_com' book = CacheBib_amazon_com.new() when 'a1books' book = CacheBib_a1books.new() when 'powells' book = CacheBib_powells.new() when 'maruzen' book = CacheBib_maruzen.new() when 'amazon_uk' book = CacheBib_amazon_uk.new() when 'bookshop' book = CacheBib_bookshop.new() when 'esbooks' book = CacheBib_esbooks.new() when 'boople' book = CacheBib_boople.new() when 'rcom' book = CacheBib_rcom.new() when 'reviewlink' book = reviewlink else $stderr.puts "WAR; unknown-bookstore; code:#{bookstore}" next end rescue Exception $stderr.puts "WAR; access-error; #{isbn1} - #{@bookstore_name[bookstore]}" next end book.expire = @expire flag_net = @flag_net #ネットへの接続を制限するなら例えば以下の様に。 #flag_net = true if bookstore == 'amazon' book.cache_dir = @cache_dir[bookstore] info = book.get_data(isbn1, flag_net) info["url_bibinfo"] = book.url_bibinfo begin #-----word delivery----- info['word_delivery'] = book.word_delivery rescue Exception #-----for no-define-word_delivery = rcom----- $stderr.puts "WAR; delivery-decode-error; #{isbn1} - #{@bookstore_name[bookstore]}" if bookstore != 'rcom' info['word_delivery'] = [nil, nil] end infolist[i] = info } storelist = [] #-----書店情報リスト作成----- @bookstore_list.each_index{ |i| bookstore = @bookstore_list[i] info = infolist[i] next if info.nil? flag_image = (bookstore == 'amazon') ? true : false new_str = '' if !info['image'].nil? #-----image----- if flag_image height = info['image_height'].to_i != 0 ? info['image_height'].to_s : '140' width = info['image_width'].to_i != 0 ? info['image_width'].to_s : '100' # mes_str << 'amazon ' mes_str << 'amazon ' else new_str << ' 絵' end end delivery, flag = info['word_delivery'] if flag new_str << ' '+ delivery + '' end if !info['reviews'].nil? new_str << ' ' + info['reviews'].size.to_s + "評" end if new_str.size > 0 #-----bookstore name----- if bookstore != 'rcom' new_str = @bookstore_name[bookstore] + new_str else #-----rcom custom----- new_str = '' + @bookstore_name[bookstore]+ '' + new_str end if flag == 'over' new_str = '' + @bookstore_name[bookstore]+ '' end end storelist << new_str if new_str.size > 0 } infolist.delete(nil) info = checkbook.merge_info( infolist ) info["isbn"] = isbn1 mes_str << rl_str #-----表示文字列作成----- mes_str << "【 " + storelist.join(" / ") + " 】\n" if storelist.size > 0 # mes_str << "題:" + info['title'] + "\n" if !info['title'].nil? # mes_str << "著:" + info['author'] + "\n" if !info['author'].nil? # mes_str << "訳:" + info['translator'] + "\n" if !info['translator'].nil? # mes_str << "符:ISBN" + info['isbn'] + "\n" if !info['isbn'].nil? # mes_str << "刊:" + info['publisher'] + "\n" if !info['publisher'].nil? # mes_str << "年:" + info['pubdate'] + "\n" if !info['pubdate'].nil? # mes_str << "型:" + info['size'] + "\n" if !info['size'].nil? # mes_str << "標:" + info['series'] + "\n" if !info['series'].nil? # mes_str << "値:" + info['price'] + "\n" if !info['price'].nil? # mes_str << "文:" + info['desc'] + "\n" if !info['desc'].nil? # mes_str = mes_str.strip + '
' mes_str = mes_str.strip mes_str << '
' } end end end class CacheBib_reviewlink attr_accessor :isbn, :affiliate_code, :url, :url_head attr_accessor :expire, :cache_dir def get_data ( isbn, flag_net = false ) @isbn = isbn return {'isbn'=>@isbn, 'reviewer'=>@affiliate_code, 'url'=>@url} end def url_bibinfo url = 'http://ippo.s5.xrea.com/x/rl/index.cgi?cmd=book&code=ISBN' + isbn.delete('-').upcase # url << "&reviewer=#{@affiliate_code}" unless @affiliate_code.nil? # url << "&url=#{@url_head + @url}" if !@url.nil? && !@url_head.nil? return CGI.escapeHTML(url) end def tag_info tags = [] tags << "ISBN#{@isbn}" tags << "reviewer=#{@affiliate_code}" unless @affiliate_code.nil? tags << "url=#{@url}" return tags.join(":") end def tag # return '' return '' end def word_delivery return ['x', 'over'] end end