Skip to content

Instantly share code, notes, and snippets.

@geberl
Created February 7, 2019 12:55
Show Gist options
  • Save geberl/a1d2138141ac2494f111e078c17cd2b1 to your computer and use it in GitHub Desktop.
Save geberl/a1d2138141ac2494f111e078c17cd2b1 to your computer and use it in GitHub Desktop.
Query the GitHub API via the github3 Python module for repo info, own user info, other user info
#! /usr/bin/python
# -*- coding: utf-8 -*-
from github3 import login
# https://github3.readthedocs.io/en/latest/index.html#more-examples
def enter_2fa_code():
# Source: https://github3.readthedocs.io/en/latest/examples/two_factor_auth.html
code = ''
while not code:
# The user could accidentally press Enter before being ready, let's protect them from doing that.
code = raw_input('Enter 2FA code: ')
return code
gh = login('your-username',
password='your-password',
two_factor_callback=enter_2fa_code)
# Querying a 3rd party repo for info about its releases
my_repo = gh.repository(owner='nuxsmin', repository='sysPass')
print(my_repo) # nuxsmin/sysPass
print(my_repo.clone_url) # https://github.com/nuxsmin/sysPass.git
for n, rel in enumerate(my_repo.releases()):
print(rel) # <Release [sysPass 2.1.16]> ...
my_latest_release = my_repo.latest_release()
print(my_latest_release) # <Release [sysPass 2.1.16]>
print(my_latest_release.name) # sysPass 2.1.16
print(my_latest_release.published_at) # 2018-06-19 00:34:34+00:00
print(my_latest_release.tag_name) # 2.1.16.18061901
print(my_latest_release.zipball_url) # https://api.github.com/repos/nuxsmin/sysPass/zipball/2.1.16.18061901
# Get info about own account
my_account = gh.me()
print(my_account.name)
print(my_account.login)
print(my_account.followers_count)
for f in gh.followers():
print(str(f))
# Get info about another user
kennethreitz = gh.user('kennethreitz')
print(kennethreitz.name) # Kenneth Reitz
print(kennethreitz.login) # kennethreitz
print(kennethreitz.followers_count) # 23898
# TODO switch to token based auth, otherwise it's required to enter a 2fa code at each request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment