#!/usr/local/bin/ruby # plugin/bk1ranking.rb =begin ndiary.conf内で以下の様な指定等どうぞ。 BOOKSTORE_BK1_IMAGE = './www/' #書影保存位置 BOOKSTORE_BK1_IMAGEURL = './' #保存書影へのリンク位置 #BOOKSTORE_BK1_IMAGEURL = 'http://ippo.itbdns.com/image/cache/bk1/' CACHE_DIRECTORY = '../log/bookcache/'#+'bk1/' #キャッシュ保存の基底位置 #CACHE_DIRECTORY = '../../bookcache/' USER_LIB_DIRECTORY = '../user/lib/' #ライブラリ位置 #相対指定ならdiary(script/diary)位置からで指定 =end # bk1ranking.rb/.cgi 贋bk1俺ランキング生成 # 2003/12/24 ver.0.1 by ippo MerryChirstmas! # 2003/12/26 ver.0.2 by ippo rank.rdf, ranklist.html生成 # 2004/01/08 ver.0.3 by ippo 微調整 # 2004/01/19 ver.0.4 by ippo 微調整・nDiary-plug部分 # RSS等多くの予備的linkは未実装 # 見栄えはCSSで制御の為別途file必要+正規通りの見栄えには調整必要 # IEでよくてもOperaで見ると崩れたり…… #require 'kconv' #require 'cookie' #require 'webagent' #require '../user/lib/cache' #require '../user/lib/cachebib' #require '../user/lib/bk1' class Bk1Ranking attr_accessor :list, :size, :title, :mode, :url_source attr_accessor :flag_full, :flag_noinfo_del, :flag_noimage_del attr_accessor :flag_bk1_xml, :flag_cacheserver attr_accessor :cache_dir, :flag_image, :dir_image, :url_image, :flag_net attr_accessor :expire, :flag_review BK1_NO_IMAGE = 'no_img_90.gif' BK1_RANK_TITLE = 'お気に入り' BK1_RANK_HTML=< 贋bk1俺ランキング
贋bk1俺ランキング
RSS
.

and more...
贋bk1俺ランキングを作ろう!
EOS BK1_LIST_HTML=< 贋bk1俺ランキングリスト
順位 書籍情報

                
EOS def initialize ( list = [], size = 10, mode = :top ) @list = list @size = list.size > size ? size : list.size @mode = mode @list = @list.reverse if @mode == :bottom @list = random(@list) if @mode == :rand @title = BK1_RANK_TITLE @url_source = './rank.html' @cache_dir = './' #'../../html/cache/' + 'bk1/' @flag_image = true @dir_image = './' #'./www/' #'../../html/image/cache/' + 'bk1/' @url_image = './' #'http://ippo.itbdns.com/image/cache/' + 'bk1/' @flag_net = true @expire = 12 @flag_review = false end def random ( list ) newlist = [] srand(Time.now.to_i) rand(list.size) #最初の値は一定の時がある? のでダミー一個かます while list.size > 0 n = rand(list.size) newlist << list[n] list.delete_at(n) end return newlist end def ranking html = BK1_RANK_HTML if !flag_full if html =~ %r|(.+?)|mi html = $1 end end head, body, tail = '', html, '' if html =~ %r|(.+?)|mi head, body, tail = $`, $1, $' end head.gsub!(%r||, @title) booklist = '' list[0...@size].each_index{ |i| isbn = list[i] str = body.dup book = CacheBib_bk1.new() book.cache_dir = @cache_dir book.flag_image = true book.dir_image = @dir_image book.url_image = @url_image book.flag_net = @flag_net book.expire = @expire book.flag_review = @flag_review book.extend CacheBib_bk1_xml if @flag_bk1_xml && !@flag_cacheserver info = book.get_data(isbn) book.extend CacheBib_bk1_xml if @flag_bk1_xml && @flag_cacheserver next if @flag_noinfo_del && info['id'].nil? next if @flag_noimage_del && info['image'].nil? num = (i+1).to_s title, author = info['title'], info['author'] image, width, height = info['image'], info['image_width'], info['image_height'] image = info['image_local'] unless info['image_local'].nil? image = BK1_NO_IMAGE if image.nil? width, height = '68', '90' if width.to_s == '' if height.to_i > 90 width = (width.to_f * 90.0 / height.to_f).to_i.to_s height = '90' elsif width.to_i > 90 height = (height.to_f * 90.0 / width.to_f).to_i.to_s width = '90' end image = %Q|ISBN#{isbn}| title = %Q|#{title}| author = %Q|#{author}著| str.gsub!(%r||, num) str.gsub!(%r||, image) str.gsub!(%r||, title) str.gsub!(%r||, author) booklist << str } return head + booklist + tail end def rankinglist html = BK1_LIST_HTML if !flag_full if html =~ %r|(.+?)|mi html = $1 end end head, body, tail = '', html, '' if html =~ %r|(.+?)|mi head, body, tail = $`, $1, $' end booklist = '' list.each_index{ |i| isbn = list[i] str = body.dup book = CacheBib_bk1.new() book.cache_dir = @cache_dir book.flag_image = true book.dir_image = @dir_image book.url_image = @url_image book.flag_net = @flag_net book.expire = @expire book.flag_review = @flag_review book.extend CacheBib_bk1_xml if @flag_bk1_xml && !@flag_cacheserver info = book.get_data(isbn) book.extend CacheBib_bk1_xml if @flag_bk1_xml && @flag_cacheserver next if @flag_noinfo_del && info['id'].nil? next if @flag_noimage_del && info['image'].nil? num = (i+1).to_s title, author, publisher, label = info['title'], info['author'], info['publisher'], info['label'] isbn, pubsize, page, pubdate, price = info['isbn'], info['pubsize'], info['page'], info['pubdate'], info['price'] title = %Q|#{title}(#{label})| unless label.nil? title = %Q|#{title}| author = %Q|#{author}著| unless author.nil? size = pubsize size = %Q|#{pubsize}/#{page}| unless page.nil? price.to_s.gsub!(/\\/, '\\\\\\') str.gsub!(%r||, num.to_s) str.gsub!(%r||, title.to_s) str.gsub!(%r||, author.to_s) str.gsub!(%r||, publisher.to_s) str.gsub!(%r||, size.to_s) str.gsub!(%r||, isbn.to_s) str.gsub!(%r||, pubdate.to_s) str.gsub!(%r||, price.to_s) booklist << str } return head + booklist + tail end def rankingrss rsslist = [] list.each_index{ |i| isbn = list[i] book = CacheBib_bk1.new() book.cache_dir = @cache_dir book.flag_image = true book.dir_image = @dir_image book.url_image = @url_image book.flag_net = @flag_net book.expire = @expire book.flag_review = @flag_review book.extend CacheBib_bk1_xml if @flag_bk1_xml && !@flag_cacheserver info = book.get_data(isbn) book.extend CacheBib_bk1_xml if @flag_bk1_xml && @flag_cacheserver next if @flag_noinfo_del && info['id'].nil? next if @flag_noimage_del && info['image'].nil? title, label, url= info['title'], info['label'], book.url_bibinfo title = %Q|#{title}(#{label})| unless label.nil? url.gsub!(/&/, '&') rsslist << [url, title] } head, body = [], [] rsslist.each{ |url, title| head << %Q| | body << [%Q| |, %Q| #{title}|, %Q| #{url}|, %Q| |].join("\n") } # rss=< 贋bk1俺ランキング [#{@title}] #{@url_source} 贋bk1俺ランキング [#{@title}] ja #{head.join("\n")} #{body.join("\n")} EOS return rss end end def plug(opt) flag_net = @diary.config['nonet'] ? false : true #キャッシュ位置等 @flag_bk1_image = @diary.config['BOOKSTORE_BK1_IMAGE'] @flag_bk1_image = './www/' if @flag_bk1_image.to_s == '' @flag_bk1_xml = @diary.config['BOOKSTORE_BK1_XML'] @flag_cacheserver = @diary.config['CACHE_SERVER'] @url_bk1_image = @diary.config['BOOKSTORE_BK1_IMAGEURL'] @url_bk1_image = 'http://ippo.itbdns.com/image/cache/bk1/' if @url_bk1_image.to_s == '' @cache_base_dir = @diary.config['CACHE_DIRECTORY'] @cache_base_dir = @diary.logDirectory.to_s.chomp('/') + '/bookcache' if @cache_base_dir.to_s == '' @cache_base_dir.chomp!('/') cache_dir = @cache_base_dir + '/bk1/' #必要なライブラリの組込 @user_lib_dir = @diary.config['USER_LIB_DIRECTORY'] @user_lib_dir = '../user/lib' if @user_lib_dir.to_s == '' @user_lib_dir.chomp!('/') require "#{@user_lib_dir}/cookie" require "#{@user_lib_dir}/webagent" require "#{@user_lib_dir}/cache" require "#{@user_lib_dir}/cachebib" require "#{@user_lib_dir}/bk1" require "#{@user_lib_dir}/cache_ippo" if @flag_cacheserver #=begin lis1 = [ '4-15-010070-5', '4-15-010207-4', '4-15-010245-7', '4-15-010265-1', '4-15-010291-0', '4-15-010380-1', '4-15-010471-9', '4-15-010555-3', '4-15-010708-4', '4-15-010739-4' ] tit1 = '早川文庫SF' lis2 = [ '4-04-425701-9', '4-04-425702-7', '4-04-425703-5', '4-04-425704-3', '4-04-425705-1', '4-04-425706-X', '4-04-425707-8', '4-04-425708-6', '4-04-425710-8' ] tit2 = 'でたまか' lis3 = [ '4-08-614459-X', '4-08-614554-5', '4-08-614591-X', '4-08-614661-4', '4-08-614695-9', '4-08-614715-7', '4-08-614817-X', '4-08-614841-2', '4-08-614895-1', '4-08-600078-4', '4-08-600136-5', '4-08-600210-8', '4-08-600243-4', '4-08-600284-1', '4-08-600337-6', '4-08-600360-0' ] tit3 = 'マリア様がみてる' rank = nil srand rand(3) s = rand(3) case s when 0 rank = Bk1Ranking.new(lis1, 10, :rand) rank.title = tit1 when 1 rank = Bk1Ranking.new(lis2) rank.title = tit2 when 2 rank = Bk1Ranking.new(lis3, 10, :bottom) rank.title = tit3 end #=end # list = [ '4-15-010292-9', '4-15-010617-7', '4-15-011052-2', '4-19-577438-1', # '4-19-577439-X', '4-89456-522-6', '4-89456-523-4', '4-15-010770-X', # '4-15-010862-5', '4-15-010863-3' ] # tit = 'クレギ2後書特集' # rank = Bk1Ranking.new(list) # rank.title = tit # # list = [] # open(diary.logDirectory + './rank.txt').each{ |line| list << line.strip } # title = list.shift # rank = Bk1Ranking.new(list) # rank.title = title rank.cache_dir = cache_dir rank.dir_image = @flag_bk1_image rank.url_image = @url_bk1_image rank.flag_net = flag_net rank.url_source = 'http://ippo.s5.xrea.com/diary/s_diary.html' rank.flag_full = true rank.flag_bk1_xml = @flag_bk1_xml rank.flag_cacheserver = @flag_cacheserver open(@diary.outputDirectory + './ranklist.html', 'w').puts rank.rankinglist.toeuc rank.flag_full = false open(@diary.outputDirectory + './rank.rdf', 'w').puts rank.rankingrss.toeuc return rank.ranking end #===== test ===== if __FILE__ == $0 list = ['4-15-010070-5', '4-15-010207-4', '4-15-010245-7', '4-15-010265-1', '4-15-010291-0', '4-15-010380-1', '4-15-010471-9', '4-15-010555-3', '4-15-010708-4', '4-15-010739-4'] rank = Bk1Ranking.new(list) rank.title = '早川文庫SF' rank.flag_full = true open('./rank.html', 'w').puts rank.ranking.toeuc unless ENV['SCRIPT_NAME'].nil? require 'cgi' cgi = CGI.new() cgi.out(){ 'rank.html' } end end