import requests
from bs4 import BeautifulSoup


def get_beta_url(model, build, version):
    """Get iOS beta URL from theiphonewiki."""

    major = f"{version.split('.')[0]}.x"
    matches = [build, 'Restore']

    # URL should look like :
    # https://www.theiphonewiki.com/wiki/Beta_Firmware/iPhone/14.x
    wiki_url = (
        f"https://www.theiphonewiki.com/wiki/Beta_Firmware/iPhone/{major}"
    )
    html_text = requests.get(wiki_url).text
    soup = BeautifulSoup(html_text, 'html.parser')
    found = False
    for link in soup.find_all('a'):
        href = link.get('href')
        if href is None:
            continue

        if model in href:
            found = True

        if found is True and all(x in href for x in matches):
            return href
    return None