This is updated to Tweepy version 4.4
In order to get a connection to twitter you need to have access tokens and consumer keys. If you don’t already have that, or you do not know what it is, then I recommend you follow this tutorial.
You also need the tweepy library. You can install it by typing the following command in the command line or see here for more details.
pip install tweepy
Then you are ready to connect to the Twitter API.’
The first thing your code should do is to connect to the Twitter API and return the tweepy api to your program.
import tweepy
def get_twitter_api():
# personal details
consumer_key = "__USE YOUR KEY HERE__"
consumer_secret = "__USE YOUR KEY HERE__"
access_token = "__USE YOUR KEY HERE__"
access_token_secret = "__USE YOUR KEY HERE__"
# authentication of consumer key and secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# authentication of access token and secret
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
return api
This code will authenticate and return the tweepy api.
Confused by the headline? Me, too. But here is where the magic happens.
The code simply explained.
def process():
api = get_twitter_api()
followers = api.get_followers_ids(user_id="PythonWithRune")
print("Followers", len(followers))
friends = api.get_friends_ids(user_id="PythonWithRune")
print("You follow:", len(friends))
for friend in friends:
if friend not in followers:
api.destroy_friendship(friend)
You can see the full code here to unfollow friends that do not follow back in Twitter using Python
import tweepy
def get_twitter_api():
# personal details
consumer_key = "__USE YOUR KEY HERE__"
consumer_secret = "__USE YOUR KEY HERE__"
access_token = "__USE YOUR KEY HERE__"
access_token_secret = "__USE YOUR KEY HERE__"
# authentication of consumer key and secret
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
# authentication of access token and secret
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
return api
def process():
api = get_twitter_api()
followers = api.get_followers_ids(user_id="PythonWithRune")
print("Followers", len(followers))
friends = api.get_friends_ids(user_id="PythonWithRune")
print("You follow:", len(friends))
for friend in friends:
if friend not in followers:
api.destroy_friendship(friend)
if __name__ == "__main__":
process()
Build and Deploy an AI App with Python Flask, OpenAI API, and Google Cloud: In…
Python REST APIs with gcloud Serverless In the fast-paced world of application development, building robust…
App Development with Python using Docker Are you an aspiring app developer looking to level…
Why Value-driven Data Science is the Key to Your Success In the world of data…
Harnessing the Power of Project-Based Learning and Python for Machine Learning Mastery In today's data-driven…
Is Python the right choice for Machine Learning? Should you learn Python for Machine Learning?…
View Comments
Any chance to brake a loop after unfollow 10 friends?
Sure.
You can do it with a simple counter:
counter = 0
for friend in friends:
if friend not in followers:
counter += 1
api.destroy_friendship(friend)
if counter >= 10:
break
When running the destroy_friendship api, I am getting the error:
destroy_friendship() takes 1 positional argument but 2 were given
My code is this:
# Create Lists
followers_list = list()
friends_list = list()
unfollow_list = list()
# Get account followers
followers = Follower.objects.filter(twitter_user_account=own_account_id)
for follower in followers:
followers_list.append(follower.follower_screen_name)
# Get account friends
friends = Friend.objects.filter(twitter_user_account=own_account_id)
for friend in friends:
friends_list.append(friend.friend_screen_name)
# Create unfollow list of users - List of accounts to unfollow
for item in friends_list:
if item not in followers_list:
unfollow_list.append(item)
# Unfollow users in unfollow list
for i in unfollow_list:
print(i)
api.destroy_friendship(i)
print('Removed ' + i + ' from following!')
time.sleep(2)
Can you help please?
First check the version of Tweepy.
The tutorial is for Tweepy version 4.4.
You can get the version by typing in a terminal where you installation is: pip freeze
Then find tweepy with the version.