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