Skip to content

Instantly share code, notes, and snippets.

@phelrine
Created December 9, 2011 05:44
Show Gist options
  • Save phelrine/1450353 to your computer and use it in GitHub Desktop.
Save phelrine/1450353 to your computer and use it in GitHub Desktop.
リツイート
# -*- 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