Skip to content

Instantly share code, notes, and snippets.

@sixem
Last active April 26, 2019 03:52
Show Gist options
  • Save sixem/b358cd424fc5e7b4b4188371d9d5c276 to your computer and use it in GitHub Desktop.
Save sixem/b358cd424fc5e7b4b4188371d9d5c276 to your computer and use it in GitHub Desktop.
Checks whether or not twitch usernames are available.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Usage: python twitch-name-checker.py list.txt
import requests
import sys
def main():
available_usernames = []
batch_list = read_batch(sys.argv[1])
if len(batch_list) > 0:
print "{}Checking {} usernames{}".format(p_colors.OKGREEN, len(batch_list), p_colors.ENDC)
else:
print "{}Batch file is empty{}".format(p_colors.FAIL, p_colors.ENDC)
print ""
try:
for count, username in enumerate(batch_list):
status = check_name(username)
if status['status_code'] == 204:
print p_colors.OKGREEN, status['username'], "is available.", status['status_code'], p_colors.ENDC
available_usernames.append(username)
else:
print p_colors.FAIL, status['username'], "is not available.", status['status_code'], p_colors.ENDC
except KeyboardInterrupt:
sys.exit(1)
print ""
if len(available_usernames) > 0:
print "Check complete,", len(available_usernames), "names are available:"
for user in available_usernames:
print p_colors.OKGREEN, user, p_colors.ENDC
else:
print "Check complete, 0/{} names are available.".format(len(batch_list))
def check_name(username):
r = requests.head("https://passport.twitch.tv/usernames/" + username,
headers={'Connection':'close'})
if r.status_code == 403:
success = False
while success == False:
r = requests.head("https://passport.twitch.tv/usernames/" + username,
headers={'Connection':'close'})
if r.status_code != 403:
success = True
if r.status_code == 200:
return {'username': username, 'taken': True, 'status_code': r.status_code}
else:
return {'username': username, 'taken': False, 'status_code': r.status_code}
def read_batch(path):
batch_list = []
with open(path) as f:
lines = f.readlines()
for line in lines:
batch_list.append(line.rstrip())
return batch_list
class p_colors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment