lastfm plugin: added now playing info to the help
[rbot] / 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   def help(plugin, topic="")
11     "freshmeat search [<max>=4] <string> => search freshmeat for <string>, freshmeat [<max>=4] => return up to <max> freshmeat headlines"
12   end
13
14   REL_ENTRY = %r{<a href="/(release)s/(\d+)/"><font color="#000000">(.*?)</font></a>}
15   PRJ_ENTRY = %r{<a href="/(project)s/(\S+?)/"><b>(.*?)</b></a>}
16
17   # This method defines a filter for fm pages. It's needed because the generic
18   # summarization grabs a comment, not the actual article.
19   #
20   def freshmeat_filter(s)
21     loc = Utils.check_location(s, /freshmeat\.net/)
22     return nil unless loc
23     entries = []
24     s[:text].scan(/#{REL_ENTRY}|#{PRJ_ENTRY}/) { |m|
25       entry = {
26         :type => ($1 || $4).dup,
27         :code => ($2 || $5).dup,
28         :name => ($3 || $6).dup
29       }
30       entries << entry
31     }
32     return nil if entries.empty?
33     title = s[:text].ircify_html_title
34     content = entries.inject([]) { |l, e| l << e[:name] }.join(" | ")
35     return {:title => title, :content => content}
36   end
37
38   def initialize
39     super
40     @bot.register_filter(:freshmeat, :htmlinfo) { |s| freshmeat_filter(s) }
41   end
42
43   def search_freshmeat(m, params)
44     max = params[:limit].to_i
45     search = params[:search].to_s
46     max = 8 if max > 8
47     xml = @bot.httputil.get("http://freshmeat.net/search-xml/?orderby=locate_projectname_full_DESC&q=#{CGI.escape(search)}")
48     unless xml
49       m.reply "search for #{search} failed"
50       return
51     end
52     doc = Document.new xml
53     unless doc
54       m.reply "search for #{search} failed"
55       return
56     end
57     matches = Array.new
58     max_width = 250
59     title_width = 0
60     url_width = 0
61     done = 0
62     doc.elements.each("*/match") {|e|
63       name = e.elements["projectname_short"].text
64       url = "http://freshmeat.net/projects/#{name}/"
65       desc = e.elements["desc_short"].text
66       title = e.elements["projectname_full"].text
67       #title_width = title.length if title.length > title_width
68       url_width = url.length if url.length > url_width
69       matches << [title, url, desc]
70       done += 1
71       break if done >= max
72     }
73     if matches.length == 0
74       m.reply "not found: #{search}"
75     end
76     matches.each {|mat|
77       title = mat[0]
78       url = mat[1]
79       desc = mat[2]
80       desc.gsub!(/(.{#{max_width - 3 - url_width}}).*/, '\1..')
81       reply = sprintf("%s | %s", url.ljust(url_width), desc)
82       m.reply reply
83     }
84   end
85   
86   def freshmeat(m, params)
87     max = params[:limit].to_i
88     max = 8 if max > 8
89     begin
90       xml = @bot.httputil.get('http://images.feedstermedia.com/feedcache/ostg/freshmeat/fm-releases-global.xml')
91       unless xml
92         m.reply "freshmeat news parse failed"
93         return
94       end
95       doc = Document.new xml
96       unless doc
97         m.reply "freshmeat news parse failed"
98         return
99       end
100     rescue
101       m.reply "freshmeat news parse failed"
102       return
103     end
104
105     matches = Array.new
106     max_width = 60
107     title_width = 0
108     done = 0
109     doc.elements.each("*/channel/item") {|e|
110       desc = e.elements["description"].text
111       title = e.elements["title"].text
112       #title.gsub!(/\s+\(.*\)\s*$/, "")
113       title.strip!
114       title_width = title.length if title.length > title_width
115       matches << [title, desc]
116       done += 1
117       break if done >= max
118     }
119     matches.each {|mat|
120       title = mat[0]
121       #desc = mat[1]
122       #desc.gsub!(/(.{#{max_width - 3 - title_width}}).*/, '\1..')
123       #reply = sprintf("%#{title_width}s | %s", title, desc)
124       m.reply title
125     }
126   end
127 end
128 plugin = FreshmeatPlugin.new
129 plugin.map 'freshmeat search :limit *search', :action => 'search_freshmeat',
130             :defaults => {:limit => 4}, :requirements => {:limit => /^\d+$/}
131 plugin.map 'freshmeat :limit', :defaults => {:limit => 4}, 
132                                :requirements => {:limit => /^\d+$/}