Retrieve Github user avatar with Github API

import time

import requests

headers = {
    "Authorization": "Bearer GITHUB_OAUTH_KEY"
    }


fetched_names = []

for i in range(100):
    print("="*16)
    print(i)
    print("="*16)
    resp = requests.get("https://api.github.com/repos/ucb-bar/chipyard/commits?per_page=100&page={page}".format(page=i), headers=headers)
    commits = resp.json()

    time.sleep(0.1)
    if not commits:
        break

    for commit in commits:
        author = commit.get("author")
        if not author:
            continue
        
        username = author.get("login")
        name = commit.get("commit").get("author").get("name")

        print(username, "-", name)
        if name in fetched_names:
            continue
        print("  downloading...")

        r = requests.get("https://github.com/{username}.png?size=90".format(username=username), headers=headers, allow_redirects=True)
        open(".git\\avatar\\{name}.png".format(name=name), "wb").write(r.content)
        fetched_names.append(name)

        time.sleep(0.1)

print("finished")

Last updated