Skip to content

Instantly share code, notes, and snippets.

@wybiral
Created April 19, 2022 04:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wybiral/ac11df88514a2cc06de8f7792d031062 to your computer and use it in GitHub Desktop.
Save wybiral/ac11df88514a2cc06de8f7792d031062 to your computer and use it in GitHub Desktop.
from argparse import ArgumentParser
from time import sleep
try:
import twitter
except:
print('Requires python-twitter: pip install python-twitter')
exit(1)
api = twitter.Api(
consumer_key='...',
consumer_secret='...',
access_token_key='...',
access_token_secret='...',
)
ap = ArgumentParser(description='Block followers of an account.')
ap.add_argument(
'screen_name',
type=str,
help='Screen name to block followers of',
)
ap.add_argument(
'-d',
'--delay',
default=0.5,
type=float,
help='Delay between block requests in seconds (to avoid API rate limits)',
)
ap.add_argument(
'-c',
'--cursor',
default=-1,
type=int,
help='Starting point of cursor (used to resume interrupted progress)',
)
args = ap.parse_args()
screen_name = args.screen_name
cursor = args.cursor
last_cursor = cursor
delay = args.delay
try:
print(cursor)
while cursor:
next_cursor, last_cursor, followers = api.GetFollowersPaged(
cursor=cursor,
count=100,
screen_name=screen_name,
)
cursor = next_cursor
for user in followers:
print('{} ({})'.format(user.name, user.screen_name))
try:
api.CreateBlock(user_id=user.id)
except twitter.error.TwitterError as e:
print(e)
pass
sleep(delay)
except KeyboardInterrupt:
pass
except Exception as e:
print(e)
# if results didn't finish, show last_cursor
if cursor:
print('\nLast cursor: {}'.format(last_cursor))
@Nrs2301
Copy link

Nrs2301 commented Jan 20, 2024

Code
Revisions
1
Stars
1
blockfollowers.py
from argparse import ArgumentParser
from time import sleep
try:
import twitter
except:
print('Requires python-twitter: pip install python-twitter')
exit(1)

api = twitter.Api(
consumer_key='...',
consumer_secret='...',
access_token_key='...',
access_token_secret='...',
)

ap = ArgumentParser(description='Block followers of an account.')

ap.add_argument(
'screen_name',
type=str,
help='Screen name to block followers of',
)

ap.add_argument(
'-d',
'--delay',
default=0.5,
type=float,
help='Delay between block requests in seconds (to avoid API rate limits)',
)

ap.add_argument(
'-c',
'--cursor',
default=-1,
type=int,
help='Starting point of cursor (used to resume interrupted progress)',
)

args = ap.parse_args()

screen_name = args.screen_name
cursor = args.cursor
last_cursor = cursor
delay = args.delay

try:
print(cursor)
while cursor:
next_cursor, last_cursor, followers = api.GetFollowersPaged(
cursor=cursor,
count=100,
screen_name=screen_name,
)
cursor = next_cursor
for user in followers:
print('{} ({})'.format(user.name, user.screen_name))
try:
api.CreateBlock(user_id=user.id)
except twitter.error.TwitterError as e:
print(e)
pass
sleep(delay)
except KeyboardInterrupt:
pass
except Exception as e:
print(e)

if results didn't finish, show last_cursor

if cursor:
print('\nLast cursor: {}'.format(last_cursor))
Comment

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