Lo-Fi Python

Oct 10, 2022

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
  1. Run the below code as a Python script.
python github_stars.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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: http://lofipython.com/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:

View repo github stars with Python

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

Github browser UX

Github Repo Stargazer API Reference