]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/slashdot.rb
rearrange repo for packaging
[user/henk/code/ruby/rbot.git] / data / 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     begin
37       doc = Document.new xml
38     rescue REXML::ParseException => e
39       puts e
40       m.reply "couldn't parse output XML: #{e.class}"
41       return
42     end
43     unless doc
44       m.reply "search for #{search} failed"
45       return
46     end
47     max = 8 if max > 8
48     done = 0
49     doc.elements.each("*/item") {|e|
50       desc = e.elements["title"].text
51       desc.gsub!(/(.{150}).*/, '\1..')
52       reply = sprintf("%s | %s", e.elements["link"].text, desc)
53       m.reply reply
54       done += 1
55       break if done >= max
56     }
57   end
58   
59   def slashdot(m, max=4)
60     xml = @bot.httputil.get(URI.parse("http://slashdot.org/slashdot.xml"))
61     unless xml
62       m.reply "slashdot news parse failed"
63       return
64     end
65     doc = Document.new xml
66     unless doc
67       m.reply "slashdot news parse failed (invalid xml)"
68       return
69     end
70     done = 0
71     oneline = false
72     if max < 0
73       max = (0 - max)
74       oneline = true
75     end
76     max = 8 if max > 8
77     matches = Array.new
78     doc.elements.each("*/story") {|e|
79       matches << [ e.elements["title"].text, 
80                    e.elements["author"].text, 
81                    e.elements["time"].text.gsub(/\d{4}-(\d{2})-(\d{2})/, "\\2/\\1").gsub(/:\d\d$/, "") ]
82       done += 1
83       break if done >= max
84     } 
85     if oneline
86       m.reply matches.collect{|mat| mat[0]}.join(" | ")
87     else
88       matches.each {|mat|
89         m.reply sprintf("%36s | %8s | %8s", mat[0][0,36], mat[1][0,8], mat[2])
90       }
91     end
92   end
93 end
94 plugin = SlashdotPlugin.new
95 plugin.register("slashdot")