Skip to content

Instantly share code, notes, and snippets.

@komasaru
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save komasaru/6a6bf1990f75f8050024 to your computer and use it in GitHub Desktop.
Save komasaru/6a6bf1990f75f8050024 to your computer and use it in GitHub Desktop.
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