Created
November 2, 2009 13:30
-
-
Save naoto/224157 to your computer and use it in GitHub Desktop.
Citrus Plugin - Twitter Search -
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 '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