import requests, re
from bs4 import BeautifulSoup
from unidecode import unidecode

def id_generator(player_name):
    player = unidecode(player_name)
    if '.' in player_name:
        player = player.replace('.', '')
    if 'ö' in player_name:
        player = player.replace('ö', 'o')
    if 'Bam Adebayo' in player:
        player = '<NAME>'
    
    player_split = player.lower().split()

    if player_split[1] == 'kleber':
        return 'klebima01'
    elif player_split[1] == 'ntilikina':
        return 'ntilila01'
    elif player_name == "<NAME>. / <NAME>":
        return 'martike04'
    elif player_name == "<NAME>.":
        return 'porteke02'
    else:
        bbref_id = player_split[1][:5] + player_split[0][:2] + '01'
        # print(player_name)
        url = f'https://www.basketball-reference.com/players/{bbref_id[0]}/{bbref_id}.html'
        r = requests.get(url)
        soup = BeautifulSoup(r.content, 'html.parser')
        name_check = re.search(">(.+?)<", str(soup.find("h1",   {'itemprop':"name"}))).group(1)
        if unidecode(name_check.lower()) in unidecode(player_name.lower()):
            return bbref_id
        elif unidecode(name_check.lower()) in unidecode(player.lower()):
            return bbref_id
        else:
            for i in range(2,10):
                bbref_id = bbref_id[:-1] + str(i)
                url = f'https://www.basketball-reference.com/players/{bbref_id[0]}/{bbref_id}.html'
                r = requests.get(url)
                # print(player_name, r.status_code)
                if r.status_code != 200:
                    return False, player
                else:
                    soup = BeautifulSoup(r.content, 'html.parser')
                    name_check = re.search(">(.+?)<", str(soup.find("h1",   {'itemprop':"name"}))).group(1)
                    if name_check.lower() in player_name.lower():
                        return bbref_id
            return False, player



if __name__ == "__main__":
    players = ['<NAME>',
            '<NAME>',
            '<NAME> Jr. / K.J. Martin',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> (Hildred)',
            "De'<NAME>",
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> Jr.',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> III',
            '<NAME>',
            '<NAME>',
            '<NAME>-Smith',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> / <NAME>',
            '<NAME> / <NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> / <NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> / <NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> / <NAME>',
            '<NAME>',
            '<NAME> / <NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> / <NAME>',
            '<NAME> Jr.',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            'Jrue Holiday',
            '<NAME>',
            '<NAME> Jr.',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> / <NAME>',
            '<NAME>',
            '<NAME>',
            'La<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> / <NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>-Stein',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> / <NAME>',
            '<NAME> / <NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> Jr.',
            '<NAME> Jr.',
            '<NAME>',
            '<NAME>',
            '<NAME> Jr.',
            '<NAME>',
            '<NAME> / <NAME>',
            "<NAME> / <NAME>",
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> Jr.',
            '<NAME>',
            "<NAME>",
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> / <NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            "<NAME>",
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>']


    for player in players:
        print(id_generator(player))
import requests, re
from bs4 import BeautifulSoup
from unidecode import unidecode

def id_generator(player_name):
    player = unidecode(player_name)
    if '.' in player_name:
        player = player.replace('.', '')
    if 'ö' in player_name:
        player = player.replace('ö', 'o')
    if 'Bam Adebayo' in player:
        player = '<NAME>'
    
    player_split = player.lower().split()

    if player_split[1] == 'kleber':
        return 'klebima01'
    elif player_split[1] == 'ntilikina':
        return 'ntilila01'
    elif player_name == "<NAME>. / <NAME>":
        return 'martike04'
    elif player_name == "<NAME>.":
        return 'porteke02'
    else:
        bbref_id = player_split[1][:5] + player_split[0][:2] + '01'
        # print(player_name)
        url = f'https://www.basketball-reference.com/players/{bbref_id[0]}/{bbref_id}.html'
        r = requests.get(url)
        soup = BeautifulSoup(r.content, 'html.parser')
        name_check = re.search(">(.+?)<", str(soup.find("h1",   {'itemprop':"name"}))).group(1)
        if unidecode(name_check.lower()) in unidecode(player_name.lower()):
            return bbref_id
        elif unidecode(name_check.lower()) in unidecode(player.lower()):
            return bbref_id
        else:
            for i in range(2,10):
                bbref_id = bbref_id[:-1] + str(i)
                url = f'https://www.basketball-reference.com/players/{bbref_id[0]}/{bbref_id}.html'
                r = requests.get(url)
                # print(player_name, r.status_code)
                if r.status_code != 200:
                    return False, player
                else:
                    soup = BeautifulSoup(r.content, 'html.parser')
                    name_check = re.search(">(.+?)<", str(soup.find("h1",   {'itemprop':"name"}))).group(1)
                    if name_check.lower() in player_name.lower():
                        return bbref_id
            return False, player



if __name__ == "__main__":
    players = ['<NAME>',
            '<NAME>',
            '<NAME> Jr. / K.J. Martin',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> (Hildred)',
            "De'<NAME>",
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> Jr.',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> III',
            '<NAME>',
            '<NAME>',
            '<NAME>-Smith',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> / <NAME>',
            '<NAME> / <NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> / <NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> / <NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> / <NAME>',
            '<NAME>',
            '<NAME> / <NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> / <NAME>',
            '<NAME> Jr.',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            'Jrue Holiday',
            '<NAME>',
            '<NAME> Jr.',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> / <NAME>',
            '<NAME>',
            '<NAME>',
            'La<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> / <NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>-Stein',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> / <NAME>',
            '<NAME> / <NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> Jr.',
            '<NAME> Jr.',
            '<NAME>',
            '<NAME>',
            '<NAME> Jr.',
            '<NAME>',
            '<NAME> / <NAME>',
            "<NAME> / <NAME>",
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> Jr.',
            '<NAME>',
            "<NAME>",
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME> / <NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            "<NAME>",
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>',
            '<NAME>']


    for player in players:
        print(id_generator(player))
# print(player_name) # print(player_name, r.status_code)