]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/grouphug.rb
grouphug: fix retrieval of spurious confessions
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / grouphug.rb
index 889c4746e6a5d57c076c0c43cb63ca05243e648e..45083e1c02bd1d5ca4f3f56fc46eb679a411f4ea 100644 (file)
@@ -10,6 +10,7 @@
 # License:: GPL v2
 
 class GrouphugPlugin < Plugin
+  START = '<div id="main"'
   REG  = Regexp.new('<div class="content">\s*<p>(.*?)</p>\s+</div>', Regexp::MULTILINE)
   REGPOST = Regexp.new('title>(.*?) \| Group Hug')
   def initialize
@@ -18,7 +19,7 @@ class GrouphugPlugin < Plugin
   end
 
   def help( plugin, topic="" )
-    return _("Grouphug plugin. Confess! Usage: 'confess' for random confession, 'confess <number>' for specific one, 'confess <confession>' to share your own confession, but you must have create auth. Confessions must be at least 10 words.")
+    return _("Grouphug plugin. Confess! Usage: 'confess' for random confession, 'confess <number>' for specific one, 'confess <confession>' to share your own confession. Confessions must be at least 10 words.")
   end
 
   def post_confession(m, params)
@@ -55,15 +56,16 @@ class GrouphugPlugin < Plugin
         path = "confessions/#{params[:num]}"
         opts.delete(:cache)
         data = @bot.httputil.get("http://grouphug.us/#{path}", opts)
-
-        res = data.scan(REG)
-        confession = res[0][0].ircify_html
+        start = data.index(START)
+        res = data[start, data.length - start].scan(REG)
+        confession = res.first[0].ircify_html
         confession = "no confession ##{params[:num]} found" if confession.empty? and params[:num]
         m.reply confession
       else # Cache random confessions
         if @confessions.empty?
           data = @bot.httputil.get("http://grouphug.us/#{path}", opts)
-          res = data.scan(REG)
+          start = data.index(START)
+          res = data[start, data.length - start].scan(REG)
           res.each do |quote|
             @confessions << quote[0].ircify_html
           end
@@ -71,7 +73,8 @@ class GrouphugPlugin < Plugin
         confession = @confessions.pop
         m.reply confession
       end
-    rescue
+    rescue Exception => e
+      error e
       m.reply "failed to connect to grouphug.us"
     end
   end