summaryrefslogtreecommitdiff
path: root/data/rbot
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2009-02-08 14:54:31 +0100
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2009-02-08 14:54:31 +0100
commit2e33012eb5eb8cf121059d06f07f6950141deccc (patch)
tree5c0ce1d65fd54314599db8569d1e0ecfb1e5876e /data/rbot
parent3dc53f61fcb7fae58c2259bc88d2a884e74b9331 (diff)
grouphug: refactor confession retrieval
Turn the confession extraction into its own method, and use it both for specific and random confession retrieval.
Diffstat (limited to 'data/rbot')
-rw-r--r--data/rbot/plugins/grouphug.rb31
1 files changed, 19 insertions, 12 deletions
diff --git a/data/rbot/plugins/grouphug.rb b/data/rbot/plugins/grouphug.rb
index 45083e1c..fa35803c 100644
--- a/data/rbot/plugins/grouphug.rb
+++ b/data/rbot/plugins/grouphug.rb
@@ -46,6 +46,15 @@ class GrouphugPlugin < Plugin
end
end
+ def get_confessions(html)
+ return [] unless html
+ start = html.index(START)
+ res = html[start, html.length - start].scan(REG)
+ return [] unless res
+ return res.map { |quote|
+ quote[0].ircify_html
+ }
+ end
def confess(m, params)
opts = { :cache => false }
@@ -56,22 +65,20 @@ class GrouphugPlugin < Plugin
path = "confessions/#{params[:num]}"
opts.delete(:cache)
data = @bot.httputil.get("http://grouphug.us/#{path}", opts)
- 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
+ confessions = get_confessions(data)
+ if confessions.length > 1
+ warn "more than one confession found!"
+ warn confessions
+ end
+ confessions << "no confession ##{params[:num]} found" if confessions.empty?
+ m.reply confessions.first
else # Cache random confessions
if @confessions.empty?
data = @bot.httputil.get("http://grouphug.us/#{path}", opts)
- start = data.index(START)
- res = data[start, data.length - start].scan(REG)
- res.each do |quote|
- @confessions << quote[0].ircify_html
- end
+ @confessions.replace get_confessions(data)
end
- confession = @confessions.pop
- m.reply confession
+ @confessions << "no confessions found!" if @confessions.empty?
+ m.reply @confessions.pop
end
rescue Exception => e
error e