]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/urban.rb
Adapt to new auth system.
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / urban.rb
1 require 'cgi'
2 begin
3   require 'rubyful_soup'
4 rescue
5   warning "could not load rubyful_soup, urban dictionary disabled"
6   warning "please get it from http://www.crummy.com/software/RubyfulSoup/"
7   warning "or install it via gem"
8   return
9 end
10 require 'uri/common'
11
12 class UrbanPlugin < Plugin
13
14   def help( plugin, topic="")
15     "urban [word] [n]. Give the [n]th definition of [word] from urbandictionary.com."
16   end
17
18   def privmsg( m )
19
20     unless(m.params && m.params.length > 0)
21       m.reply "incorrect usage: " + help(m.plugin)
22       return
23     end
24
25     paramArray = m.params.split(' ')
26     definitionN = 0
27     if m.params == 'random' then
28       uri = URI.parse( "http://www.urbandictionary.com/random.php" )
29     else 
30       if( paramArray.last.to_i != 0 ) then
31         definitionN = paramArray.last.to_i - 1
32         query = m.params.chomp( paramArray.last )
33         query.rstrip!
34       else
35         query = m.params
36       end
37       uri = URI.parse( "http://www.urbandictionary.com/define.php?term=#{ URI.escape query}" )
38     end
39
40     soup = BeautifulSoup.new( @bot.httputil.get_cached( uri ) )
41     if titleNavi = soup.find_all( 'td', :attrs => { 'class' => 'def_word' } )[0] then
42       title = titleNavi.contents
43       results = soup.find_all( 'div', :attrs => { 'class' => 'def_p' } )
44       # debug PP.pp(results,'')
45       output = Array.new
46       if results[definitionN] then
47         results[definitionN].p.contents.each { |s| output.push( strip_tags( s.to_s ) ) }
48         m.reply "\002#{title}\002 - #{output}"
49       else
50         m.reply "#{query} does not have #{definitionN + 1} definitions."
51       end
52     else
53       m.reply "#{m.params} not found."
54     end
55
56   end
57
58   def strip_tags(html)
59     html.gsub(/<.+?>/,'').
60     gsub(/&amp;/,'&').
61     gsub(/&quot;/,'"').
62     gsub(/&lt;/,'<').
63     gsub(/&gt;/,'>').
64     gsub(/&ellip;/,'...').
65     gsub(/&apos;/, "'").
66     gsub("\n",'')
67   end
68 end
69
70 plugin = UrbanPlugin.new
71 plugin.register( "urban" )