どの時間帯にツイートすると一番反応良いかをざっくり調べる

毎日ブログを書いているのですが、逆に毎日書いているせいで良い記事を見逃してしまうという意見が僕の半径5mぐらいでありました。という訳でBuffer的なサービスを利用して特定時刻に過去に書いたブログのエントリをつぶやこうと思ったのですが、ふと、何時にツイートすると一番反応が良いのかな?と気になりました。

そんな訳で、そういうのを調べるスクリプトを書いてみました。

仮説

  • retweetされたりfavoriteされたりしているtweetは注目されている。
  • 注目されているtweetの多い時間帯は注目されている。
  • なので注目されているtweetの多い時間帯に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

参考

使い方

$ ruby reaction_survey.rb mah_lab

結果

$ ruby reaction_survey.rb mah_lab
00: 30140
01: 9
02: 2
03: 0
04: 2
05: 325
06: 25
07: 4570
08: 3078
09: 5729
10: 2414
11: 2231
12: 4741
13: 11939
14: 10510
15: 209
16: 5070
17: 8029
18: 16087
19: 153
20: 8432
21: 270
22: 404
23: 16327

朝の方が反応良いと思いきや、18時台、もしくは23〜0時台の反応が良いみたい。