]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/slashdot.rb
url plugin: add list of hosts for which no link info should be retrieved
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / slashdot.rb
1 require 'rexml/document'
2
3 class SlashdotPlugin < Plugin
4   include REXML
5   def help(plugin, topic="")
6     "slashdot search <string> [<max>=4] => search slashdot for <string>, slashdot [<max>=4] => return up to <max> slashdot headlines (use negative max to return that many headlines, but all on one line.)"
7   end
8   
9   def search_slashdot(m, params)
10     max = params[:limit].to_i
11     search = params[:search].to_s
12
13     xml = @bot.httputil.get("http://slashdot.org/search.pl?content_type=rss&query=#{CGI.escape(search)}")
14     unless xml
15       m.reply "search for #{search} failed"
16       return
17     end
18     debug xml.inspect
19     begin
20       doc = Document.new xml
21     rescue REXML::ParseException => e
22       warning e.inspect
23       m.reply "couldn't parse output XML: #{e.class}"
24       return
25     end
26     unless doc
27       m.reply "search for #{search} failed"
28       return
29     end
30     debug doc.inspect
31     max = 8 if max > 8
32     done = 0
33     doc.elements.each("*/item") {|e|
34       desc = e.elements["title"].text
35       desc.gsub!(/(.{150}).*/, '\1..')
36       reply = sprintf("%s | %s", e.elements["link"].text, desc.ircify_html)
37       m.reply reply
38       done += 1
39       break if done >= max
40     }
41     unless done > 0
42       m.reply "search for #{search} failed"
43     end
44   end
45   
46   def slashdot(m, params)
47     debug params.inspect
48     max = params[:limit].to_i
49     debug "max is #{max}"
50     xml = @bot.httputil.get('http://slashdot.org/slashdot.xml')
51     unless xml
52       m.reply "slashdot news parse failed"
53       return
54     end
55     doc = Document.new xml
56     unless doc
57       m.reply "slashdot news parse failed (invalid xml)"
58       return
59     end
60     done = 0
61     oneline = false
62     if max < 0
63       max = (0 - max)
64       oneline = true
65     end
66     max = 8 if max > 8
67     matches = Array.new
68     doc.elements.each("*/story") {|e|
69       matches << [ e.elements["title"].text, 
70                    e.elements["author"].text, 
71                    e.elements["time"].text.gsub(/\d{4}-(\d{2})-(\d{2})/, "\\2/\\1").gsub(/:\d\d$/, "") ]
72       done += 1
73       break if done >= max
74     } 
75     if oneline
76       m.reply matches.collect{|mat| mat[0]}.join(" | ")
77     else
78       matches.each {|mat|
79         m.reply sprintf("%36s | %8s | %8s", mat[0][0,36], mat[1][0,8], mat[2])
80       }
81     end
82   end
83 end
84 plugin = SlashdotPlugin.new
85 plugin.map 'slashdot search :limit *search', :action => 'search_slashdot',
86            :defaults => {:limit => 4}, :requirements => {:limit => /^-?\d+$/}
87 plugin.map 'slashdot :limit', :defaults => {:limit => 4},
88                               :requirements => {:limit => /^-?\d+$/}