]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - lib/rbot/plugins/freshmeat.rb
New auth framework is now backwards compatible: auth <masterpassword> works again...
[user/henk/code/ruby/rbot.git] / lib / 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     xml = @bot.httputil.get(URI.parse("http://images.feedstermedia.com/feedcache/ostg/freshmeat/fm-releases-global.xml"))
62     unless xml
63       m.reply "freshmeat news parse failed"
64       return
65     end
66     doc = Document.new xml
67     unless doc
68       m.reply "freshmeat news parse failed"
69       return
70     end
71     matches = Array.new
72     max_width = 60
73     title_width = 0
74     done = 0
75     doc.elements.each("*/channel/item") {|e|
76       desc = e.elements["description"].text
77       title = e.elements["title"].text
78       #title.gsub!(/\s+\(.*\)\s*$/, "")
79       title.strip!
80       title_width = title.length if title.length > title_width
81       matches << [title, desc]
82       done += 1
83       break if done >= max
84     }
85     matches.each {|mat|
86       title = mat[0]
87       #desc = mat[1]
88       #desc.gsub!(/(.{#{max_width - 3 - title_width}}).*/, '\1..')
89       #reply = sprintf("%#{title_width}s | %s", title, desc)
90       m.reply title
91     }
92   end
93 end
94 plugin = FreshmeatPlugin.new
95 plugin.map 'freshmeat search :limit *search', :action => 'search_freshmeat',
96             :defaults => {:limit => 4}, :requirements => {:limit => /^\d+$/}
97 plugin.map 'freshmeat :limit', :defaults => {:limit => 4}, 
98                                :requirements => {:limit => /^\d+$/}