summaryrefslogtreecommitdiff
path: root/data/rbot
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2006-07-23 14:23:47 +0000
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2006-07-23 14:23:47 +0000
commitb7d2b095520d8951eb41ebd9c7794889169e5228 (patch)
treed3c277e5b04b17fe8887ff54bd1d2bdeba49dc46 /data/rbot
parent6bc43542c5f660cb8ba1bcaa1081e7c1aee7e26d (diff)
Urban dictionary plugin, contributed by eean (#105)
Diffstat (limited to 'data/rbot')
-rw-r--r--data/rbot/plugins/urban.rb64
1 files changed, 64 insertions, 0 deletions
diff --git a/data/rbot/plugins/urban.rb b/data/rbot/plugins/urban.rb
new file mode 100644
index 00000000..dd275e2d
--- /dev/null
+++ b/data/rbot/plugins/urban.rb
@@ -0,0 +1,64 @@
+require 'cgi'
+require 'rubyful_soup'
+require 'uri/common'
+
+class UrbanPlugin < Plugin
+
+ def help( plugin, topic="")
+ "~urban [word] [n]. Give the [n]th definition of [word] from urbandictionary.com."
+ end
+
+ def privmsg( m )
+
+ unless(m.params && m.params.length > 0)
+ m.reply "incorrect usage: " + help(m.plugin)
+ return
+ end
+
+ paramArray = m.params.split(' ')
+ definitionN = 0
+ if m.params == 'random' then
+ uri = URI.parse( "http://www.urbandictionary.com/random.php" )
+ else
+ if( paramArray.last.to_i != 0 ) then
+ definitionN = paramArray.last.to_i - 1
+ query = m.params.chomp( paramArray.last )
+ query.rstrip!
+ else
+ query = m.params
+ end
+ uri = URI.parse( "http://www.urbandictionary.com/define.php?term=#{ URI.escape query}" )
+ end
+
+ soup = BeautifulSoup.new( @bot.httputil.get( uri ) )
+ if titleNavi = soup.find_all( 'td', :attrs => { 'class' => 'def_word' } )[0] then
+ title = titleNavi.contents
+ results = soup.find_all( 'div', :attrs => { 'class' => 'def_p' } )
+ debug PP.pp(results,'')
+ output = Array.new
+ if results[definitionN] then
+ results[definitionN].p.contents.each { |s| output.push( strip_tags( s.to_s ) ) }
+ m.reply "\002#{title}\002 - #{output}"
+ else
+ m.reply "#{query} does not have #{definitionN + 1} definitions."
+ end
+ else
+ m.reply "#{m.params} not found."
+ end
+
+ end
+
+ def strip_tags(html)
+ html.gsub(/<.+?>/,'').
+ gsub(/&amp;/,'&').
+ gsub(/&quot;/,'"').
+ gsub(/&lt;/,'<').
+ gsub(/&gt;/,'>').
+ gsub(/&ellip;/,'...').
+ gsub(/&apos;/, "'").
+ gsub("\n",'')
+ end
+end
+
+plugin = UrbanPlugin.new
+plugin.register( "urban" )