]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - data/rbot/plugins/urban.rb
Show the page number again.
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / urban.rb
index dd275e2de7a995e1c90f3fdc7d7287d5a3dcc1a2..246251ecc3e614471581db14282bdc2a379c8c95 100644 (file)
@@ -1,26 +1,26 @@
 require 'cgi'
-require 'rubyful_soup'
+begin
+  require 'rubyful_soup'
+rescue
+  warning "could not load rubyful_soup, urban dictionary disabled"
+  warning "please get it from http://www.crummy.com/software/RubyfulSoup/"
+  warning "or install it via gem"
+  return
+end
 require 'uri/common'
 
 class UrbanPlugin < Plugin
 
   def help( plugin, topic="")
-    "~urban [word] [n]. Give the [n]th definition of [word] from urbandictionary.com."
+    "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
+
+    if m.params
+      paramArray = m.params.split(' ')
+      if paramArray.last.to_i != 0 
         definitionN = paramArray.last.to_i - 1
         query = m.params.chomp( paramArray.last )
         query.rstrip!
@@ -28,24 +28,25 @@ class UrbanPlugin < Plugin
         query = m.params
       end
       uri = URI.parse( "http://www.urbandictionary.com/define.php?term=#{ URI.escape query}" )
+    else 
+      uri = URI.parse( "http://www.urbandictionary.com/random.php" )
     end
 
-    soup = BeautifulSoup.new( @bot.httputil.get( uri ) )
+    soup = BeautifulSoup.new( @bot.httputil.get_cached( 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,'')
+      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}"
+        m.reply "\002#{title}\002 - #{output} (#{definitionN+1}/#{results.length})"
       else
         m.reply "#{query} does not have #{definitionN + 1} definitions."
       end
     else
       m.reply "#{m.params} not found."
     end
-
   end
 
   def strip_tags(html)