3
2

More than 5 years have passed since last update.

TwitterでFollowしてないユーザーをまとめてFollowする

Last updated at Posted at 2014-02-02
followAllFollowers.py
# -*- coding: utf-8 -*-
import twitter
import time
import secret

api = twitter.Api(
        consumer_key = secret.dict['consumer_key'],
        consumer_secret = secret.dict['consumer_secret'],
        access_token_key = secret.dict['your_user_key'],
        access_token_secret = secret.dict['your_secret_key']
        )

def getNotfollowingIDs():
    following = api.GetFriendIDs()
    followers = api.GetFollowerIDs()

    notfollowing = []
    for item in followers:
        if item not in following:
            notfollowing.append(item)

    return notfollowing

if __name__ == '__main__':
    notfollowing = getNotfollowingIDs()
    for i in notfollowing:
        try:
            api.CreateFriendship(i)
            usr = api.GetUser(i)
            print 'Create a friendship with %s(@%s)' % (usr.GetName(), usr.GetScreenName())
        except:
            time.sleep(60)

 Twitterの単位時間あたりのAPIアクセス制限があるらしいので,あまり数が多いと途中で例外が投げられる.途中にsleep入れて回避.
 これだと例外が投げられた瞬間のユーザーも確実にフォローすることはできないけれど,まぁ,そこまでクリティカルなコードでもないし,いいか.

 自分で使うことはしないw

 ちょこっと変えれば,逆の方法にも使えるよね.

3
2
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
2