DEV Community

Azka
Azka

Posted on • Edited on

3 2

Blocking twitter trolls (2): Blocking fancams

Earlier this year, a kpop translation account started running a script that blocks all trolls who post fancam under their tweets. We can now restrict who can leave a reply under our tweet but the feature is limited.

If you're running a big account that provides content, you don't want to block all conversations under your tweet because after all, not all people are trolls. If you only want to reduce the number of trolls, in this case people who post fancam (related or not), this script is the answer. Another case of use of this script is if you want to read the conversations under a tweet WITHOUT having to go through all those fancam posts.

This is what this script does: blocking all people who post fancam (video) under a certain tweet. You'll need a Twitter developer account (apply here) with 'Read and Write permission'.

Paste the link to the tweet then run the script. Full script can be found on my github.

import tweepy
import re
import time
## You should already have a Twitter developer account first
consumer_key = "YOUR CONSUMER KEY HERE"
consumer_secret = "YOUR CONSUMER SECRET HERE"
access_token = "YOUR ACCESS TOKEN HERE"
access_token_secret = "YOUR ACCESS TOKEN SECRET"
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)
twtlink = '' # put the link of the tweet under which you want to block all the replies with fancam, place inside the ' '
regex = r'(?=twitter\.com\/(\w+)\/status\/(\d+))'
twtid = int(re.search(regex, twtlink).group(2))
user_name = re.search(regex, twtlink).group(1)
tweets = tweepy.Cursor(api.search, q='to:{} filter:replies'.format(user_name), tweet_mode='extended', since_id=twtid).items()
items = []
blocklist = []
username = set()
for tweet in tweets:
if not hasattr(tweet,'in_reply_to_status_id'):
continue
if tweet.in_reply_to_status_id == twtid:
items.append(tweet.id)
username.add(tweet.user.id)
if not hasattr(tweet, 'extended_entities'):
continue
elif tweet.extended_entities['media'][0]['type'] == 'video':
api.create_block(id=tweet.user.id)
time.sleep(10)
blocklist.append(tweet.id)
print(f'Number of all reply under the status: {len(items)}')
print(f'Number of people replying under the status: {len(username)}')
print(f'Number of reply under the status with fancam: {len(blocklist)}')

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more