Get GitHub information using PyGithub

Hi, this is @ yshr10ic.

Recently, as an output goal, I've been trying to make more than one commit per day on GitHub. (Of course, I know that committing shouldn't be the goal.)

Since I commit every day, I wanted to visualize how much I committed. So, when I was investigating how to get the commit log of GitHub, I found out that PyGithub exists.

In this article, I would like to briefly summarize how to use PyGithub.

What is PyGithub

PyGitHub is a Python library to access the GitHub API v3 and Github Enterprise API v3. This library enables you to manage GitHub resources such as repositories, user profiles, and organizations in your Python applications.

A library for accessing GitHub API v3 and Github Enterprise API v3. You will be able to manage GitHub resources such as repositories, user profiles, and organizations in your Python application.

Install PyGithub

$ pip install PyGithub

In my environment, the version is 1.47.

How to use PyGithub

Partial excerpt from PyGithub --Examples.

Create a GitHub instance

create_instance.py


from github import Github

#Instantiation by user name and password
g = GitHub('username', 'password')

#Instance generation by access token
g = Github('access_token')

#Instantiate a GitHub enterprise with a custom host
g = Github(base_url='https://{hostname}/api/v3', login_or_token='access_token')

Get a list of repositories

get_repos.py


for repo in g.get_user().get_repos():
    print(repo)

output

Repository(full_name="yshr10ic/deep-learning-from-scratch")
Repository(full_name="yshr10ic/deep-learning-from-scratch-2")
...

Get the number of stars

get_count_of_stars.py


repo = g.get_repo('yshr10ic/deep-learning-from-scratch-2')
print(repo.stargazers_count)

output

1

Get a list of branches

get_branches.py


repo = g.get_repo('yshr10ic/deep-learning-from-scratch-2')
for branch in repo.get_branches():
    print(branch)

output

Branch(name="images")
Branch(name="master")

Get a list of committed files

get_committed_files.py


repo = g.get_repo('yshr10ic/sample')
for commit in repo.get_commits():
    print(commit.files)

output

[File(sha="xxx", filename="django/djangorestframework/tutorial/tutorial/urls.py")]
[File(sha="yyy", filename="django/djangorestframework/tutorial/tutorial/quickstart/views.py")]
[File(sha="zzz", filename="django/djangorestframework/tutorial/tutorial/quickstart/serializers.py")]
...

Summary

This time, I only got the information of the existing GitHub repository, but in reality, I can also create files, pull requests, issues, etc.

I tried everything from creating a GitHub instance to getting various information, and I'm glad I was able to get the information very easily. However, in order to achieve the original purpose of "I want to visualize how many commits I have made", I have to be able to search the commit log by time. With PyGithub, it is unlikely that you can search by time, so I will consider other means.

Recommended Posts

Get GitHub information using PyGithub
Get GitHub information using PyGithub
Get note information using Evernote SDK for Python 3
[Linux] Get Linux resource information
[google-oauth] Get user information
[Rails] How to get location information using Geolocation API
Get similar posts using Doc2Vec
I tried to get Web information using "Requests" and "lxml"
Calculate information gain using NLTK
Get Alembic information with Python
Call github api in python to get pull request information
[Python] I tried to get various information using YouTube Data API!
Get video file information with ffmpeg-python
Get Salesforce data using REST API
Get Amazon data using Keep API # 1 Get data
Try to get statistics using e-Stat
Get data from Twitter using Tweepy
Backtrace using DWARF information and pyelftools
Get the file path using Pathlib
Extract zip code information using spark
Get weather information with Python & scraping