カメニッキ

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

MacでRuby2.1を使い、Twitterで自動リムーブする。

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


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

  • CONSUMER_KEY
  • CONSUMER_SECRET
  • OAUTH_TOKEN
  • OAUTH_TOKEN_SECRET

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

■remove.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

#片思いを取得
@remove = @follow.to_a - @follower.to_a
cnt = 0
@remove.each do |f|
  if cnt > 250 then
    break
  end
  cnt += 1
  p "リムーブ:#{cnt.to_s}人目の処理:#{f}"
  #ユーザーをリムーブします
  begin
    client.unfollow(f)
  rescue
    next
  end
end

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

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