Created
October 14, 2021 17:48
-
-
Save jdblischak/b9f00ae56123ddd96a957522014bea42 to your computer and use it in GitHub Desktop.
Unfollow all Twitter accounts that aren't following you back
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# Unfollow all Twitter accounts that aren't following you back | |
# | |
# Twitter API: | |
# https://developer.twitter.com/en/docs/twitter-api | |
# https://github.com/python-twitter-tools/twitter | |
# https://developer.twitter.com/en/portal/projects/new | |
# | |
# Authentication: | |
# https://github.com/python-twitter-tools/twitter#authentication | |
# https://rdrr.io/cran/twitteR/man/setup_twitter_oauth.html | |
# | |
# Endpoints: | |
# Followers: https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/follow-search-get-users/api-reference/get-followers-ids | |
# Following: https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/follow-search-get-users/api-reference/get-friends-ids | |
# Unfollow: https://developer.twitter.com/en/docs/twitter-api/v1/accounts-and-users/follow-search-get-users/api-reference/post-friendships-destroy | |
# Pagination: https://developer.twitter.com/en/docs/pagination | |
# | |
# Rate limits: | |
# Can only unfollow 500 users per 24 hours | |
# https://developer.twitter.com/en/docs/twitter-api/rate-limits#v2-limits | |
# In practice I was able to unfollow more than 500 per day. Maybe this | |
# doesn't apply to API v1? I don't actually know. | |
# | |
# Troubleshooting | |
# * The app and access tokens must have write access. The app you create will | |
# initially be read-only. You need to edit it to include write-access. Then | |
# you can generate an access token with write-access. If you created an | |
# access token before enabling the app to have write-access, you will need | |
# to regenerate it. | |
import os | |
import random | |
import sys | |
import time | |
from twitter import * | |
# Authentication -------------------------------------------------------------- | |
consumer_key = os.getenv("TWITTER_CONSUMER_KEY") | |
consumer_secret = os.getenv("TWITTER_CONSUMER_SECRET") | |
access_token = os.getenv("TWITTER_ACCESS_TOKEN") | |
access_secret = os.getenv("TWITTER_ACCESS_SECRET") | |
auth = OAuth( | |
token=access_token, | |
token_secret=access_secret, | |
consumer_key=consumer_key, | |
consumer_secret=consumer_secret | |
) | |
t = Twitter(auth=auth) | |
# Followers ------------------------------------------------------------------- | |
followers = [] | |
cursor = -1 | |
count = 3000 | |
while cursor != 0: | |
response = t.followers.ids(cursor=cursor, count=count) | |
followers = followers + response["ids"] | |
cursor = response["next_cursor"] | |
time.sleep(random.uniform(0, 1)) | |
sys.stderr.write("You have %d followers\n"%(len(followers))) | |
# Following ------------------------------------------------------------------- | |
following = [] | |
cursor = -1 | |
count = 3000 | |
while cursor != 0: | |
response = t.friends.ids(cursor=cursor, count=count) | |
following = following + response["ids"] | |
cursor = response["next_cursor"] | |
time.sleep(random.uniform(0, 1)) | |
sys.stderr.write("You are following %d accounts\n"%(len(following))) | |
# Unfollow -------------------------------------------------------------------- | |
unfollow = [account for account in following if not account in followers] | |
sys.stderr.write("You are following %d accounts that do not follow you back\n"%(len(unfollow))) | |
for i in range(len(unfollow)): | |
if i == 0: | |
sys.stdout.write("id\taccount\tname\n") | |
if i == 500: | |
sys.stderr.write("Unfollowed 500 accounts. This is the rate limit for a 24 hour period\n") | |
break | |
unfollowed = t.friendships.destroy(user_id=unfollow[i]) | |
sys.stdout.write("%d\t%s\t%s\n"%(unfollowed["id"], unfollowed["screen_name"], unfollowed["name"])) | |
time.sleep(random.uniform(0, 1)) | |
if len(unfollow) > 0: | |
sys.stderr.write("You unfollowed %d accounts that do not follow you back\n"%(i)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment