# bk1.rb $Revision: 1.11 $
# Copyright (C) 2004  Michitaka Ohno <elpeo@mars.dti.ne.jp>
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

require 'net/http'
require 'timeout'

def get_bk1_aid
	if @conf['bk1.aid'] && @conf['bk1.aid'] != "" then
		@conf['bk1.aid']
	else
		'freelink'
	end
end

def get_bk1( bibid )
	aid = get_bk1_aid
	h = Hash.new

	limittime = 20

	cache = "#{@cache_path}/bk1"
	Dir::mkdir( cache ) unless File::directory?( cache )
	begin
		item = File::readlines( "#{cache}/#{bibid}" )
		raise if item.length < 2
		item.each do |line|
			key, value = line.split( /\t/, 2 )
			h[ key ] = value.strip
		end
	rescue
		host = 'cgi.bk1.jp'
		port = 80
		path = "/xml.cgi?bibid=#{bibid}&aid=#{aid}"
		timeout( limittime ) do
			begin
				Net::HTTP.version_1_1
				Net::HTTP.start( host.untaint, port.untaint ) do |http|
					response, = http.get( path )
					begin
						require 'uconv'
						buf = Uconv.u8toeuc( response.body )
					rescue LoadError
						require 'utf8decode'
						buf = utf8decode( response.body )
					end
					buf.scan( /<([A-Z][A-Za-z]*)>([^<]*)<\/\1>/ ) do |tag|
						h[ tag[0] ] = tag[1]
					end
					if /<Details url="(.*?)">/ =~ buf then
						h['DetailURL'] = $1.gsub( /%26/, '&' )
					end
				end
			rescue Exception
			rescue
				$stderr.puts "#{$0}: #{$!.to_s}"
			end
		end
# for BAKA no KABE
#		if h['BIBID'] == '02309600' then
#			isbn = bibid.gsub( /-/, '' ).strip
#			if isbn != '4106100037' && isbn != '02309600' then
#				h.clear
#			end
#		end
# for bk1 STRAP
		if h['BIBID'] == '02397767' then
			isbn = bibid.gsub( /-/, '' ).strip
			if isbn != '4999997002' && isbn != '02397767' then
				h.clear
			end
		end
		open("#{cache}/#{bibid}","w") do |f|
			h.each do |key, value|
				f.print "#{key}\t#{value}\n"
			end
		end
	end
	if @conf['bk1.site'] == 1 && h['BIBID'] then
		h['DetailURL'] = "http://breeder.bk1.jp/rd/#{h['BIBID']}/#{aid}/noentry";
	end
	h['AID'] = aid
	return h
end

def bk1( bibid )
	h = get_bk1( bibid )

	return bibid if h.length < 2

	basketImage = 'http://www.bk1.co.jp/images/bk1/ic-kago.gif'
	basketURL = "http://www.bk1.co.jp/cgi-bin/bk1bskt.cgi?aid=#{h['AID']}&b=#{h['BIBID'] || bibid}&n=1"
	basket = %Q[<a href="#{basketURL}"><img src="#{basketImage}" vspace="1" style="border-width: 0px" alt="basket"></a>]

	<<-HTML
	<span class="bk1">
	<a href="#{h['DetailURL']}">
	<img class="bk1" src="#{h['ImageUrlSmall']}" align="left" hspace="5" style="border-width: 0px" alt="bk1">
	<strong>#{h['ProductName']}</strong> </a><br />
	#{h['Authors']}<br />
	#{h['Manufacturer']} / #{h['TotalPrice']} (#{h['OurPrice']}) / #{h['DateOfIssue']}<br />
	#{basket}
	</span>
	HTML
end

def bk1_conf_proc
	if @mode == 'saveconf' then
		@conf['bk1.aid'] = @cgi.params['bk1.aid'][0]
		@conf['bk1.site'] = @cgi.params['bk1.site'][0].to_i
		if @cgi.params['bk1.clearcache'][0] == 'true' then
			Dir["#{@cache_path}/bk1/*"].each do |cache|
				File::delete( cache.untaint )
			end
		end
	end

	<<-HTML
	<h3>#{@bk1_label_aid}</h3>
	<p><input name="bk1.aid" value="#{CGI::escapeHTML( @conf['bk1.aid'] ) if @conf['bk1.aid']}"></p>
	<h3>#{@bk1_label_site}</h3>
	<p><select name="bk1.site">
	<option value="0"#{" selected" if @conf['bk1.site']==0}>#{@bk1_label_bk1cojp}</option>
	<option value="1"#{" selected" if @conf['bk1.site']==1}>#{@bk1_label_bk1jp}</option>
	</select></p>
	<h3>#{@bk1_label_clearcache}</h3>
	<p><input type="checkbox" name="bk1.clearcache" value="true">#{@bk1_label_clearcache_desc}</input></p>
	HTML
end

# ja/bk1.rb
# Copyright (C) 2004  Michitaka Ohno <elpeo@mars.dti.ne.jp>
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

@bk1_label_conf = 'bk1プラグイン'
@bk1_label_aid = 'bk1ブリーダーIDの指定'
@bk1_label_site = '詳細のリンク先として使用するサイト'
@bk1_label_bk1cojp = 'bk1.co.jp'
@bk1_label_bk1jp = 'bk1.jp'
@bk1_label_clearcache = 'キャッシュの削除'
@bk1_label_clearcache_desc = 'イメージ関連情報のキャッシュを削除する(bk1上の表示と矛盾がある場合に試して下さい)'


add_conf_proc( 'bk1', @bk1_label_conf ) do
	bk1_conf_proc
end
