Search code examples
pythongitgithubgithub-api

Add all the repos from a user using git in python


I have a function: def get_repos(user). What I want is to add that user's public repo names from Github to a set() in Python. Is there a way to do this?


Solution

  • Yes, as illustrated by "Build a Dynamic Portfolio With the Github API" from Ramki Pitchala, using, as commented, the Users REST API:

    def getRepos():
        try:
            url = "https://api.github.com/users/Ramko9999/repos"
            headers = {"Accept":"application/vnd.github.mercy-preview+json"}
            repos = requests.get(url, headers=headers, auth=(USERNAME,TOKEN)).json()
            projects = []
            for repo in repos:
                if repo["homepage"]:
                    project = {
                        "id": repo["id"],
                        "name": repo["name"],
                        "url": repo["html_url"],
                        "description": repo["description"],
                        "topics":repo["topics"],
                        "images": repo["homepage"].split(";")
                    }
                    projects.append(project)
            return {"projects": projects, "error": False}
        except Exception as e:
            return {"error": True, "message": str(e)}
    

    (Replace Ramko9999 by the actual username you need)