How to Check Github Repo Star Counts With Python

Snooping through my package list, I noticed the PyGithub library was installed. Its repo boasts “Typed interactions with the GitHub API v3”. I googled the package, wanting to check in on the repos I profiled in an earlier post about static site generators.

I drafted the code below after noticing the repo.stargazer_count function in its documentation. This is neat to have if you want to keep tabs on a batch of repos, instead of tediously checking the Github web interface! If you’re new to Github, the trending page is an easy way to find new, interesting repos.

Getting Started

  1. You’ll need to create a personal access token for your Github account. See the Github docs, “Creating a personal access token”.
  2. Install PyGithub and pandas:
pip install PyGithub
pip install pandas

3. Run the below code as a Python script.

python github_stars.py
import pandas as pd
from github import Github


def stars(repo, g):
    """Retrieve github repo star count.
    Accepts: str, repo "username/repo name",ex: "getpelican/pelican"
    Returns: int, github repo stargazers number"""
    repo = g.get_repo(repo)
    return repo.stargazers_count


# static site repos: https://pythonmarketer.com/2021/07/lc28/a-brief-summary-of-promising-python-static-site-generators/
urls = [
    "https://github.com/getpelican/pelican",
    "https://github.com/lektor/lektor",
    "https://github.com/eudicots/Cactus",
    "https://github.com/getnikola/nikola",
    "https://github.com/sunainapai/makesite",
    "https://github.com/hyde/hyde",
    "https://github.com/Anomareh/mynt",
    "https://github.com/staticjinja/staticjinja",
]
repos = [url.replace("https://github.com/", "") for url in urls]
g = Github("access_token")
counts = [(repo, stars(repo, g)) for repo in repos]
stars_df = pd.DataFrame(counts, columns=["repo","stars"])
stars_df.to_csv("Stars.csv", index=False)

On Linux, I was able to check the results of the CSV with the cat command:

I confirmed the API was accurate against the web interface in pelican’s repo!

Github Repo Stargazer API Reference

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.