Created
December 9, 2011 05:44
-
-
Save phelrine/1450353 to your computer and use it in GitHub Desktop.
リツイート
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
require 'twitter/json_stream' | |
require 'twitter_oauth' | |
require 'json' | |
require 'pp' | |
CONSUMER_KEY = "consumer key" | |
CONSUMER_SECRET = "consumer secret" | |
ACCESS_KEY = "access key" | |
ACCESS_SECRET = "access secret" | |
PATTERN = "pattern" | |
client = TwitterOAuth::Client.new( | |
:consumer_key => CONSUMER_KEY, | |
:consumer_secret => CONSUMER_SECRET, | |
:token => ACCESS_KEY, | |
:secret => ACCESS_SECRET | |
) | |
EventMachine::run do | |
stream = Twitter::JSONStream.connect({ | |
:host => "userstream.twitter.com", | |
:path => "/2/user.json", | |
:port => 443, | |
:ssl => true, | |
:oauth => { | |
:consumer_key => CONSUMER_KEY, | |
:consumer_secret => CONSUMER_SECRET, | |
:access_key => ACCESS_KEY, | |
:access_secret => ACCESS_SECRET | |
} | |
}) | |
stream.each_item do |item| | |
json = JSON.parse(item) | |
if PATTERN =~ json["text"] | |
id = json["id_str"] | |
begin | |
client.retweet id | |
rescue => e | |
$stderr.puts e.message | |
end | |
end | |
end | |
trap('TERM') { | |
stream.stop | |
EventMachine.stop if EventMachine.reactor_running? | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment