Skip to content

Instantly share code, notes, and snippets.

@komasaru komasaru/ts_test.rb
Last active Aug 29, 2015

Embed
What would you like to do?
Ruby script to get tweet statuses by tweetstream library.
#! /usr/local/bin/ruby
# coding: utf-8
# ---------------------------------------------------------
# Ruby script to get tweet statuses by tweetstream library.
# ---------------------------------------------------------
#
require 'tweetstream'
# Twitter OAuth keys ( from environment variables )
CONS_KEY = ENV['TW_CONS_KEY']
CONS_SEC = ENV['TW_CONS_SEC']
ACCS_KEY = ENV['TW_ACCS_KEY']
ACCS_SEC = ENV['TW_ACCS_SEC']
# Coniguring of TweetStream
TweetStream.configure do |config|
config.consumer_key = CONS_KEY
config.consumer_secret = CONS_SEC
config.oauth_token = ACCS_KEY
config.oauth_token_secret = ACCS_SEC
config.auth_method = :oauth
end
# Instantiation
client = TweetStream::Client.new
# Getting Twitter streamings
client.sample do |item|
# If "user" does not exist, go next.
# If "user" exists, get "name", "screen_name", "text".
next unless user = item.user
name, screen_name, lang, text = user.name, user.screen_name, user.lang, item.text
# If lang is not "ja", go next.
next unless lang == "ja"
# If a tweet sentense does not include specified strings, go next.
#next unless text =~ /Ruby/
# コンソール出力
puts "\n**** #{name} [ #{screen_name} ]"
puts text
end
# Output error messages
client.on_error do |message|
$stderr.puts "[ERROR] #{message}"
end
# Output reconnect messages
client.on_reconnect do |timeout, retries|
$stderr.puts "[RECONNECT] timeout: #{timeout}, retires: #{retries}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.