Created
July 17, 2012 08:11
-
-
Save kfatehi/3127934 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'cinch' | |
require 'open-uri' | |
require 'nokogiri' | |
require 'cgi' | |
require 'json' | |
require 'pry' | |
bot = Cinch::Bot.new do | |
configure do |c| | |
c.server = "irc.mdx.la" | |
c.port = 6668 | |
c.channels = ["#ducky", "#mdx"] | |
c.nicks = ["ducky", "ducky_sees_you"] | |
end | |
helpers do | |
# Extremely basic method, grabs the first result returned by Google | |
# or "No results found" otherwise | |
def google(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] | |
desc = res.at("./following::div").children.first.text | |
rescue | |
"No results found" | |
else | |
CGI.unescape_html "#{title} - #{desc} (#{link})" | |
end | |
def duckduckgo(query) | |
url = "http://api.duckduckgo.com/?q=!#{CGI.escape(query)}&format=json&no_redirect=1" | |
JSON.parse(open(url).read)["Redirect"] rescue "oops" | |
end | |
end | |
on :message, /^!(.+)/ do |m, query| | |
m.reply duckduckgo(query) | |
end | |
end | |
bot.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment