Skip to content

Instantly share code, notes, and snippets.

@mahm
Created January 7, 2014 05:31
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 mahm/8295021 to your computer and use it in GitHub Desktop.
Save mahm/8295021 to your computer and use it in GitHub Desktop.
時間毎のtweetのretweet数とfavorite数の合計を求める。どの時間帯のtweetが一番反応が良いか?という簡易的な調査。
require 'twitter'
if ARGV[0].nil? || ARGV[0] == ''
puts 'Usage:'
puts ' ruby reaction_survey.rb [twitter user name]'
exit
end
username = ARGV[0]
@client = Twitter::REST::Client.new do |config|
config.consumer_key = 'twitter consumer_key'
config.consumer_secret = 'twitter consumer_secret'
config.access_token = 'twitter access_token'
config.access_token_secret = 'twitter access_token_secret'
end
def collect_with_max_id(collection = [], max_id = nil, &block)
response = yield max_id
collection += response
response.empty? ? collection.flatten : collect_with_max_id(collection, response.last.id - 1, &block)
end
def get_all_tweets(user)
collect_with_max_id do |max_id|
options = {:count => 200, :include_rts => true}
options[:max_id] = max_id unless max_id.nil?
@client.user_timeline(user, options)
end
end
result = []
get_all_tweets(username).map do |tweet|
hour = DateTime.parse(tweet.attrs[:created_at]).to_time.hour
result[hour] = (result[hour] || 0) + tweet.favorite_count + tweet.retweet_count
end
result.each_with_index do |point, hour|
puts "#{sprintf("%02d", hour)}: #{point}"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment