Python 3 Github REST API V3 Example to Search Repositories by Username Using pygithub Library Full Project For Beginners

 

 

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

 

 

 

Install

 

 

pip install pygithub

 

 

Simple Demo

app.py
from github import Github

# First create a Github instance:

# using an access token
g = Github("access_token")

# Github Enterprise with custom hostname
g = Github(base_url="https://{hostname}/api/v3", login_or_token="access_token")

# Then play with your Github objects:
for repo in g.get_user().get_repos():
    print(repo.name)

 

 

 

Get current user

>>> user = g.get_user()
>>> user.login
u'sfdye'

Get user by name

>>> user = g.get_user("sfdye")
>>> user.name
u'Wan Liuyang'

Get repository by name

>>> repo = g.get_repo("PyGithub/PyGithub")
>>> repo.name
u'PyGithub'

Get organization by name

>>> org = g.get_organization("PyGithub")
>>> org.login
u'PyGithub'

Search repositories by language

>>> repositories = g.search_repositories(query='language:python')
>>> for repo in repositories:
...    print(repo)
...
Repository(full_name="vinta/awesome-python")
Repository(full_name="donnemartin/system-design-primer")
Repository(full_name="toddmotto/public-apis")
Repository(full_name="rg3/youtube-dl")
Repository(full_name="tensorflow/models")
Repository(full_name="django/django")

Search repositories based on number of issues with good-first-issue

>>> repositories = g.search_repositories(query='good-first-issues:>3')
>>> for repo in repositories:
...    print(repo)
...
Repository(full_name="vuejs/vue")
Repository(full_name="facebook/react")
Repository(full_name="facebook/react-native")
Repository(full_name="electron/electron")
Repository(full_name="Microsoft/vscode")

Leave a Reply