class Plugins::Wikipedia

Plugin to allow users to search wikipedia.

Public Class Methods

new(*args) click to toggle source
Calls superclass method
# File lib/Zeta/plugins/wiki.rb, line 22
def initialize(*args)
  super
  @max_length = config[:max_length] || 300
end

Public Instance Methods

execute(m, term) click to toggle source
# File lib/Zeta/plugins/wiki.rb, line 27
def execute(m, term)
  m.reply wiki(term)
end

Private Instance Methods

get_def(term, url) click to toggle source
# File lib/Zeta/plugins/wiki.rb, line 45
def get_def(term, url)
  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
    wiki_text = Cinch::Toolbox.get_html_element(url, '#mw-content-text p')
    if wiki_text.nil? || wiki_text.include?('Help:Searching')
      return not_found(wiki_text, url)
    end
  end
  wiki_text
end
not_found(wiki_text, url) click to toggle source
# File lib/Zeta/plugins/wiki.rb, line 58
def not_found(wiki_text, url)
  msg = "I couldn't find anything for that search, "
  alt_term_text = Cinch::Toolbox.get_html_element(url, '.searchdidyoumean')
  if alt_term_text
    alt_term = alt_term_text[/\ADid you mean: (\w+)\z/, 1]
    msg << "did you mean '#{alt_term}'?"
  else
    msg << 'sorry!'
  end
  msg
end
wiki(term) click to toggle source
# File lib/Zeta/plugins/wiki.rb, line 33
def wiki(term)
  # URI Encode
  term = URI.escape(term, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
  url = "http://en.wikipedia.org/w/index.php?search=#{term}"

  # Truncate text and url if they are too long
  text = Cinch::Toolbox.truncate(get_def(term, url), @max_length)
  url  = Cinch::Toolbox.shorten(url)

  "Wiki ∴ #{text} [#{url}]"
end