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

Revision 16036, 2.4 kB (checked in by sho, 5 months ago)

platform/tdiary/plugin/search-yahoo.rb: added search plugin sample using Yahoo! Search BOSS API.

Line 
1#
2# search-yahoo.rb - site search plugin sample using Yahoo! Search BOSS API.
3#
4# Copyright (C) 2008, TADA Tadashi <sho@spc.gr.jp>
5# You can redistribute it and/or modify it under GPL.
6#
7# Needed these options below:
8#
9# @options['search-yahoo.appid'] : Your BOSS APPID
10# @options['search-yahoo.result_filter'] : your dialy's URL format of DAY mode into Regexp.
11#
12
13require 'open-uri'
14require 'timeout'
15require 'rexml/document'
16
17def search_title
18        '全文検索 by Yahoo! Search BOSS'
19end
20
21def search_input_form( q )
22        r = <<-HTML
23                <div>
24                        <form method="GET" action="#{@conf.index}">
25                        検索キーワード:
26                        <input name="q" value="#{h q}">
27                        <input type="submit" value="OK">
28                        </form>
29                </div>
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=50&start=#{start}"
38
39        proxy = @conf['proxy']
40        proxy = 'http://' + proxy if proxy
41        timeout( 10 ) do
42                open( url, :proxy => proxy ) {|f| f.read }
43        end
44end
45
46def search_result
47        query = CGI::unescape( @cgi.params['q'][0] )
48        start = CGI::unescape( @cgi.params['start'][0] || '0' ).to_i
49
50        begin
51                uri = URI::parse( @conf.base_url )
52                q = "#{query} site:#{uri.host}"
53                q << " inurl:#{uri.path}" unless uri.path == '/'
54                xml = search_boss_api( u( q.untaint ), start )
55                doc = REXML::Document::new( xml ).root
56                res = doc.elements.to_a( '/ysearchresponse' )[0]
57                unless res.attribute( 'responsecode' ).value == '200' then
58                        return '<p class="message">ERROR</p>'
59                end
60        rescue OpenURI::HTTPError
61                return %Q|<p class="message">#$!</p>|
62        end
63
64        r = search_input_form( query )
65        r << '<dl class="search-result">'
66        doc.elements.to_a( '*/result' ).each do |elem|
67                url = elem.elements.to_a( 'url' )[0].text
68                next unless url =~ @conf['search-yahoo.result_filter']
69                title = elem.elements.to_a( 'title' )[0].text
70                abstract = elem.elements.to_a( 'abstract' )[0].text
71                r << %Q|<dt><a href="#{h url}">#{title}</a></dt>|
72                r << %Q|<dd>#{abstract}</dd>|
73        end
74        r << '</dl>'
75
76        r << '<div class="search-navi">'
77        doc.elements.to_a( '/ysearchresponse/prevpage' ).each do |p|
78                if /start=\d+/ =~ p.text then
79                        r << %Q|<a href="#{@conf.index}?q=#{u query}&#$&">&lt;前の50件</a>&nbsp;|
80                end
81        end
82
83        doc.elements.to_a( '/ysearchresponse/nextpage' ).each do |n|
84                if /start=\d+/ =~ n.text then
85                        r << %Q|<a href="#{@conf.index}?q=#{u query}&#$&">次の50件&gt;</a>|
86                end
87        end
88        r << '</div>'
89
90        r
91end
Note: See TracBrowser for help on using the browser.