LoginSignup
3
1

【TwitterAPI】FF比が小さいフォロワーを自動でフォロー解除する【Ver.1.1.0】

Last updated at Posted at 2023-01-15

詳細は前記事参照

12/4追記

上記のサービスがTwitterAPIの仕様変更により提供を終了したので現在この記事の手法は使えません。

3/11追記

APIの仕様が変わるタイミングで使えなくなるかなと思っていたのですが、大丈夫でした。

Ver.1.1.0 アップデート内容

  • ホワイトリスト機能の追加
  • CSVファイルを編集する必要がなくなった
  • SettingsのUI改善
  • 高速化
  • TwitterAPIでは1日400件がアンフォローの上限であることを考慮し、botが止まらないように制御

手順(最新版)

  1. Twitterのフォロワーリスト一括ダウンロード にて自分のフォロワー一覧をCSVファイルで取得。

  2. 以下のPythonコードをローカル環境にて実行。
    pandasをinstallしていない方は pip install pandas を事前に行ってください。
    GitHubにもアップしてます → https://github.com/Asa-Shu/twitter-bot

unfollow_1.1.0.py
import tweepy
import time
import pandas as pd

# Settings

# TwitterAPI
api_key = '******'
api_key_secret = '******'
access_token = '******'
access_token_secret = '******'

# Exception account
white_list = {'******', '******', '******'}

# CSV file
file_path = '******.csv'

# FF rate
rate = 1.5

# start number
start = 0

client = tweepy.Client(
    consumer_key=api_key,
    consumer_secret=api_key_secret,
    access_token=access_token,
    access_token_secret=access_token_secret,
)

df = pd.read_csv(file_path)
selected_account_list = df[(df.フォロー数 / df.フォロワー数 > rate)
                           & (~df['スクリーン名'].isin(white_list))][['スクリーン名', '名前']]

selected_account_list['No'] = range(0, len(selected_account_list))

loop_count = 0
for selected_account in list(selected_account_list.itertuples())[start:]:
    screen_name = selected_account[1]
    name = selected_account[2]
    index = selected_account[3]
    print(index, screen_name, name)
    user = client.get_user(username=screen_name,
                           user_fields=['description', 'protected', 'name', 'username', 'public_metrics', 'profile_image_url'], user_auth=True)
    client.unfollow_user(target_user_id=user.data.id)
    loop_count += 1
    if loop_count % 399 == 0:
        time.sleep(24 * 60 * 60)
    else:
        time.sleep(24)
3
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
1