]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/urban.rb
ircbot: do not use debug in sendmsg() because it breaks script. script plugin: handle...
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / urban.rb
1 class UrbanPlugin < Plugin
2
3   def help( plugin, topic="")
4     "urban [word] [n]: give the [n]th definition of [word] from urbandictionary.com. urbanday: give the word-of-the-day at urban"
5   end
6
7   def urban(m, params)
8     words = params[:words].to_s
9     n = params[:n].nil? ? 1 : params[:n].to_i rescue 1
10
11     if words.empty?
12       resp = @bot.httputil.head('http://www.urbandictionary.com/random.php',
13                                :max_redir => -1)
14       if resp.code == "302" && (loc = resp['location'])
15         words = URI.unescape(loc.match(/define.php\?term=(.*)$/)[1]) rescue nil
16       end
17     end
18     # we give a very high 'skip' because this will allow us to get the number of definitions by retrieving the previous definition
19     uri = "http://www.urbanwap.com/search.php?term=#{CGI.escape words}&skip=65536"
20     page = @bot.httputil.get(uri)
21     if page.nil?
22       m.reply "Couldn't retrieve an urban dictionary definition of #{words}"
23       return
24     end
25     if page =~ / is undefined<\/card><\/wml>/
26       m.reply "There is no urban dictionary definition of #{words}"
27       return
28     end
29     if page =~ /&amp;skip=(\d+)">prev<\/a>/
30       numdefs = $1.to_i + 1
31     else
32       numdefs = 1
33     end
34     n = numdefs + n + 1 if n < 0
35     if n > numdefs
36       m.reply "Urban dictionary only has #{numdefs} definitions for '#{words}'"
37       n = numdefs
38     end
39     if n < numdefs
40       uri = "http://www.urbanwap.com/search.php?term=#{CGI.escape words}&skip=#{n-1}"
41       page = @bot.httputil.get(uri)
42       if page.nil?
43         case n % 10
44         when 1
45           ord = 'st'
46         when 2
47           ord = 'nd'
48         when 3
49           ord = 'rd'
50         else
51           ord = 'th'
52         end
53         m.reply "Couldn't retrieve the #{n}#{ord} urban dictionary definition of #{words}"
54         return
55       end
56     end
57     m.reply "#{get_def(page)} (#{n}/#{numdefs})"
58   end
59
60   def get_def(text)
61     # Start by removing the prev/home/next links
62     t = text.gsub(/(?:<a href[^>]*>prev<\/a> )?<a href[^>]*>home<\/a>(?: <a href[^>]*>next<\/a>)?/,'')
63     # Close up paragraphs
64     t.gsub!(/<\/?p>/, ' ')
65     t.gsub!("\n", ' ')
66     # Reverse headings
67     t.gsub!(/<\/?b>/,"#{Reverse}")
68     # Enbolden links
69     t.gsub!(/<\/?a(?: [^>]*)?>/,"#{Bold}")
70     # Reverse examples
71     t.gsub!(/<\/?(?:i|em)>/,"#{Underline}")
72     # Clear anything else
73     t.gsub!(/<.*?>/, '')
74
75     Utils.decode_html_entities t.strip
76   end
77
78   def uotd(m, params)
79     home = @bot.httputil.get("http://www.urbanwap.com/")
80     if home.nil?
81       m.reply "Couldn't get the urban dictionary word of the day"
82       return
83     end
84     home.match(/Word of the Day: <a href="(.*?)">.*?<\/a>/)
85     wotd = $1
86     debug "Urban word of the day: #{wotd}"
87     page = @bot.httputil.get(wotd)
88     if page.nil?
89       m.reply "Couldn't get the urban dictionary word of the day"
90     else
91       m.reply get_def(page)
92     end
93   end
94 end
95
96 plugin = UrbanPlugin.new
97 plugin.map "urban *words :n", :requirements => { :n => /^-?\d+$/ }, :action => 'urban'
98 plugin.map "urban [*words]", :action => 'urban'
99 plugin.map "urbanday", :action => 'uotd'
100