-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwikipedia.rb
74 lines (61 loc) · 2.34 KB
/
wikipedia.rb
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
require 'cinch'
require 'cinch/toolbox'
require_relative "config/check_ignore"
module Cinch
module Plugins
class Wikipedia
include Cinch::Plugin
set :plugin_name, 'wikipedia'
set :help, <<-USAGE.gsub(/^ {6}/, '')
The Wikipedia plugin searches Wikipedia for the terms provided and returns with the closest matching article.
Usage:
- !(wiki(pedia)) <terms>: Returns with the article most closely matching your terms.
USAGE
match /wiki (.*)/i
match /wikipedia (.*)/i
def initialize(*args)
super
@max_length = config[:max_length] || 300
end
def execute(m, term)
return if check_ignore(m.user)
m.reply get_def(term)
end
private
def get_def(term)
# URI Encode
term = URI.escape(term, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
url = "http://en.wikipedia.org/w/index.php?search=#{term}"
cats = Cinch::Toolbox.get_html_element(url, '#mw-normal-catlinks')
if cats && cats.include?('Disambiguation')
wiki_text = "'#{term} is too vague and lead to a disambiguation page."
else
# Grab the text
wiki_text = Cinch::Toolbox.get_html_element(url, '#mw-content-text p')
# Check for search errors
return not_found(wiki_text, url) if wiki_text.nil? || wiki_text.include?('Help:Searching')
end
# Truncate text and url if they are too long
text = Cinch::Toolbox.truncate(wiki_text, @max_length)
url = Cinch::Toolbox.shorten(url)
return Format(:green, "%s ∴ #{text} [#{url}]" % [Format(:bold, "Wikipedia")] )
end
def not_found(wiki_text, url)
msg = "I couldn't find anything for that search"
if alt_term_text = Cinch::Toolbox.get_html_element(url, '.searchdidyoumean')
alt_term = alt_term_text[/\ADid you mean: (\w+)\z/, 1]
msg << ", did you mean '#{alt_term}'?"
else
msg << ", sorry!"
end
return msg
end
end
end
end
## Written by Richard Banks for Eve-Bot "The Project for a Top-Tier IRC bot.
## E-mail: namaste@rawrnet.net
## Github: Namasteh
## Website: www.rawrnet.net
## IRC: irc.sinsira.net #Eve
## If you like this plugin please consider tipping me on gittip