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

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:

 

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:

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:

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:

Muted users:
@BILD – BILD – 9204502

 

Unmute an account

If we no longer want to mute an account, we can use the method unmute():

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():

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:

Blocked users:
@BILD – BILD – 9204502

 

Unblock an account

Should you have blocked the wrong account, you can use the method unblock() to fix it:

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.

6 thoughts on “Python Friday #118: Block and Mute Accounts With Tweepy”

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

    Reply
    • 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

      Reply
  2. 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

    Reply

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.