Last active
April 27, 2020 09:54
-
-
Save yusukesaitoh/b84f9346715c4f90e8a5308c32557abc 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 twitter | |
import sys | |
import time | |
# Twitterに開発者登録、Appsを作成して必要な情報を取得 | |
ACCESS_TOKEN = "" | |
ACCESS_TOKEN_SECRET = "" | |
CONSUMER_KEY = "" | |
CONSUMER_SECRET = "" | |
FOLLOWERS_COUNT = 2000 | |
muted_ids = [] | |
def get_muted_ids(): | |
global muted_ids | |
try: | |
muted_ids = api.GetMutesIDs() | |
except twitter.error.TwitterError as e: | |
muted_ids = [] | |
def open_muted_ids(): | |
global muted_ids | |
try: | |
f = open('muted_ids.txt') | |
except FileNotFoundError as e: | |
get_muted_ids() | |
else: | |
muted_ids = list(map(int, f.read().split())) | |
f.close() | |
def save_muted_ids(): | |
global muted_ids | |
muted_ids_str = '\n'.join(map(str,muted_ids)) | |
with open("muted_ids.txt", 'wt') as f: | |
f.write(muted_ids_str) | |
def mute_replies(tweet, is_recursion=False): | |
tweet_id = tweet.id | |
max_id = None | |
while True: | |
term = "to:"+tweet.user.screen_name | |
try: | |
replies = api.GetSearch(term=term, since_id=tweet_id, max_id=max_id, count=100, result_type='recent') | |
except twitter.error.TwitterError as e: | |
print("エラー %d: %s" % (e.message[0]["code"], e.message[0]["message"])) | |
break | |
for reply in replies: | |
if reply.in_reply_to_status_id == tweet_id: | |
#指定ツイートへのリプライ | |
if reply.user.screen_name != tweet.user.screen_name: | |
#他人のリプライ | |
if reply.user.followers_count < FOLLOWERS_COUNT: | |
#フォロワー数が一定よりも少ないユーザー | |
if reply.user.id not in muted_ids: | |
try: | |
api.CreateMute(screen_name=reply.user.screen_name) | |
except twitter.error.TwitterError as e: | |
print("エラー %d: %s" % (e.message[0]["code"], e.message[0]["message"])) | |
return | |
muted_ids.append(reply.user.id) | |
print("%sをミュート" % reply.user.screen_name) | |
if is_recursion: | |
mute_replies(reply) #リプライへのリプライ | |
max_id = reply.id | |
if len(replies) != 100: | |
break | |
if __name__ == '__main__': | |
args = sys.argv | |
if len(args) >= 2: | |
if args[1].isdigit(): | |
api = twitter.Api( | |
consumer_key=CONSUMER_KEY, | |
consumer_secret=CONSUMER_SECRET, | |
access_token_key=ACCESS_TOKEN, | |
access_token_secret=ACCESS_TOKEN_SECRET, | |
sleep_on_rate_limit=True | |
) | |
open_muted_ids() | |
tweet = api.GetStatus(status_id=int(args[1]), trim_user=False, include_my_retweet=False, include_entities=False) | |
mute_replies(tweet) | |
save_muted_ids() | |
else: | |
print('エラー: ツイートIDに文字列が含まれています') | |
else: | |
print('エラー: 引数にツイートIDを指定してください') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment