]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - rbot/plugins/freshmeat.rb
Wed Jul 20 23:30:01 BST 2005 Tom Gilbert <tom@linuxbrit.co.uk>
[user/henk/code/ruby/rbot.git] / rbot / plugins / freshmeat.rb
1 require 'rexml/document'
2 require 'uri/common'
3
4 class FreshmeatPlugin < Plugin
5   include REXML
6   def help(plugin, topic="")
7     "freshmeat search <string> [<max>=4] => search freshmeat for <string>, freshmeat [<max>=4] => return up to <max> freshmeat headlines"
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_freshmeat m, search, limit
14     elsif m.params && m.params =~ /^search\s+(.*)$/
15       search = $1
16       search_freshmeat m, search
17     elsif m.params && m.params =~ /^(\d+)$/
18       limit = $1.to_i
19       freshmeat m, limit
20     else
21       freshmeat m
22     end
23   end
24   
25   def search_freshmeat(m, search, max=4)
26     max = 8 if max > 8
27     begin
28       xml = @bot.httputil.get(URI.parse("http://freshmeat.net/search-xml/?orderby=locate_projectname_full_DESC&q=#{URI.escape(search)}"))
29     rescue URI::InvalidURIError, URI::BadURIError => e
30       m.reply "illegal search string #{search}"
31       return
32     end
33     unless xml
34       m.reply "search for #{search} failed"
35       return
36     end
37     doc = Document.new xml
38     unless doc
39       m.reply "search for #{search} failed"
40       return
41     end
42     matches = Array.new
43     max_width = 250
44     title_width = 0
45     url_width = 0
46     done = 0
47     doc.elements.each("*/match") {|e|
48       name = e.elements["projectname_short"].text
49       url = "http://freshmeat.net/projects/#{name}/"
50       desc = e.elements["desc_short"].text
51       title = e.elements["projectname_full"].text
52       #title_width = title.length if title.length > title_width
53       url_width = url.length if url.length > url_width
54       matches << [title, url, desc]
55       done += 1
56       break if done >= max
57     }
58     if matches.length == 0
59       m.reply "not found: #{search}"
60     end
61     matches.each {|mat|
62       title = mat[0]
63       url = mat[1]
64       desc = mat[2]
65       desc.gsub!(/(.{#{max_width - 3 - url_width}}).*/, '\1..')
66       reply = sprintf("%s | %s", url.ljust(url_width), desc)
67       m.reply reply
68     }
69   end
70   
71   def freshmeat(m, max=4)
72     max = 8 if max > 8
73     xml = @bot.httputil.get(URI.parse("http://images.feedstermedia.com/feedcache/ostg/freshmeat/fm-releases-global.xml"))
74     unless xml
75       m.reply "freshmeat news parse failed"
76       return
77     end
78     doc = Document.new xml
79     unless doc
80       m.reply "freshmeat news parse failed"
81       return
82     end
83     matches = Array.new
84     max_width = 60
85     title_width = 0
86     done = 0
87     doc.elements.each("*/channel/item") {|e|
88       desc = e.elements["description"].text
89       title = e.elements["title"].text
90       #title.gsub!(/\s+\(.*\)\s*$/, "")
91       title.strip!
92       title_width = title.length if title.length > title_width
93       matches << [title, desc]
94       done += 1
95       break if done >= max
96     }
97     matches.each {|mat|
98       title = mat[0]
99       #desc = mat[1]
100       #desc.gsub!(/(.{#{max_width - 3 - title_width}}).*/, '\1..')
101       #reply = sprintf("%#{title_width}s | %s", title, desc)
102       m.reply title
103     }
104   end
105 end
106 plugin = FreshmeatPlugin.new
107 plugin.register("freshmeat")