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
ちょこっと変えれば,逆の方法にも使えるよね.