Bot is unfollowing people that do follow me back.
vmunich opened this issue · 10 comments
Hi,
First of all, thank you for making this available to everyone! It's great!
My problem is, when I use the auto_unfollow_nonfollowers() function on an account that has around 20k followers, it unfollows people that do follow me back.
Researching online, I found this: https://stackoverflow.com/questions/18799023/php-twitter-bot-to-follow-unfollow-using-api-version-1-1-and-cursors
Has anyone had this same problem?
That's interesting! I never looked at this except when I was far below 5,000 followers. Now I have to wonder how many people I've unfollowed on accident...
I've confirmed that these calls do indeed only return 5,000 users at a time. Filed this as a bug.
I've started hacking at the code a bit and found a solution, but unfortunately it doesn't seem to scale. I basically work through the cursors to build up a list of all of the user's followers and who they're following. In my case, that's ~40k IDs in both sets, which is about 16 API calls just to populate these lists. As you can imagine, I hit the Twitter API rate limit pretty quick by testing that. And I didn't even do any following/favoriting/etc.
def auto_unfollow_nonfollowers():
"""
Unfollows everyone who hasn't followed you back
"""
# create a list of all users that the user is following and followed by
# necessary to use cursors if the user has >5,000 followers or following
followers_status = t.followers.ids(screen_name=TWITTER_HANDLE)
followers = set(followers_status["ids"])
next_cursor = followers_status["next_cursor"]
while next_cursor != 0:
followers_status = t.followers.ids(screen_name=TWITTER_HANDLE, cursor=next_cursor)
followers.update(followers_status["ids"])
next_cursor = followers_status["next_cursor"]
following_status = t.friends.ids(screen_name=TWITTER_HANDLE)
following = set(following_status["ids"])
next_cursor = following_status["next_cursor"]
while next_cursor != 0:
following_status = t.friends.ids(screen_name=TWITTER_HANDLE, cursor=next_cursor)
following.update(following_status["ids"])
next_cursor = following_status["next_cursor"]
# ... rest of the code goes here
Perhaps a more scalable solution is to store the IDs locally and have a one-time "setup" function that users have to call to populate these lists. But that won't be a perfect solution because any follow/unfollow not done through the bot won't be recorded in the list. Furthermore, if anyone unfollows them after the setup, that also won't be reflected in the local list.
In the meantime, I advise against using the auto unfollow functionality if you have over 5,000 followers.
I do it on the regular honestly. twitter doesnt limit me and i have accounts with 40k followers.
Hi Randy,
I see that there is a 15 API calls per 15 minutes window limit. If you delay each API call that populates the list of people that don't follow you back, would twitter still block you?
So for example, the function would grab all the cursors/pages until it hits the API limit, wait for 15 minutes, then continue the process.
Another option would be use multiple Twitter Apps/API Keys and switch among them, but I don't think this is a good idea.
What do you think?
Randy,
Please take a look on this:
bear/python-twitter#71
More precisely, the last comment. I will play with that tonight and see what I can get.
@vmunich, please let me know if you have any luck with that method! It seems promising.
Still a standing issue. Haven't had time to dedicate to this bot.
Finally got around to fixing this issue. Cursors ended up being the key to fixing it - now the bot can properly look up all of an accounts follows/followers. However, due to API rate limits, follows/followers now have to be cached locally. I recommend running the new sync_follows()
method daily to ensure that the bot is working on a relatively up-to-date cache.