]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/freshmeat.rb
freshmeat plugin: use current API
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / freshmeat.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Freshmeat plugin for rbot
5
6 require 'rexml/document'
7
8 class FreshmeatPlugin < Plugin
9   include REXML
10
11   Config.register Config::StringValue.new('freshmeat.api_token',
12     :desc => "Auth token for freshmeat API requests. Without this, no freshmeat calls will be made. Find it in your freshmeat user account settings.",
13     :default => "")
14
15   def api_token
16     return @bot.config['freshmeat.api_token']
17   end
18
19   # Checks if an API token is configure, warns if not, returns true or false
20   def check_api_token(m=nil)
21     if api_token.empty?
22       if m
23         m.reply _("you must set the configuration value freshmeat.api_token to a valid freshmeat auth token, otherwise I cannot make requests to the site")
24       end
25       return false
26     end
27     return true
28   end
29
30   def help(plugin, topic="")
31     "freshmeat search [<max>=4] <string> => search freshmeat for <string>, freshmeat [<max>=4] => return up to <max> freshmeat headlines"
32   end
33
34   REL_ENTRY = %r{<a href="/(release)s/(\d+)/"><font color="#000000">(.*?)</font></a>}
35   PRJ_ENTRY = %r{<a href="/(project)s/(\S+?)/"><b>(.*?)</b></a>}
36
37   # This method defines a filter for fm pages. It's needed because the generic
38   # summarization grabs a comment, not the actual article.
39   #
40   def freshmeat_filter(s)
41     loc = Utils.check_location(s, /freshmeat\.net/)
42     return nil unless loc
43     entries = []
44     s[:text].scan(/#{REL_ENTRY}|#{PRJ_ENTRY}/) { |m|
45       entry = {
46         :type => ($1 || $4).dup,
47         :code => ($2 || $5).dup,
48         :name => ($3 || $6).dup
49       }
50       entries << entry
51     }
52     return nil if entries.empty?
53     title = s[:text].ircify_html_title
54     content = entries.inject([]) { |l, e| l << e[:name] }.join(" | ")
55     return {:title => title, :content => content}
56   end
57
58   def initialize
59     super
60     @bot.register_filter(:freshmeat, :htmlinfo) { |s| freshmeat_filter(s) }
61   end
62
63   def search_freshmeat(m, params)
64     return unless check_api_token(m)
65     max = params[:limit].to_i
66     search = params[:search].to_s
67     max = 8 if max > 8
68     xml = @bot.httputil.get("http://freshmeat.net/search.xml?auth_code=#{api_token}&q=#{CGI.escape(search)}")
69     unless xml
70       m.reply "search for #{search} failed (is the API token configured correctly?)"
71       return
72     end
73     doc = nil
74     begin
75       doc = Document.new xml
76     rescue
77       debug xml
78       error $!
79     end
80     unless doc
81       m.reply "search for #{search} failed"
82       return
83     end
84     matches = Array.new
85     max_width = 250
86     title_width = 0
87     url_width = 0
88     done = 0
89     doc.elements.each("hash/projects/project") {|e|
90       title = e.elements["name"].text
91       title_width = title.length if title.length > title_width
92
93       url = "http://freshmeat.net/projects/#{e.elements['permalink'].text}"
94       url_width = url.length if url.length > url_width
95
96       desc = e.elements["oneliner"].text
97
98       matches << [title, url, desc]
99       done += 1
100       break if done >= max
101     }
102     if matches.length == 0
103       m.reply "not found: #{search}"
104     end
105
106     title_width += 2 # for bold
107
108     matches.each {|mat|
109       title = Bold + mat[0] + Bold
110       url = mat[1]
111       desc = mat[2]
112       reply = sprintf("%s | %s | %s", title.ljust(title_width), url.ljust(url_width), desc)
113       m.reply reply, :overlong => :truncate
114     }
115   end
116
117   # We do manual parsing so that we can work even with the RSS plugin not loaded
118   def freshmeat(m, params)
119     max = params[:limit].to_i
120     max = 8 if max > 8
121     begin
122       xml = @bot.httputil.get('http://freshmeat.net/?format=atom')
123       unless xml
124         m.reply _("couldn't retrieve freshmeat news feed")
125         return
126       end
127       doc = Document.new xml
128       unless doc
129         m.reply "freshmeat news parse failed"
130         return
131       end
132     rescue
133       m.reply "freshmeat news parse failed"
134       return
135     end
136
137     matches = Array.new
138     max_width = 60
139     title_width = 0
140     done = 0
141     doc.elements.each("feed/entry") {|e|
142       # TODO desc should be replaced by the oneliner, but this means one more hit per project
143       # so we clip out all of the description and leave just the 'changes' part
144       desc = e.elements["content"].text.ircify_html.sub(/.*?#{Bold}Changes:#{Bold}/,'').strip
145       title = e.elements["title"].text.ircify_html.strip
146       title_width = title.length if title.length > title_width
147       matches << [title, desc]
148       done += 1
149       break if done >= max
150     }
151     title_width += 2
152     matches.each {|mat|
153       title = Bold + mat[0] + Bold
154       desc = mat[1]
155       reply = sprintf("%s | %s", title.ljust(title_width), desc)
156       m.reply reply, :overlong => :truncate
157     }
158   end
159 end
160 plugin = FreshmeatPlugin.new
161 plugin.map 'freshmeat search :limit *search', :action => 'search_freshmeat',
162             :defaults => {:limit => 4}, :requirements => {:limit => /^\d+$/}
163 plugin.map 'freshmeat :limit', :defaults => {:limit => 4},
164                                :requirements => {:limit => /^\d+$/}