Skip to content

Consider using ghapi #28

@matthewfeickert

Description

@matthewfeickert

As an alternative to PyGithub, https://github.com/fastai/ghapi provides nice pagination that could make it easier to work with and gives access to the full GitHub API. This should also allow for the ability to go and get the full star history of any project.

Example: This code gets all of the GitHub star times and dates for pyhf

import json

import matplotlib.dates as mdates
import matplotlib.pyplot as plt
import pandas as pd
from ghapi.all import GhApi, pages

with open(".secrets.json") as read_file:
    api = GhApi(token=json.load(read_file)["api_token"])

owner = "scikit-hep"
repo = "pyhf"
per_page = 30

api.activity.list_stargazers_for_repo(owner, repo, per_page=per_page)
n_pages = api.last_page()

all_pages = pages(
    api.activity.list_stargazers_for_repo,
    n_pages,
    owner,
    repo,
    per_page=per_page,
    headers={"Accept": "application/vnd.github.v3.star+json"},
)

data = [{"date": item["starred_at"], "count": 1} for page in all_pages for item in page]
df = pd.DataFrame(data)

df["date"] = pd.to_datetime(df["date"])
df = df.resample("W-Mon", on="date").sum().cumsum()
df["repo"] = repo

fig, ax = plt.subplots()

time_fmt = mdates.AutoDateLocator()
ax.xaxis.set_major_locator(time_fmt)

ax.plot(df.index, df["count"], label=repo)
ax.legend(loc="best", frameon=False)

ax.set_xlabel("Date", size=14)
ax.set_ylabel("Number of GitHub Stars", size=14)

fig.tight_layout()

file_types = ["png", "pdf", "svg"]
for extension in file_types:
    fig.savefig(f"ghapi_{repo}_star_history.{extension}", facecolor="white")

ghapi_pyhf_star_history

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions