Created
August 30, 2012 09:57
-
-
Save komiya-atsushi/3525187 to your computer and use it in GitHub Desktop.
statuses/sample の streaming API から流れるツイートの流量を測定する Ruby スクリプト
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 'open3' | |
API_URL = 'https://stream.twitter.com/1/statuses/sample.json' | |
USER = 'Twitter_account' | |
PW = 'password' | |
INTERVAL_SECONDS = 10 | |
stdin, stdout, stderr = *Open3.popen3("curl #{API_URL} -u#{USER}:#{PW}") | |
line_count = 0 | |
last_line_count = 0 | |
next_elapsed = INTERVAL_SECONDS | |
last_elapsed = 0 | |
start = Time.now | |
stdout.each do |line| | |
line_count += 1 | |
current = Time.now | |
elapsed = current - start | |
if elapsed >= next_elapsed | |
log_msg = "#{current.strftime('%Y/%m/%d %H:%m:%S')} #{line_count / elapsed} #{(line_count - last_line_count) / (elapsed - last_elapsed)}" | |
puts log_msg | |
$stderr.puts log_msg | |
next_elapsed += INTERVAL_SECONDS | |
last_elapsed = elapsed | |
last_line_count = line_count | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment