This is part three of my series on using Python to connect to the Twitter API.
Imagine for a moment that you had a specific vision for your Twitter account. A vision of balance, and harmony. What if you only followed people who also followed you? Whether or not you want to curate your Twitter experience in this transactional way is entirely up to you. It’s your account!
We can do that with Python. As always, replace the placeholders with your own account credentials. See Part One of this series if you’re not sure how to do that.
Let’s take a look at the code required to do this:
import tweepy consumerKey = "<>" consumerSecret = "<>" accessToken = "<>" accessTokenSecret = "<>" auth = tweepy.OAuthHandler(consumerKey, consumerSecret) auth.set_access_token(accessToken, accessTokenSecret) #Call the api api = tweepy.API(auth,wait_on_rate_limit=True) #define two empty lists followers =  following =  #Get the list of friends for status in tweepy.Cursor(api.get_friends,count=200).items(): #Add the results to the list following.append(status.screen_name) #get the list of followers for status in tweepy.Cursor(api.get_followers,count=200).items(): #Add the results to the list followers.append(status.screen_name) #compare the lists and take action for person in following: if person not in followers: api.destroy_friendship(screen_name=person) print("Unfollowed " + person)