Ticket #40: urban.rb

File urban.rb, 2.1 kB (added by broderson@gmail.com, 3 years ago)

urban dictionary module

Line 
1 # UrbanDictionary plugin for RubyBot
2 # (c) 2005 Dayne Broderson <broderson@gmail.com>
3 # bish0p in #modzer0 on irc.freenode.net
4 # Licensed under MIT License.
5
6 class UrbanPlugin < Plugin
7         require 'soap/rpc/driver'
8         def initialize
9                 super
10                 @key =  @bot.config['urban']
11                 @soap = SOAP::RPC::Driver.new('http://api.urbandictionary.com/soap')
12                 @soap.add_method('lookup', 'key', 'term')
13                 # returns an integer, the number of published definitions for that word
14                 @soap.add_method('count_definitions', 'key', 'term')
15                 # returns a list of words that surround the parameter in alphabetical order
16                 @soap.add_method('nearby', 'key', 'term')
17                 # returns Urban Word of the Day
18                 @soap.add_method('get_daily_word')
19         end
20
21   def help(plugin, topic="")
22     "urban => look up a word in urban dictionary\n" +
23                 "      no options gives word of the day\n" +
24                 "      a number provides that specific definition of the last word looked up"
25   end
26
27   def privmsg(m)
28                 if not @key
29                         m.reply "'urban' key is not set in ~/.rbot.conf.yaml"
30                         m.reply "get one at http://www.urbandictionary.com/api.php"
31                         return
32                 end
33                 if m.params
34                         urban m, m.params
35                 else
36                         urban m
37                 end
38   end
39  
40   def urban(m, term=nil)
41                 if term.nil?
42                         # slurp down the term of the day
43                         daily = @soap.get_daily_word
44                         term = daily.word
45                         m.reply "UrbanDictionary Word of the Day is: #{term}"
46                         result = @soap.lookup(@key, term)
47                 elsif term =~ /(\d+)$/
48                         num = $1
49                         term = @registry['urban.last']
50                         m.reply "Getting definition #{num} of #{term}"
51                         result = @soap.lookup(@key, term)
52                         i = num.to_i - 1
53                 else
54                         # slurp down the term asked for
55                         result = @soap.lookup(@key,term)
56                 end
57
58                 unless result
59                         m.reply "urban lookup failed"
60                         return
61                 end
62                        
63                 if result.size < 1
64                         m.reply "no result found for '#{term}'"
65                         return
66                 end
67
68                 @registry["urban.last"] = "#{term}"
69                 i = rand(result.size) if not i
70                 out = "(#{i+1}/#{result.size}): #{result[i].definition}"
71                 if result[i].author
72                         out  = out + "\n - #{result[i].author}"
73                 else
74                         out = "definition # #{i} doesn't exist"
75                 end
76                 m.reply out
77   end
78 end
79 plugin = UrbanPlugin.new
80 plugin.register("urban")