| 1 | # yshop.rb $Revision: 2 $ |
|---|
| 2 | # Copyright (C) 2008 Michitaka Ohno <elpeo@mars.dti.ne.jp> |
|---|
| 3 | # You can redistribute it and/or modify it under GPL2. |
|---|
| 4 | |
|---|
| 5 | # ja/yshop.rb |
|---|
| 6 | @yshop_label_conf ='Yahoo!ショッピング' |
|---|
| 7 | @yshop_label_appid = 'アプリケーションID' |
|---|
| 8 | @yshop_label_affiliate_type = 'アフィリエイトの種類' |
|---|
| 9 | @yshop_label_yid = 'Yahoo! JAPANアフィリエイト' |
|---|
| 10 | @yshop_label_vc = 'バリューコマースアフィリエイト' |
|---|
| 11 | @yshop_label_affiliate_id = 'アフィリエイトID' |
|---|
| 12 | @yshop_label_imgsize = '表示するイメージのサイズ' |
|---|
| 13 | @yshop_label_medium = '普通' |
|---|
| 14 | @yshop_label_small = '小さい' |
|---|
| 15 | @yshop_label_clearcache = 'キャッシュの削除' |
|---|
| 16 | @yshop_label_clearcache_desc = 'イメージ関連情報のキャッシュを削除する(Yahoo!ショッピング上の表示と矛盾がある場合に試して下さい)' |
|---|
| 17 | |
|---|
| 18 | require 'rexml/document' |
|---|
| 19 | require 'open-uri' |
|---|
| 20 | require 'kconv' |
|---|
| 21 | |
|---|
| 22 | eval( <<-TOPLEVEL_CLASS, TOPLEVEL_BINDING ) |
|---|
| 23 | require 'erb' |
|---|
| 24 | include ERB::Util |
|---|
| 25 | TOPLEVEL_CLASS |
|---|
| 26 | |
|---|
| 27 | def yshop_get( param, store ) |
|---|
| 28 | param = {:jan => param.to_s} unless Hash === param |
|---|
| 29 | param[:store_id] = store if store |
|---|
| 30 | cache = "#{@cache_path}/yshop" |
|---|
| 31 | Dir::mkdir( cache ) unless File::directory?( cache ) |
|---|
| 32 | file = "#{cache}/#{u( param.to_s )}.xml" |
|---|
| 33 | begin |
|---|
| 34 | xml = File::read( file ) |
|---|
| 35 | rescue |
|---|
| 36 | qs = [] |
|---|
| 37 | qs << 'appid='+u( @conf['yshop.appid']||'YahooDemo' ) |
|---|
| 38 | qs << 'affiliate_type='+u( @conf['yshop.affiliate_type'] ) if @conf['yshop.affiliate_type'] |
|---|
| 39 | qs << 'affiliate_id='+u( @conf['yshop.affiliate_id'] ) if @conf['yshop.affiliate_id'] |
|---|
| 40 | [:query, :jan, :isbn, :store_id].each do |item| |
|---|
| 41 | qs << "#{item}="+u( param[item].toutf8 ) if param.include?( item ) |
|---|
| 42 | end |
|---|
| 43 | qs << 'hits=1' |
|---|
| 44 | xml = open( 'http://shopping.yahooapis.jp/ShoppingWebService/V1/itemSearch?'+(qs*'&') ){|f| f.read} |
|---|
| 45 | open( file, 'wb' ) {|f| f.write( xml )} if xml |
|---|
| 46 | end |
|---|
| 47 | REXML::Document.new( xml ).root if xml |
|---|
| 48 | end |
|---|
| 49 | |
|---|
| 50 | def yshop_get_image( doc ) |
|---|
| 51 | img = Struct.new( :size, :src, :width, :height ).new |
|---|
| 52 | if @conf['yshop.imgsize'] == 1 then |
|---|
| 53 | img.size = 'Small' |
|---|
| 54 | img.width = img.height = 76 |
|---|
| 55 | else |
|---|
| 56 | img.size = 'Medium' |
|---|
| 57 | img.width = img.height = 146 |
|---|
| 58 | end |
|---|
| 59 | begin |
|---|
| 60 | img.src = doc.elements["Result/Hit/Image/#{img.size}"].text |
|---|
| 61 | rescue |
|---|
| 62 | img.src = "http://i.yimg.jp/images/sh/noimage/#{img.width}x#{img.height}.gif" |
|---|
| 63 | end |
|---|
| 64 | img |
|---|
| 65 | end |
|---|
| 66 | |
|---|
| 67 | def yshop_get_brands( doc ) |
|---|
| 68 | begin |
|---|
| 69 | @conf.to_native( doc.elements['Result/Hit/Brands/Name'].text ) |
|---|
| 70 | rescue |
|---|
| 71 | '-' |
|---|
| 72 | end |
|---|
| 73 | end |
|---|
| 74 | |
|---|
| 75 | def yshop_get_price( doc ) |
|---|
| 76 | begin |
|---|
| 77 | r = doc.elements['Result/Hit/Price'].text |
|---|
| 78 | nil while r.gsub!(/(.*\d)(\d\d\d)/, '\1,\2') |
|---|
| 79 | "\\"+r |
|---|
| 80 | rescue |
|---|
| 81 | '(no price)' |
|---|
| 82 | end |
|---|
| 83 | end |
|---|
| 84 | |
|---|
| 85 | def yshop_get_shop( doc ) |
|---|
| 86 | begin |
|---|
| 87 | @conf.to_native( doc.elements['Result/Hit/Store/Name'].text ) |
|---|
| 88 | rescue |
|---|
| 89 | '-' |
|---|
| 90 | end |
|---|
| 91 | end |
|---|
| 92 | |
|---|
| 93 | def yshop_get_html( param, store, pos ) |
|---|
| 94 | doc = yshop_get( param, store ) |
|---|
| 95 | return unless doc && doc.attributes['totalResultsReturned'].to_i > 0 |
|---|
| 96 | name = @conf.to_native( doc.elements["Result/Hit/Name"].text ) |
|---|
| 97 | link = doc.elements["Result/Hit/Url"].text |
|---|
| 98 | img = yshop_get_image( doc ) |
|---|
| 99 | r = %Q[<a href="#{h( link )}">] |
|---|
| 100 | r << %Q[<img class="#{pos}" src="#{h( img.src )}" width="#{h( img.width )}" height="#{h( img.height )}" alt="#{h( name )}" title="#{h( name )}">] if img.src |
|---|
| 101 | r << h( name ) |
|---|
| 102 | r << %Q[</a>] |
|---|
| 103 | end |
|---|
| 104 | |
|---|
| 105 | def yshop_image( param, store = nil ) |
|---|
| 106 | yshop_get_html( param, store, 'amazon' ) |
|---|
| 107 | end |
|---|
| 108 | |
|---|
| 109 | def yshop_image_left( param, store = nil ) |
|---|
| 110 | yshop_get_html( param, store, 'left' ) |
|---|
| 111 | end |
|---|
| 112 | |
|---|
| 113 | def yshop_image_right( param, store = nil ) |
|---|
| 114 | yshop_get_html( param, store, 'right' ) |
|---|
| 115 | end |
|---|
| 116 | |
|---|
| 117 | def yshop_detail( param, store = nil ) |
|---|
| 118 | doc = yshop_get( param, store ) |
|---|
| 119 | return unless doc && doc.attributes['totalResultsReturned'].to_i > 0 |
|---|
| 120 | name = @conf.to_native( doc.elements["Result/Hit/Name"].text ) |
|---|
| 121 | link = doc.elements["Result/Hit/Url"].text |
|---|
| 122 | img = yshop_get_image( doc ) |
|---|
| 123 | brands = yshop_get_brands( doc ) |
|---|
| 124 | price = yshop_get_price( doc ) |
|---|
| 125 | shop = yshop_get_shop( doc ) |
|---|
| 126 | <<-HTML |
|---|
| 127 | <a href="#{h( link )}"> |
|---|
| 128 | <img class="amazon-detail left" src="#{h( img.src )}" |
|---|
| 129 | width="#{h( img.width)}" height="#{h( img.height )}" |
|---|
| 130 | alt="#{h( name )}" title="#{h( name )}"> |
|---|
| 131 | </a> |
|---|
| 132 | <span class="amazon-title">#{h( name )}</span><br> |
|---|
| 133 | <span class="amazon-author">#{h( brands )}</span><br> |
|---|
| 134 | <span class="amazon-label">#{h( shop )}</span><br> |
|---|
| 135 | <span class="amazon-price">#{h( price )}</span><br style="clear: left"> |
|---|
| 136 | HTML |
|---|
| 137 | end |
|---|
| 138 | |
|---|
| 139 | add_conf_proc( 'yshop', @yshop_label_conf ) do |
|---|
| 140 | yshop_conf_proc |
|---|
| 141 | end |
|---|
| 142 | |
|---|
| 143 | def yshop_conf_proc |
|---|
| 144 | if @mode == 'saveconf' then |
|---|
| 145 | @conf['yshop.imgsize'] = @cgi.params['yshop.imgsize'][0].to_i |
|---|
| 146 | if @cgi.params['yshop.clearcache'][0] == 'true' then |
|---|
| 147 | Dir["#{@cache_path}/yshop/*"].each do |cache| |
|---|
| 148 | File::delete( cache.untaint ) |
|---|
| 149 | end |
|---|
| 150 | end |
|---|
| 151 | if @cgi.params['yshop.appid'][0].empty? then |
|---|
| 152 | @conf['yshop.appid'] = nil |
|---|
| 153 | else |
|---|
| 154 | @conf['yshop.appid'] = @cgi.params['yshop.appid'][0] |
|---|
| 155 | end |
|---|
| 156 | if @cgi.params['yshop.affiliate_id'][0].empty? then |
|---|
| 157 | @conf['yshop.affiliate_type'] = nil |
|---|
| 158 | @conf['yshop.affiliate_id'] = nil |
|---|
| 159 | else |
|---|
| 160 | @conf['yshop.affiliate_type'] = @cgi.params['yshop.affiliate_type'][0] |
|---|
| 161 | @conf['yshop.affiliate_id'] = @cgi.params['yshop.affiliate_id'][0] |
|---|
| 162 | end |
|---|
| 163 | end |
|---|
| 164 | |
|---|
| 165 | <<-HTML |
|---|
| 166 | <h3>#{@yshop_label_appid}</h3> |
|---|
| 167 | <p><input name="yshop.appid" value="#{h( @conf['yshop.appid'] )}" size="100"></p> |
|---|
| 168 | <h3>#{@yshop_label_affiliate_type}</h3> |
|---|
| 169 | <p><select name="yshop.affiliate_type"> |
|---|
| 170 | <option value="yid"#{" selected" if @conf['yshop.affiliate_type'] == 'yid'}>#{@yshop_label_yid}</option> |
|---|
| 171 | <option value="vc"#{" selected" if @conf['yshop.affiliate_type'] == 'vc'}>#{@yshop_label_vc}</option> |
|---|
| 172 | </select></p> |
|---|
| 173 | <h3>#{@yshop_label_affiliate_id}</h3> |
|---|
| 174 | <p><input name="yshop.affiliate_id" value="#{h( @conf['yshop.affiliate_id'] )}" size="100"></p> |
|---|
| 175 | <h3>#{@yshop_label_imgsize}</h3> |
|---|
| 176 | <p><select name="yshop.imgsize"> |
|---|
| 177 | <option value="0"#{" selected" if @conf['yshop.imgsize'] == 0}>#{@yshop_label_medium}</option> |
|---|
| 178 | <option value="1"#{" selected" if @conf['yshop.imgsize'] == 1}>#{@yshop_label_small}</option> |
|---|
| 179 | </select></p> |
|---|
| 180 | <h3>#{@yshop_label_clearcache}</h3> |
|---|
| 181 | <p><label for="yshop.clearcache"><input type="checkbox" id="yshop.clearcache" name="yshop.clearcache" value="true">#{@yshop_label_clearcache_desc}</label></p> |
|---|
| 182 | HTML |
|---|
| 183 | end |
|---|