Skip to content

Instantly share code, notes, and snippets.

@redknight99
Created November 17, 2019 12:35
Show Gist options
  • Save redknight99/bd1f09d02a46acbfcf959c98eb4fad56 to your computer and use it in GitHub Desktop.
Save redknight99/bd1f09d02a46acbfcf959c98eb4fad56 to your computer and use it in GitHub Desktop.
Background: I encountered an interesting Twitter thread where someone was trying to figure out how to mass unblock. This gist is adapted from one of the replies. Essentially it uses api oauth tokens to get that user's block user list and then unblocks them one by one.
import json
import oauth2 as oauth
import requests
import urllib
CONSUMER_KEY = "CHANGEME"
CONSUMER_SECRET = "CHANGEME"
ACCESS_KEY = "CHANGEME"
ACCESS_SECRET = "CHANGEME"
def get_user_block_list():
consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
access_token = oauth.Token(key=ACCESS_KEY, secret=ACCESS_SECRET)
client = oauth.Client(consumer, access_token)
block_list_endpoint = "https://api.twitter.com/1.1/blocks/list.json"
options = "?skip_status=true&include_entities=false&cursor=-1"
block_list_endpoing = block_list_endpoing + options
response, data = client.request(block_list_endpoint, method='GET')
user_list = json.loads(data)
blocked_users_list = []
for user in user_list["users"]:
blocked_users_list.append(user["screen_name"])
if user_list["next_cursor"] != 0:
more_accounts_to_get = True
current_cursor = user_list["next_cursor"]
while more_accounts_to_get == True:
block_list_endpoint = "https://api.twitter.com/1.1/blocks/list.json"
options = "?skip_status=true&include_entities=false&cursor=" + str(current_cursor)
block_list_endpoint = block_list_endpoint + options
response, data = client.request(block_list_endpoint, method='GET')
user_list = json.loads(data)
for user in user_list["users"]:
blocked_users_list.append(user["screen_name"])
if user_list["next_cursor"] == 0:
more_accounts_to_get = False
else:
current_cursor = user_list["next_cursor"]
return blocked_users_list
def unblock_user_list(blocked_user_list):
consumer = oauth.Consumer(key=CONSUMER_KEY, secret=CONSUMER_SECRET)
access_token = oauth.Token(key=ACCESS_KEY, secret=ACCESS_SECRET)
client = oauth.Client(consumer, access_token)
unblock_list_endpoint = "https://api.twitter.com/1.1/blocks/destroy.json"
for screen_name in blocked_user_list:
print("Now attempting to unblock: " + str(screen_name))
payload = {"screen_name": screen_name,
"include_entities": False,
"skip_status": True}
response, data = client.request(unblock_list_endpoint,
body=urllib.urlencode(payload),
method='POST')
return "Success!"
if __name__ == '__main__':
list_of_blocked_users = get_user_block_list()
un_block_result = unblock_user_list(list_of_blocked_users)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment