Not everyone on Twitter is interested in a friendly conversation. Some people are just there for mischief and harassment. Let’s look at how we can use Tweepy to block and mute accounts.
This post is part of my journey to learn Python. You can find the other parts of this series here. You find the code for this post in my PythonFriday repository on GitHub.
Preparation
As with the examples before, we need the user authentication tokens to work on behalf of that user. We use the V2 endpoints for this post:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import tweepy import os from dotenv import load_dotenv load_dotenv() # read keys from .env api_key = os.getenv('api-key') api_secret = os.getenv('api-key-secret') client_id = os.getenv('client-id') client_secret = os.getenv('client-secret') user_token = os.getenv('access-token') user_token_secret = os.getenv('access-token-secret') bearer_token = os.getenv('bearer-token') client = tweepy.Client( bearer_token=bearer_token, consumer_key=api_key, consumer_secret=api_secret, access_token=user_token, access_token_secret=user_token_secret, wait_on_rate_limit=True ) |
Get the user id from a screen name
Most methods we use in this post require the user id for an account. If you have a screen name, you can use this code to find the user id:
1 2 3 4 5 |
# find user id by username user_to_mute = "BILD" response = client.get_user(username=user_to_mute) user = response.data print(f"id of user '@{user_to_mute}': {user.id}") |
id of user ‘@BILD’: 9204502
Mute an account
Muted accounts can still see what you tweet, but you do no longer see what they tweet and retweet. We can use the method mute() to mute an account:
1 2 3 |
# mute a user result = client.mute(target_user_id=user.id) print(f"user muted? {result.data['muting']}") |
user muted? True
List all muted accounts
If you need a list of all muted accounts, you can use the method get_muted() and put that into a paginator:
1 2 3 4 5 6 7 8 9 10 11 |
# get a list of all muted users muted = [] for response in tweepy.Paginator(client.get_muted, max_results=1000, limit=10): for user in response.data: muted.append(f"@{user.username} - {user.name} - {user.id}") print("Muted users:") for entry in muted: print(entry) |
Muted users:
@BILD – BILD – 9204502
Unmute an account
If we no longer want to mute an account, we can use the method unmute():
1 2 3 |
# unmute a user result = client.unmute(target_user_id=user.id) print(f"user muted? {result.data['muting']}") |
user muted? False
Block an account
If an account no longer should see your tweets, you can block it. The method to do that is block():
1 2 3 |
# block a user result = client.block(target_user_id=user.id) print(f"user blocked? {result.data['blocking']}") |
user blocked? True
List all blocked accounts
If we use the paginator with the method get_blocked() we get the list of all accounts we have blocked:
1 2 3 4 5 6 7 8 9 10 11 |
# get a list of all blocked users blocked = [] for response in tweepy.Paginator(client.get_blocked, max_results=1000, limit=10): for user in response.data: blocked.append(f"@{user.username} - {user.name} - {user.id}") print("Blocked users:") for entry in blocked: print(entry) |
Blocked users:
@BILD – BILD – 9204502
Unblock an account
Should you have blocked the wrong account, you can use the method unblock() to fix it:
1 2 3 |
# unblock a user result = client.unblock(target_user_id=user.id) print(f"user blocked? {result.data['blocking']}") |
user blocked? False
Next
Blocking and muting are important activities to keep a positive Twitter experience. With the trolls out of our sight, we can next week look at the more positive side of interactions.
Hi there, how do you get the client id and client-secret keys?
I have all others, but can’t find these 2 on the developper space of Twitter.
thank you
Hi nono,
You must allow your application to access the V2 endpoints. Then you get a section for “OAuth 2.0 Client ID and Client Secret” below the bearer & access token in the developer portal for Twitter.
Regards,
Johnny
hi, the code works for all the reading part without using cliendid/secret
nevertheless i get error 403/forbiden when I try to block accounts.
my app access is read only (from what i can remember), and developper (not prod), could one of the 2 be teh reason why?
thank you for these examples, very useful
best
Hi nono,
To block users you need also the two authentication tokens for the user. You can get them after an authentication round trip as described here: https://improveandrepeat.com/2022/02/python-friday-111-twitter-authentication-tweepy/
Regards,
Johnny
Hi, I had done all that, but what worked for me was to change app permission from read-only to read-write in the setup page.
tx for your help and for this blog. very useful