[PYTHON] Follow all users who are not following on Twitter

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)

It seems that there are API access restrictions per unit time of Twitter, so if there are too many, an exception will be thrown on the way. Avoid by putting sleep in the middle. With this, the user at the moment when the exception is thrown can not be surely followed, but well, it is not such a critical code, is it okay?

I don't use it myself w

If you change it a little, you can use it in the opposite way.

Recommended Posts

Follow all users who are not following on Twitter
All Pythonistas who do not follow the PEP8 coding conventions are equally feces
To people who are "recruiting but not recruiting"
The one who is not on DVD
Get images from specific users on Twitter