Created
June 7, 2020 10:02
-
-
Save sawadyrr5/a1eccbeeb58086a2f3c55e6634f4154c to your computer and use it in GitHub Desktop.
一方的にフォローされているフォロワーをブロ解するスクリプト
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json, config | |
from requests_oauthlib import OAuth1Session | |
from time import sleep | |
# 同じフォルダにconfig.pyというファイルを作っておく | |
# トークンの取り方とかはここ参照 | |
# https://qiita.com/bakira/items/00743d10ec42993f85eb | |
CK = config.CONSUMER_KEY | |
CS = config.CONSUMER_SECRET | |
AT = config.ACCESS_TOKEN | |
ATS = config.ACCESS_TOKEN_SECRET | |
twitter = OAuth1Session(CK, CS, AT, ATS) # 認証処理 | |
url_follower = "https://api.twitter.com/1.1/followers/ids.json" | |
params = {'count': 1000} # 取得数 | |
res = twitter.get(url_follower, params=params) | |
if res.status_code == 200: # 正常通信出来た場合 | |
followers = json.loads(res.text) | |
followers_ids = followers['ids'] | |
else: # 正常通信出来なかった場合 | |
print("Failed: %d" % res.status_code) | |
url_friends = "https://api.twitter.com/1.1/friends/ids.json" | |
params = {'count': 1000} # 取得数 | |
res = twitter.get(url_friends, params=params) | |
if res.status_code == 200: # 正常通信出来た場合 | |
friends = json.loads(res.text) | |
friends_ids = friends['ids'] | |
else: # 正常通信出来なかった場合 | |
print("Failed: %d" % res.status_code) | |
block_ids = [id for id in followers_ids if id not in friends_ids] | |
for id in block_ids: | |
url_block = "https://api.twitter.com/1.1/blocks/create.json" | |
params = {'user_id': id} | |
res = twitter.post(url_block, params=params) | |
if res.status_code == 200: # 正常通信出来た場合 | |
blocked = json.loads(res.text) | |
print( | |
"block success:", | |
blocked['id'], | |
blocked['screen_name'] | |
) | |
else: # 正常通信出来なかった場合 | |
print("Failed: %d" % res.status_code) | |
sleep(0.5) | |
url_unblock = "https://api.twitter.com/1.1/blocks/destroy.json" | |
params = {'user_id': id} | |
res = twitter.post(url_unblock, params=params) | |
if res.status_code == 200: # 正常通信出来た場合 | |
unblocked = json.loads(res.text) | |
print( | |
"unblock success:", | |
unblocked['id'], | |
unblocked['screen_name'] | |
) | |
else: # 正常通信出来なかった場合 | |
print("Failed: %d" % res.status_code) | |
sleep(0.5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment