Created
April 19, 2022 04:16
-
-
Save wybiral/ac11df88514a2cc06de8f7792d031062 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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