Sha256: 29b36788a4ffda0f92ed4b587342ef2beafac5b2ecdebe6e388e3d392aced329

Contents?: true

Size: 884 Bytes

Versions: 1

Compression:

Stored size: 884 Bytes

Contents

require 'robut'
require 'wikipedia'
require 'nokogiri'

class Robut::Plugin::Wikipedia
  include Robut::Plugin
  WIKI_REGEX = /^wiki (.*)$/

  def usage
    [
      "#{at_nick} wiki [page-slug]"
    ]
  end

  def handle(time, sender_nick, message)
    return unless sent_to_me?(message)

    request = words(message).join(" ")
    if match = request.match(WIKI_REGEX)
      process_response_for(match[1])
    end

  end

  def process_response_for(query)
    page_slug = query.strip.gsub(/\s/, "_")
    page = ::Wikipedia.find(page_slug)

    content = page.content
    if content.nil?
      reply("Couldn't find a page for: '#{page_slug}'")
      return
    end

    doc = Nokogiri::HTML(page.sanitized_content)
    reply("Wikipedia result for '#{page.title}'")
    reply(doc.search('p:first-child').text)

    if image = page.image_urls.first
      reply(image)
    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
robut-wikipedia-0.0.3 lib/robut-wikipedia.rb