]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/grouphug.rb
* plugins/grouphug: cache the results from a page request, don't re-request until...
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / grouphug.rb
1 #-- vim:sw=2:et
2 #++
3 #
4 # :title: Grouphug Plugin for rbot
5 #
6 # Author:: Mark Kretschmann <markey@web.de>
7 # Author:: Casey Link <unnamedrambler@gmail.com>
8 # Copyright:: (C) 2005 Mark Kretschmann
9 # Copyright:: (C) 2008 Casey Link
10 # License:: GPL v2
11
12 class GrouphugPlugin < Plugin
13   def initialize
14     super
15     @confessions = Array.new
16   end
17
18   def help( plugin, topic="" )
19     return "Grouphug plugin. Confess! Usage: 'confess' for random confession, 'confess <number>' for specific one."
20   end
21
22   def confess(m, params)
23     opts = { :cache => false }
24     path = "random"
25     begin
26       # Fetch a specific question - separate from cache
27       if params[:num]
28         path = "confessions/#{params[:num]}"
29         opts.delete(:cache)
30         data = @bot.httputil.get("http://grouphug.us/#{path}", opts)
31
32         reg = Regexp.new('<div class="content">.*?<p>(.*?)</p>', Regexp::MULTILINE)
33         res = data.scan(reg)
34         confession = res[0][0].ircify_html
35         confession = "no confession ##{params[:num]} found" if confession.empty? and params[:num]
36         m.reply confession
37       else # Cache random confessions
38         if @confessions.empty?
39           data = @bot.httputil.get("http://grouphug.us/#{path}", opts)
40           reg = Regexp.new('<div class="content">.*?<p>(.*?)</p>', Regexp::MULTILINE)
41           res = data.scan(reg)
42           res.each do |quote|
43             @confessions << quote[0].ircify_html
44           end
45         end
46         confession = @confessions.pop
47         m.reply confession
48       end
49     rescue
50       m.reply "failed to connect to grouphug.us"
51     end
52   end
53 end
54
55
56 plugin = GrouphugPlugin.new
57
58 plugin.map "grouphug [:num]",
59   :thread => true, :action => :confess, :requirements => { :num => /\d+/ }
60 plugin.map "confess [:num]",
61   :thread => true, :action => :confess, :requirements => { :num => /\d+/ }
62