Skip to content

Instantly share code, notes, and snippets.

@naoto
Created November 2, 2009 13:30
Show Gist options
  • Save naoto/224157 to your computer and use it in GitHub Desktop.
Save naoto/224157 to your computer and use it in GitHub Desktop.
Citrus Plugin - Twitter Search -
require 'nokogiri'
require 'open-uri'
class TwitterSearch < Citrus::Plugin
def on_privmsg(prefix, channel, message)
if message =~ /(#{@config["words"]})/ &&
(!@config["channels"] || @config["channels"].include?(channel))
words = $1 if message =~ /^tws\s(.+)$/
search(words.split(/\s/)).each { |result|
notice channel, result
break if result == 3
}
end
end
private
def search(words)
result = []
words.each { |word| word.gsub!(/(-)/,'"\\1"') if word =~ /^([^#])/ }
url = "http://search.twitter.com/search.atom?q=#{words.join("+")}&lang=all"
html = Nokogiri::HTML(open(URI.escape(url)))
html.search("entry").each{ |e|
result << "@#{e.at('author/name').content.gsub(/\(.+$/,'')}: #{e.at('title').content} #{URI.short_uri(e.at("link")["href"])}"
break if result.size > 2
}
result << "しらない" if result.empty?
return result
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment