# today_amazon.rb $Revision: 1.2 $
# Copyright (C) 2006  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 'timeout'
require 'net/http'
require 'rexml/document'

#@today_amazon_size = [468, 60, 13] # 468x60 (Small)
@today_amazon_size = [728, 90, 48] # 728x90 (Large)

@today_amazon_modelist = [
'books-jp',
#'books-us',
#'classical-jp',
'electronics-jp',
#'kitchen-jp',
'music-jp',
'dvd-jp',
#'vhs-jp',
'software-jp',
'videogames-jp',
#'toys-jp',
#'sporting-goods-jp',
]

def today_amazon_keywords( date, update = false )
	cache = "#{@cache_path}/today_amazon"
	Dir::mkdir( cache ) unless File::directory?( cache )
	file = "#{cache}/#{date.strftime( '%Y%m%d' )}"
	words = []
	begin
		raise if update
		words = File::readlines( file ).map{|i| i.strip}
	rescue
		old_apply_plugin = @conf['apply_plugin']
		@conf['apply_plugin'] = true
		src = apply_plugin( @diaries[date.strftime( '%Y%m%d' )].to_html( {} ), true )
		@conf['apply_plugin'] = old_apply_plugin
		query = "content=#{CGI::escape( NKF::nkf( '-m0 -Ew', src ) )}"
		header = {'Content-Type' => 'application/x-www-form-urlencoded'}
		doc = nil
		timeout( 20 ) do
			begin
				Net::HTTP.version_1_2
				Net::HTTP.start( 'bulkfeeds.net', 80 ) do |http|
  					response, = http.post( '/app/terms.xml', query, header )
					doc = REXML::Document::new( response.body ).root
				end
			rescue Exception
				$stderr.puts "#{$0}: #{$!.to_s}"
			end
		end
		if doc then
			doc.elements.each( 'term' ) {|i| words << i.text}
			unless words.empty? then
				open( file, "w" ) do |f|
					words.each {|i| f.puts i}
				end
			end
		end
	end
	words
end

def today_amazon( date )
	words = today_amazon_keywords( date )
	search = CGI::escape( words[0,5].join( "|" ) )
	aid =  @conf['amazon.aid'] || ''
	mode = @today_amazon_modelist[rand( @today_amazon_modelist.length )]
	w, h, p = @today_amazon_size
	<<-HTML
	<div class="amazon-livelink">
	<iframe src="http://rcm-jp.amazon.co.jp/e/cm?t=#{aid}&o=9&p=#{p}&l=st1&mode=#{mode}&search=#{search}&lt1=_blank&f=ifr" marginwidth="0" marginheight="0" width="#{w}" height="#{h}" border="0" frameborder="0" style="border:none;" scrolling="no"></iframe>
	</div>
	HTML
end

if @mode == 'day' && !@cgi.mobile_agent? && !bot? then
	add_body_leave_proc do |date|
		today_amazon( date )
	end
end

if ['append','replace'].include?( @mode ) then
	add_update_proc do
		today_amazon_keywords( @date, true )
	end
end
