summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/urban.rb
blob: 246251ecc3e614471581db14282bdc2a379c8c95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require 'cgi'
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."
  end

  def privmsg( m )
    definitionN = 0

    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!
      else
        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_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,'')
      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} (#{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)
    html.gsub(/<.+?>/,'').
    gsub(/&amp;/,'&').
    gsub(/&quot;/,'"').
    gsub(/&lt;/,'<').
    gsub(/&gt;/,'>').
    gsub(/&ellip;/,'...').
    gsub(/&apos;/, "'").
    gsub("\n",'')
  end
end

plugin = UrbanPlugin.new
plugin.register( "urban" )