Sha256: b8d62591c3ef7c22e6b693416bd1d2a8ad3efcac6d33b697550e9902ca55112a

Contents?: true

Size: 788 Bytes

Versions: 14

Compression:

Stored size: 788 Bytes

Contents

require 'open-uri'
require 'nokogiri'
require 'cgi'

module Cinch
  module Plugins
    class Google
      include Cinch::Plugin

      match /g (.+)/

      set :plugin_name, 'google'
      set :help, <<EOT
Google returns the first google match on a search
.g  <keywords> : searches on those keywords
EOT

      def new(bot)
        @bot = bot
      end

      def search(query)
        url = "http://www.google.com/search?q=#{CGI.escape(query)}"
        res = Nokogiri::HTML(open(url)).at("h3.r")

        title = res.text
        link = res.at('a')[:href]
        link.gsub!(/^.*q=(.*)&sa.*$/) { $1 }
        CGI.unescape_html "#{title} (#{link})"
      rescue
        "No results found"
      end

      def execute(m, query)
        m.reply(search(query))
      end
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
cogbot-0.1.14 plugins/google.rb
cogbot-0.1.13 plugins/google.rb
cogbot-0.1.12 plugins/google.rb
cogbot-0.1.11 plugins/google.rb
cogbot-0.1.10 plugins/google.rb
cogbot-0.1.9 plugins/google.rb
cogbot-0.1.7 plugins/google.rb
cogbot-0.1.6 plugins/google.rb
cogbot-0.1.5 plugins/google.rb
cogbot-0.1.4 plugins/google.rb
cogbot-0.1.3 plugins/google.rb
cogbot-0.1.2 plugins/google.rb
cogbot-0.1.1 plugins/google.rb
cogbot-0.1.0 plugins/google.rb