]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/urban.rb
Show the page number again.
[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     definitionN = 0
20
21     if m.params
22       paramArray = m.params.split(' ')
23       if paramArray.last.to_i != 0 
24         definitionN = paramArray.last.to_i - 1
25         query = m.params.chomp( paramArray.last )
26         query.rstrip!
27       else
28         query = m.params
29       end
30       uri = URI.parse( "http://www.urbandictionary.com/define.php?term=#{ URI.escape query}" )
31     else 
32       uri = URI.parse( "http://www.urbandictionary.com/random.php" )
33     end
34
35     soup = BeautifulSoup.new( @bot.httputil.get_cached( uri ) )
36     if titleNavi = soup.find_all( 'td', :attrs => { 'class' => 'def_word' } )[0] then
37       title = titleNavi.contents
38       results = soup.find_all( 'div', :attrs => { 'class' => 'def_p' } )
39       # debug PP.pp(results,'')
40       output = Array.new
41       if results[definitionN] then
42         results[definitionN].p.contents.each { |s| output.push( strip_tags( s.to_s ) ) }
43         m.reply "\002#{title}\002 - #{output} (#{definitionN+1}/#{results.length})"
44       else
45         m.reply "#{query} does not have #{definitionN + 1} definitions."
46       end
47     else
48       m.reply "#{m.params} not found."
49     end
50   end
51
52   def strip_tags(html)
53     html.gsub(/<.+?>/,'').
54     gsub(/&amp;/,'&').
55     gsub(/&quot;/,'"').
56     gsub(/&lt;/,'<').
57     gsub(/&gt;/,'>').
58     gsub(/&ellip;/,'...').
59     gsub(/&apos;/, "'").
60     gsub("\n",'')
61   end
62 end
63
64 plugin = UrbanPlugin.new
65 plugin.register( "urban" )