カメニッキ

カメとインコと釣りの人です

MacでRuby2.1を使い、Twitterで自動リフォローする。

★★以下も合わせてどうぞ★★

※予め以下のページを参考に、トークン等取得してください。

  • CONSUMER_KEY
  • CONSUMER_SECRET
  • OAUTH_TOKEN
  • OAUTH_TOKEN_SECRET

MacでRuby2.1を使い、Twitterのbotを作成する。 - アラサーメモ

■follow.rb

#!/usr/bin/ruby
# -*- coding: utf-8 -*-

require 'user_stream'
require 'twitter'

consumer_key = '取得したCONSUMER_KEY'
consumer_secret = '取得したCONSUMER_SECRET'
oauth_token = '取得したOAUTH_TOKEN'
oauth_token_secret = '取得したOAUTH_TOKEN_SECRET'

#config実施
client = Twitter::REST::Client.new do |config|
  config.consumer_key        = consumer_key
  config.consumer_secret     = consumer_secret 
  config.access_token        = oauth_token
  config.access_token_secret = oauth_token_secret 
end

begin
  #フォロワーを取得
  @follower = client.follower_ids

  #フォロー済みを取得
  @follow = client.friend_ids
rescue
    #エラーメッセージ出力とか
    exit
end

#フォロワーとフォロー済みの差分を取得
@fan = @follower.to_a - @follow.to_a
cnt = 0
@fan.each do |f|
    #規制されないよう適当に制限(ここでは100件まで)
    if cnt > 100 then
      break
    end
    cnt += 1
    p "リフォロー:#{cnt.to_s}人目の処理:#{f}"
    #ユーザーをフォローします。
    begin
        client.follow(f)
    rescue
        #エラーメッセージ出力とか
        next 
    end
end

つぶやきbot同様に、cronで実行します。
ruby follow.rb

あまり叩き過ぎると、規制されてしまうので気をつけてください。