import requests


def get_github_from_pypi(package, version):
    """
    Make a request to pypi to get the github repo for the given package and version
    :param package: PyPI package
    :param version: Package version
    :return: Owner and github repo of package
    """
    res = requests.get("https://pypi.python.org/pypi/{}/{}/json".format(package, version))
    if res.status_code != 200:
        print(res.text)
        raise Exception("Got error requesting from pypii")

    # Parse out the package home page
    res_json = res.json()
    home_page = res_json['info']['home_page']

    if "github.com" not in home_page:
        # If not a github repo just return what was given
        return home_page
    else:
        # Parse out the owner and package of github repo
        home_page = home_page.replace("https://github.com/", "")
        home_page = home_page.replace("http://github.com", "")
        split_b = home_page.split("/")
        github = {
            "owner": split_b[0],
            "package": split_b[1]
        }
        return github