]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/demauro.rb
Initial implementation of proper caching based on last-modified and etag HTTP headers
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / demauro.rb
1 require 'uri'\r
2 \r
3 DEMAURO_LEMMA = /<anchor>(.*?)(?: - (.*?))<go href="lemma.php\?ID=(\d+)"\/><\/anchor>/\r
4 class DeMauroPlugin < Plugin\r
5   def initialize\r
6     super\r
7     @dmurl = "http://www.demauroparavia.it/"\r
8     @wapurl = "http://wap.demauroparavia.it/"\r
9   end\r
10 \r
11 \r
12   def help(plugin, topic="")\r
13     return "demauro <word> => provides a link to the definition of the word from the Italian dictionary De Mauro/Paravia"\r
14   end\r
15 \r
16   def demauro(m, params)\r
17     parola = params[:parola].downcase\r
18     url = @wapurl + "index.php?lemma=#{URI.escape(parola)}"\r
19     xml = @bot.httputil.get_cached(url)\r
20     if xml.nil?\r
21       info = @bot.httputil.last_response\r
22       info = info ? "(#{info.code} - #{info.message})" : ""\r
23       m.reply "An error occurred while looking for #{parola}#{info}"\r
24       return\r
25     end\r
26     if xml=~ /Non ho trovato occorrenze per/\r
27       m.reply "Nothing found for #{parola}"\r
28       return\r
29     end\r
30     entries = xml.scan(DEMAURO_LEMMA)\r
31     text = parola\r
32     if !entries.assoc(parola) and !entries.assoc(parola.upcase)\r
33       text += " not found. Similar words"\r
34     end\r
35     text += ": "\r
36     text += entries[0...5].map { |ar|\r
37       "#{ar[0]} - #{ar[1].gsub(/<\/?em>/,'')}: #{@dmurl}#{ar[2]}"\r
38     }.join(" | ")\r
39     m.reply text\r
40   end\r
41 end\r
42 \r
43 plugin = DeMauroPlugin.new\r
44 plugin.map 'demauro :parola', :action => 'demauro'\r
45 \r