root/platform/tdiary/plugin/search-yahoo.rb

Revision 38384, 3.0 kB (checked in by kayakaya, 21 months ago)

using Net::HTTP substitute for open-uri to avoid error in ruby-1.9.2

Line 
1# -*- coding: utf-8 -*-
2#
3# search-yahoo.rb - site search plugin sample using Yahoo! Search BOSS API.
4#
5# Copyright (C) 2009, TADA Tadashi <t@tdtds.jp>
6# You can redistribute it and/or modify it under GPL.
7#
8# Needed these options below:
9#
10# @options['search-yahoo.appid'] : Your BOSS APPID
11# @options['search-yahoo.result_filter'] : your dialy's URL format of DAY mode into Regexp.
12#
13
14require 'timeout'
15require 'rexml/document'
16require 'net/http'
17Net::HTTP.version_1_2
18
19def search_title
20        '全文検索 by Yahoo! Search BOSS'
21end
22
23def search_input_form( q )
24        r = <<-HTML
25                <form method="GET" action="#{@conf.index}"><div>
26                        検索キーワード:
27                        <input name="q" value="#{h q}">
28                        <input type="submit" value="OK">
29                </div></form>
30        HTML
31end
32
33def search_boss_api( q, start = 0 )
34        url = 'http://boss.yahooapis.com/ysearch/web/v1/'
35        appid = @conf['search-yahoo.appid']
36
37        url << "#{q}?appid=#{appid}&format=xml&count=20&start=#{start}"
38
39        proxy = @conf['proxy']
40        proxy = 'http://' + proxy if proxy
41       
42        proxy_host, proxy_port = nil
43        if proxy
44                proxy_host = proxy_uri.host
45                proxy_port = proxy_uri.port
46        end
47        proxy_class = Net::HTTP::Proxy(proxy_host, proxy_port)
48
49        query = URI.parse(url)
50        req = Net::HTTP::Get.new(query.request_uri)
51        http = proxy_class.new(query.host, query.port)
52        http.open_timeout = 20
53        http.read_timeout = 20
54        res = http.start do
55                http.request(req)
56        end
57        res.body
58end
59
60def search_to_html( str )
61        (str || '').gsub( /(?:<wbr(?:[ \t\r\n][^>]*)?>)+/, '' ).gsub( %r{<(/?)b[ \t\n\r]*>}, '<\\1strong>' )
62end
63
64def search_result
65        query = CGI::unescape( @cgi.params['q'][0] )
66        start = CGI::unescape( @cgi.params['start'][0] || '0' ).to_i
67
68        begin
69                uri = URI::parse( @conf.base_url )
70                q = "#{query} site:#{uri.host}"
71                q << %Q| inurl:"#{uri.path}"| unless uri.path == '/'
72                xml = search_boss_api( u( q.untaint ), start )
73                doc = REXML::Document::new( REXML::Source.new( xml ) ).root
74                res = doc.elements.to_a( '/ysearchresponse' )[0]
75                unless res.attribute( 'responsecode' ).value == '200' then
76                        return '<p class="message">ERROR</p>'
77                end
78        rescue OpenURI::HTTPError
79                return %Q|<p class="message">#$!</p>|
80        end
81
82        r = search_input_form( query )
83        r << '<dl class="search-result autopagerize_page_element">'
84        doc.elements.to_a( '*/result' ).each do |elem|
85                url = elem.elements.to_a( 'url' )[0].text
86                next unless url =~ @conf['search-yahoo.result_filter']
87                title = elem.elements.to_a( 'title' )[0].text
88                abstract = elem.elements.to_a( 'abstract' )[0].text
89                r << %Q|<dt><a href="#{h url}">#{search_to_html title}</a></dt>|
90                r << %Q|<dd>#{search_to_html abstract}</dd>|
91        end
92        r << '</dl>'
93
94        r << '<div class="search-navi">'
95        doc.elements.to_a( '/ysearchresponse/prevpage' ).each do |p|
96                if /start=\d+/ =~ p.text then
97                        r << %Q|<a href="#{@conf.index}?q=#{u query}&amp;#$&" rel="prev">&lt;前の20件</a>&nbsp;|
98                end
99        end
100
101        doc.elements.to_a( '/ysearchresponse/nextpage' ).each do |n|
102                if /start=\d+/ =~ n.text then
103                        r << %Q|<a href="#{@conf.index}?q=#{u query}&amp;#$&" rel="next">次の20件&gt;</a>|
104                end
105        end
106        r << '</div>'
107
108        r
109end
Note: See TracBrowser for help on using the browser.