Created
October 2, 2021 11:20
mass_unfollow_twitter.py
This file contains 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/python3 | |
import tweepy | |
import time | |
def limit_handled(cursor): | |
while True: | |
try: | |
yield next(cursor) | |
except tweepy.errors.TooManyRequests as e: | |
print(e) | |
time.sleep(15 * 60) | |
SCREEN_NAME = "" | |
CONSUMER_KEY = "" | |
CONSUMER_SECRET = "" | |
ACCESS_TOKEN = "" | |
ACCESS_TOKEN_SECRET = "" | |
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) | |
auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET) | |
api = tweepy.API(auth, wait_on_rate_limit=True) | |
# test authentication | |
try: | |
api.verify_credentials() | |
print("Authentication OK") | |
except: | |
print("Error during authentication") | |
def get_twitter_followers(retry = 3): | |
followers = None | |
retry = retry - 1 | |
if retry == 0: | |
return followers | |
try: | |
followers = api.get_follower_ids() | |
return followers | |
except tweepy.errors.TooManyRequests as e: | |
print(e) | |
time.sleep(15 * 60) | |
followers = get_twitter_followers(retry-1) | |
def get_twitter_following(retry = 3): | |
following = None | |
retry = retry - 1 | |
if retry == 0: | |
return following | |
try: | |
following = api.get_friend_ids() | |
return following | |
except tweepy.errors.TooManyRequests as e: | |
print(e) | |
time.sleep(15 * 60) | |
following = get_twitter_following(retry-1) | |
followers = get_twitter_followers(10) | |
following = get_twitter_following(10) | |
print(f"Total followers before {len(followers)}") | |
print(f"Total following before {len(following)}") | |
print("Staring mass unfollow operations..") | |
for follow in following: | |
if follow not in followers: | |
try: | |
user = api.get_user(user_id=follow) | |
# print(user) | |
print(f"User id {user.screen_name} is not following you back. Unfollowing...") | |
api.destroy_friendship(user_id=follow) | |
except tweepy.errors.TooManyRequests as e: | |
print(e) | |
time.sleep(15*60) | |
followers = get_twitter_followers(10) | |
following = get_twitter_following(10) | |
print(f"Total followers now {len(followers)}") | |
print(f"Total following now {len(following)}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment