Skip to content

#118: Block and Mute Accounts With Tweepy

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.

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:

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:

# 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:

# 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.