]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - rbot/plugins/slashdot.rb
Wed Jul 20 23:30:01 BST 2005 Tom Gilbert <tom@linuxbrit.co.uk>
[user/henk/code/ruby/rbot.git] / rbot / plugins / slashdot.rb
1 require 'rexml/document'
2 require 'uri/common'
3
4 class SlashdotPlugin < Plugin
5   include REXML
6   def help(plugin, topic="")
7     "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.)"
8   end
9   def privmsg(m)
10     if m.params && m.params =~ /^search\s+(.*)\s+(\d+)$/
11       search = $1
12       limit = $2.to_i
13       search_slashdot m, search, limit
14     elsif m.params && m.params =~ /^search\s+(.*)$/
15       search = $1
16       search_slashdot m, search
17     elsif m.params && m.params =~ /^([-\d]+)$/
18       limit = $1.to_i
19       slashdot m, limit
20     else
21       slashdot m
22     end
23   end
24   
25   def search_slashdot(m, search, max=4)
26     begin
27       xml = @bot.httputil.get(URI.parse("http://slashdot.org/search.pl?content_type=rss&query=#{URI.escape(search)}"))
28     rescue URI::InvalidURIError, URI::BadURIError => e
29       m.reply "illegal search string #{search}"
30       return
31     end
32     unless xml
33       m.reply "search for #{search} failed"
34       return
35     end
36     doc = Document.new xml
37     unless doc
38       m.reply "search for #{search} failed"
39       return
40     end
41     max = 8 if max > 8
42     done = 0
43     doc.elements.each("*/item") {|e|
44       desc = e.elements["title"].text
45       desc.gsub!(/(.{150}).*/, '\1..')
46       reply = sprintf("%s | %s", e.elements["link"].text, desc)
47       m.reply reply
48       done += 1
49       break if done >= max
50     }
51   end
52   
53   def slashdot(m, max=4)
54     xml = @bot.httputil.get(URI.parse("http://slashdot.org/slashdot.xml"))
55     unless xml
56       m.reply "slashdot news parse failed"
57       return
58     end
59     doc = Document.new xml
60     unless doc
61       m.reply "slashdot news parse failed"
62       return
63     end
64     done = 0
65     oneline = false
66     if max < 0
67       max = (0 - max)
68       oneline = true
69     end
70     max = 8 if max > 8
71     matches = Array.new
72     doc.elements.each("*/story") {|e|
73       matches << [ e.elements["title"].text, 
74                    e.elements["author"].text, 
75                    e.elements["time"].text.gsub(/\d{4}-(\d{2})-(\d{2})/, "\\2/\\1").gsub(/:\d\d$/, "") ]
76       done += 1
77       break if done >= max
78     } 
79     if oneline
80       m.reply matches.collect{|mat| mat[0]}.join(" | ")
81     else
82       matches.each {|mat|
83         m.reply sprintf("%36s | %8s | %8s", mat[0][0,36], mat[1][0,8], mat[2])
84       }
85     end
86   end
87 end
88 plugin = SlashdotPlugin.new
89 plugin.register("slashdot")