GitHub Examples

Examples using theGitHub object.

Assumptions

I’ll just make some basic assumptions for the examples on this page. First,let’s assume that all you ever import from github3.py islogin andGitHub and that you have already received yourGitHubobjectg. That might look like this:

fromgithub3importlogin,GitHubfromgetpassimportgetpass,getuserimportsystry:importreadlineexceptImportError:passtry:user=input('GitHub username: ')orgetuser()exceptKeyboardInterrupt:user=getuser()password=getpass('GitHub password for{0}: '.format(user))# Obviously you could also prompt for an OAuth tokenifnot(userandpassword):print("Cowardly refusing to login without a username and password.")sys.exit(1)g=login(user,password)

So anywhere you seeg used, you can safely assume that it is an instancewhere a user has authenticated already.

For the cases where we do not need an authenticated user, or where we aretrying to demonstrate the differences between the two, I will useanon.anon could be instantiated like so:

anon=GitHub()

Also let’s define the following constants:

sigma='sigmavirus24'github3='github3.py'todopy='Todo.txt-python'kr='kennethreitz'requests='requests'

We may not need all of them, but they’ll be useful

Adding a new key to your account

try:path=input('Path to key: ')exceptKeyboardInterrupt:path=''try:name=input('Key name: ')exceptKeyboardInterrupt:name=''ifnot(pathandname):# Equivalent to not path or not nameprint("Cannot create a new key without a path or name")sys.exit(1)withopen(path,'r')askey_file:key=g.create_key(name,key_file)ifkey:print('Key{0} created.'.format(key.title))else:print('Key addition failed.')

Deleting the key we just created

Assuming we still havekey from the previous example:

ifg.delete_key(key.id):print("Successfully deleted key{0}".format(key.id))

There would actually be an easier way of doing this, however, if we do have thekey object that we created:

ifkey.delete():print("Successfully deleted key{0}".format(key.id))

Creating a new repository

repo={}keys=['name','description','homepage','private','has_issues','has_wiki','has_downloads']forkeyinkeys:try:repo[key]=input(key+': ')exceptKeyboardInterrupt:passr=Noneifrepo.get('name'):r=g.create_repository(repo.pop('name'),**repo)ifr:print("Created{0} successfully.".format(r.name))

Create a commit to change an existing file

repo.file_contents('/README.md').update('commit message','file content'.encode('utf-8'))

Follow another user on GitHub

I’m cheating here and using most of the follow functions in one example

ifnotg.is_following(sigma):g.follow(sigma)ifnotg.is_subscribed(sigma,github3py):g.subscribe(sigma,github3py)ifg.is_subscribed(sigma,todopy):g.unsubscribe(sigma,todopy)forfollowering.iter_followers():print("{0} is following me.".format(follower.login))forfolloweeing.iter_following():print("I am following{0}.".format(followee.login))ifg.is_following(sigma):g.unfollow(sigma)

Changing your user information

Note that youcan not change your login name via the API.

new_name='J. Smith'blog='http://www.example.com/'company='Vandelay Industries'bio="""# J. SmithA simple man working at a latex factory"""ifg.update_user(new_name,blog,company,bio=bio):print('Profile updated.')

This is the same as:

me=g.me()# or me = g.user(your_user_name)ifme.update(new_name,blog,company,bio=bio):print('Profile updated.')