Skip to content

Instantly share code, notes, and snippets.

@ovelny
Created December 13, 2021 12:34
Show Gist options
  • Save ovelny/e311828abb20d8c979a40a9ea1889fda to your computer and use it in GitHub Desktop.
Save ovelny/e311828abb20d8c979a40a9ea1889fda to your computer and use it in GitHub Desktop.
Block all likers of a tweet, semi-automatically.

Blocking all likers of a tweet

Sometimes you need to block a lot of bigots —promptly— and megablock doesn't always work. Here's a quick & dirty way to do it.

Preparing for the megablock

You'll need to create a developer app on Twitter. Follow the steps here: https://developer.twitter.com/en/apps

You'll also need python3 on your system, which should already be present if you use any flavor of Linux or macOS. If you're using Windows, you can follow the installation steps here: https://docs.python.org/3/using/windows.html

Next, install the following dependencies:

pip3 install --user tweepy
pip3 install --user pyperclip

Fetching all likers

Navigate to the modal listing people who liked the bigoted tweet (endpoint should look like https://twitter.com/<user>/status/<id>/likes), press F12 and paste the following script in your console:

let likersModal = document.querySelector("div[class='css-1dbjc4n r-1pp923h r-1moyyf3 r-16y2uox r-1wbh5a2 r-1dqxon3']")
let likersArr = []

while (likersModal.offsetHeight + likersModal.scrollTop < likersModal.scrollHeight) {
  let likers = document.querySelectorAll("div[aria-labelledby='modal-header'] div[class='css-1dbjc4n r-1wbh5a2 r-dnmrzs'] a[class='css-4rbku5 css-18t94o4 css-1dbjc4n r-1loqt21 r-1wbh5a2 r-dnmrzs r-1ny4l3l']")
  likersArr.push([].map.call(likers, liker => liker.href))
  likersModal.scrollBy(0,2000)
  await new Promise(r => setTimeout(r, 2000))
}

likersArr = [...new Set(likersArr.flat())]
likersArr = likersArr.map.call(likersArr, likerUrl => likerUrl.replace("https://twitter.com/", ""))

This may take some time depending on how popular the tweet is, so go grab a coffee or something. After a while, you should see an array of all likers returned in your console. Right-click on the array and select "Copy object".

Blocking likers, one by one

⚠️ Take your time to review the list before proceeding to the next step! I am not responsible for any unintended block or error. ⚠️

Paste the following script in a any file (how about megablock.py?):

#!/usr/bin/env python3
import json

import pyperclip
import tweepy


def tweepy_auth():
    consumer_key = ""
    consumer_secret = ""
    access_token = ""
    access_token_secret = ""

    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    api = tweepy.API(auth)

    return api


def main(api):
    likers = json.loads(pyperclip.paste())
    for liker in likers:
        liker = api.get_user(screen_name=liker)
        print(f"Blocking {liker.screen_name} (id: {liker.id}) ...")
        api.create_block(screen_name=liker.screen_name, user_id=liker.id)


if __name__ == "__main__":
    api = tweepy_auth()
    main(api)

Fill the consumer_key, consumer_secret, access_token, access_token_secret variables with the credentials of your developer app created earlier, and save.

Make sure your clipboard still has the list of likers fetched earlier (if not, redo step #2), and you're ready to go.

⚠️ THERE IS NO CONFIRMATION PROMPT, THE SCRIPT WILL START BLOCKING PEOPLE FROM YOUR CLIPBOARD RIGHT AWAY ⚠️

... Ready to go?

... You sure?

Then run the script and watch the blocks unfold:

python3 megablock.py
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment