summaryrefslogtreecommitdiff
path: root/data/rbot/plugins/demauro.rb
blob: 7e5705a1e3596280695cdb2c7764c477d033f64c (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# TODO: cache definitions

require 'erb'

class DeMauroPlugin < Plugin
  include ERB::Util

  def initialize
    super
    @dmurl = "http://www.demauroparavia.it/"
  end


  def help(plugin, topic="")
    return "demauro <parola> => fornisce il link della definizione della parola dal dizionario De Mauro/Paravia"
  end

  def demauro(m, params)
    parola = params[:parola]
    url = @dmurl + "cerca?stringa=#{url_encode(parola)}"
    uri = URI.parse(url)
    http = @bot.httputil.get_proxy(uri)
    xml = nil
    defurls = Array.new
    begin
      http.start() { |http|
	resp = http.get(uri)
	case resp.code
	when "200"
	  xml = resp.body
	when "302"
	  loc = resp['location']
	  if loc =~ /#{@dmurl}\d+/
	    defurls << loc
	  end
	else
	  debug resp.to_a
	end
      }
    rescue => e
      debug "HttpUtil.get exception: #{e}, while trying to get #{uri}"
      m.reply "Errore"
      return
    end
    if xml
      if xml=~ /Non ho trovato occorrenze per/
	m.reply "Parola non trovata"
	return
      else
	xml.gsub(/href="(\d+)"/) { |match|
	  debug match.to_a.join(" || ")
	  defurls << "#{@dmurl}#{$1}"
	}
      end
    end
    lemmas = Array.new
    defurls.each { |url|
      uri = URI.parse(url)
      http = @bot.httputil.get_proxy(uri)
      begin
	debug "Scanning #{url}"
	http.start() { |http|
	  resp = http.get(uri)
	  case resp.code
	  when "200"
	    debug "Got data"
	    matched = /<span class="lemma">(.*)<\/span><br\/><span class="qualifica".*?>(.*?)<\/span><br\/>/.match(resp.body)
	    dirtylemma = matched[1]
	    qual = matched[2]
	    lemma = dirtylemma.gsub(/<\/?span(?: class="pipelemma")?>/,"")
	    debug lemma
	    lemma = lemma.gsub(/<sup>1<\/sup>/,'¹').gsub(/<sup>2<\/sup>/,'²').gsub(/<sup>3<\/sup>/,'³')
	    lemma = lemma.gsub(/<sup>4<\/sup>/,'⁴').gsub(/<sup>5<\/sup>/,'⁵').gsub(/<sup>6<\/sup>/,'⁶')
	    lemma = lemma.gsub(/<sup>7<\/sup>/,'⁷').gsub(/<sup>8<\/sup>/,'⁸').gsub(/<sup>9<\/sup>/,'⁹')
	    debug lemma
	    lemma += " #{qual} (#{uri})"
	    lemmas << lemma
	  else
	    debug resp.to_a.join("\r")
	  end
	}
      rescue => e
	debug "Exception '#{e}' while trying to get and parse #{uri}"
	m.reply "Errore"
	return
      end
    }
    pre = lemmas.length > 1 ? "Lemmi trovati" : "Lemma trovato"
    m.reply "#{pre}: #{lemmas.join(' ; ')}"
  end
end

plugin = DeMauroPlugin.new
plugin.map 'demauro :parola', :action => 'demauro'