Sha256: 8256f689c8e6f8b35820bbc4d4b17853b342dd9f1280e537e04e120b09cd34b1

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

# -*- coding: utf-8 -*-
require 'cinch'
require 'cinch-toolbox'
require 'cinch-cooldown'

module Cinch::Plugins
  class Wikipedia
    include Cinch::Plugin

    enforce_cooldown

    self.help = "Use .wiki <term> to see the Wikipedia info for that term."

    match /wiki (.*)/
    match /wikipedia (.*)/

    def initialize(*args)
      super
      @max_length = config[:max_length] || 300
    end

    def execute(m, term)
      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.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 "Wikipedia ∴ #{text} [#{url}]"
    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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
cinch-wikipedia-1.0.0 lib/cinch/plugins/wikipedia/wikipedia.rb