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.

Revisions

  1. redknight99 created this gist Nov 17, 2019.
    66 changes: 66 additions & 0 deletions simple_mass_unblock_twitter.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,66 @@
    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)