import requests
from bs4 import BeautifulSoup


def prelimSeeds(URL: str) -> dict:
    """Parses the prelim seeds page of a tournament

    Args:
        URL (str): the URL of the prelim seeds page of a certain division

    Returns:
        dict: a dict containing the parsed prelim data

        RETURN SCHEMA:
        {
            <(str) team code> : [
                <(int) prelim rank>,
                <(int) total # of prelim teams>
            ],
            ...
        }
    """
    data = {}

    # Getting page and setting up parser
    r = requests.get(URL)
    soup = BeautifulSoup(r.text, "html.parser")

    # Getting all rows except for the first header
    rawData = soup.find_all("tr")
    rawData = rawData[1:len(rawData)]

    # Getting number of entries
    numEntries = len(rawData)

    for r, element in enumerate(rawData, start = 1):

        rawEntryData = element.find_all("td")
        textData = []

        for node in rawEntryData:
            nodeText = node.get_text().replace('\t','').split('\n')
            textData.append(nodeText)
        try:
            pos = int(textData[0][1])
            code = textData[1][1]
            data[code] = [pos, numEntries]


        except Exception as e:
            print(e)

    return data