2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

闇のプログラミングAdvent Calendar 2021

Day 4

フォローしてない鍵垢のフォロワーをまとめてブロックする

Last updated at Posted at 2021-06-06

フォローリクエストに応えない不気味な鍵垢を一掃する

環境の切り替え

import sys
import argparse

def args():
    parser = argparse.ArgumentParser(description='environment name')
    parser.add_argument('-e', '--env', metavar='env', type=str,
                        help='environment name', default="")
    args = parser.parse_args()
    print(args)
    if args.env == "":
        print("No Argument")
        sys.exit()
    envName = args.env
    return envName

認証

import tweepy
import configparser

def auth_api(envName):
    config = configparser.ConfigParser()
    config.read('setting.ini')
    print("envName is " + envName)
    consumer_key = config.get(envName, 'consumer_key')
    consumer_secret = config.get(envName, 'consumer_secret')
    access_key = config.get(envName, 'access_key')
    access_secret = config.get(envName, 'access_secret')
    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_key, access_secret)
    api = tweepy.API(auth)
    return api

実行部分

def main():
    meId = api.me().screen_name
    followerIds = api.followers_ids(meId)
    friendsIds = api.friends_ids(meId)
    for i in followerIds:
        try:
            user = api.get_user(i)
        except Exception as e:
            print(e)
        if user.protected == True and user.id not in friendsIds:
            print([user.screen_name, user.name])
            try:
                api.create_block(user.screen_name)
            except Exception as e:
                print(e)

余談

  • 寝ぼけてuser.protected == "True"にしてたら、Trueがstr扱いでboolじゃないのに10分くらい気が付かなくてキーボード破壊するところだった。
2
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?