# # mingplot plugin for hiki 0.6 or later # # Copyright (C) 2003-2004 Satoru Takabayashi # All rights reserved. # This is free software with ABSOLUTELY NO WARRANTY. # # You can redistribute it and/or modify it under the terms of # the GNU General Public License version 2. # require 'amazonplot' require 'googleplot' def amazonplot_config must_options = [ 'mingplot.flash_font', 'mingplot.amazonplot.dir', 'mingplot.amazonplot.base_url', 'mingplot.amazonplot.token', 'mingplot.amazonplot.locale', ] check_options(must_options) config = { :flash_font => @options['mingplot.flash_font'], :output_dir => @options['mingplot.amazonplot.dir'], :base_url => @options['mingplot.amazonplot.base_url'], :token => @options['mingplot.amazonplot.token'], :locale => @options['mingplot.amazonplot.locale'], :id => @options['mingplot.amazonplot.id'], } return config end def googleplot_config must_options = [ 'mingplot.flash_font', 'mingplot.googleplot.dir', 'mingplot.googleplot.base_url', ] check_options(must_options) config = { :flash_font => @options['mingplot.flash_font'], :output_dir => @options['mingplot.googleplot.dir'], :base_url => @options['mingplot.googleplot.base_url'], } return config end def amazonplot (asin) asin = asin.to_s if asin.kind_of?(Integer) asin.gsub!(/\W/, "") config = amazonplot_config return mingplot_common(AmazonPlotStat, asin, config) end def amazonplot_compact_format (info) line = sprintf("[%s] %s: %s", info.catalog.escapeHTML, (info.author or info.artist or info.manufacturer or "").escapeHTML, info.search_url, info.asin.escape, info.title.escapeHTML) return Iconv.u8toeuc(line) + " " + Commify.commify(info.price) + "円" end def amazonplot_title_format (info) line = sprintf("%s", info.search_url, info.asin.escape, info.title.escapeHTML) return Iconv.u8toeuc(line) end def amazonplot_title_format2 (info) line = sprintf("%s", info.asin.escape, info.title.escapeHTML) return Iconv.u8toeuc(line) end def amazonplot_compact_common (asin, format_method) asin = asin.to_s if asin.kind_of?(Integer) asin.gsub!(/\W/, "") config = amazonplot_config stat = AmazonPlotStat.new(asin, config) if stat.latest_info.nil? unless stat.update return (asin + ": not found") end end __send__(format_method, stat.latest_info) end def amazonplot_title (asin) amazonplot_compact_common(asin, :amazonplot_title_format) end def amazonplot_title2 (asin) amazonplot_compact_common(asin, :amazonplot_title_format2) end def amazonplot_compact (asin) amazonplot_compact_common(asin, :amazonplot_compact_format) end def googleplot (query) query = Iconv.euctou8(query) config = googleplot_config return mingplot_common(GooglePlotStat, query, config) end def check_options (must_options) must_options.each {|key| raise "$options['#{key}'] must be specified" if @options[key].nil? } end def mingplot_common (stat_class, key, config) stat = stat_class.new(key, config) html = if stat.chart_exist? stat.chart_html else if stat.update stat.draw_chart stat.chart_html else key + ": not found" end end return Iconv.u8toeuc(html) end def make_asin_table table = Hash.new amazonplot_regex = /\{\{amazonplot(?:_(?:compact|title))?\((.*)\)\}\}/ @db.page_info.each {|info| name = info.keys.first @db.load(name).each {|line| if m = amazonplot_regex.match(line) and mm = /([^"']+)/.match(m[1]) asin = mm[1] table[asin] = name end } } return table end def show_asin_table table = make_asin_table s = "" return s end def amazonplot_sale_list config = amazonplot_config if amazonplot_use_cache? amazonplot_load_cache else make_amazonplot_sale_list(config) end end def amazonplot_compare (a, b, *fields) fields.each {|field| if a.send(field) and b.send(field) aa = a.send(field) bb = b.send(field) aa = aa.downcase if aa.is_a?(String) bb = bb.downcase if bb.is_a?(String) if (c = (aa <=> bb)) != 0 return c end end } return 0 end def make_amazonplot_sale_list (config) asin_table = make_asin_table recent_days = (@options['mingplot.amazonplot.recent_days'] or 14) discount_rate = (@options['mingplot.amazonplot.discount_rate'] or 0.1) time = Time.now last_modified = time.strftime("%Y-%02m-%02d %02H:%02M:%02S") html = sprintf("

リストの最終更新日時: %s

", "{font-size: smaller; text-align: right}", last_modified) @db.set_attribute(@page, [[:last_modified, time]]) html << "\n" amazonplot_update_cache(html) return html end def amazonplot_cache_file_name File.join(@cache_path, "amazonplot_sale_list") end def amazonplot_load_cache File.new(amazonplot_cache_file_name).read end def amazonplot_update_cache (html) File.open(amazonplot_cache_file_name, "w") {|f| f.print html } end def amazonplot_use_cache? cache_expiration_time = (@options['mingplot.amazonplot.cache_expiration_time'] or 3600 * 12) File.exist?(amazonplot_cache_file_name) and Time.now - File.mtime(amazonplot_cache_file_name) < cache_expiration_time end # # Following methods are based on comment.rb included in hiki # but highly simplified. # add_body_enter_proc(Proc.new do @amazonplot_insert_id = 0 "" end) def amazonplot_insert @amazonplot_insert_id += 1 <
ASIN:
EOS end def amazonplot_post params = @cgi.params id = (params['id'][0] || 0).to_i asin = params['asin'][0] return '' if asin.strip.empty? lines = @db.load(@page) md5hex = @db.md5hex(@page) count = 1 content = '' lines.each do |line| content << line if /^\{\{amazonplot_insert.*\}\}/.match(line) content << "* {{amazonplot_compact('#{asin}')}}\n" if count == id count += 1 end end @db.save(@page, content, md5hex) end export_plugin_methods(:amazonplot_post, :amazonplot_insert, :amazonplot_compact, :amazonplot_title, :amazonplot_title2, :amazonplot, :amazonplot_sale_list, :googleplot)