]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blob - data/rbot/plugins/oxford.rb
plugin(oxford): moved to lexico.com, closes #13
[user/henk/code/ruby/rbot.git] / data / rbot / plugins / oxford.rb
1 # encoding: UTF-8
2 #-- vim:sw=2:et
3 #++
4 #
5 # :title: Oxford Dictionary lookup plugin for rbot
6 #
7 # Author:: Giuseppe "Oblomov" Bilotta <giuseppe.bilotta@gmail.com>
8 # Copyright:: (C) 2006-2007 Giuseppe Bilotta
9 # License:: GPL v2
10 #
11 require 'cgi'
12
13 class OxfordPlugin < Plugin
14   Config.register Config::IntegerValue.new(
15     'oxford.max_lines',
16     :default => 1,
17     :desc => 'The number of lines to respond with.')
18
19   def initialize
20     super
21     @base_url = "https://www.lexico.com"
22   end
23
24   def help(plugin, topic="")
25     'oxford <word>: check for <word> on the lexico english dictionary (powered by oxford english dictionary).'
26   end
27
28   def oxford(m, params)
29     word = params[:word].join
30
31     url = "#{@base_url}/definition/#{CGI.escape word}"
32
33     begin
34       response = @bot.httputil.get(url, resp: true)
35       definition = parse_definition(response)
36
37       if definition.empty?
38         closest = response.xpath('//div[@class="no-exact-matches"]//ul/li/a').first
39
40         url = @base_url + closest['href']
41
42         m.reply "did you mean: #{Bold}#{closest.content.ircify_html}#{NormalText}"
43
44         response = @bot.httputil.get(url, resp: true)
45         definition = parse_definition(response)
46       end
47     rescue => e
48       m.reply "error accessing lexico url -> #{url}"
49       error e
50       return
51     end
52
53     if definition
54       m.reply definition.ircify_html, max_lines: @bot.config['oxford.max_lines']
55     else
56       m.reply "couldn't find a definition for #{word} on oxford dictionary"
57     end
58   end
59
60   private
61
62   def parse_definition(r)
63     r.xpath('//section[@class="gramb"]//text()').map(&:content).join(' ')
64   end
65 end
66
67 plugin = OxfordPlugin.new
68 plugin.map 'oxford *word', :action => 'oxford', :threaded => true
69