Since you mentioned you are a beginner python programmer, I would suggest you to try to use the JSON API without any Github library first. It really isn't that difficult and it will help you a lot later in your programming life since same approach can be applied to any JSON API. Especially if it seems that trying out libraries will take days.
I'm not saying that some library isn't easier to use, I'm just saying the small extra effort to use the API directly might be worth it in the long run. At least it will help you understand why some of those libraries seem "unintuitive" (as you said).
Simple example to fetch creation time of django repository:
import requests
import json
r = requests.get('https://api.github.com/repos/django/django')
if(r.ok):
repoItem = json.loads(r.text or r.content)
print "Django repository created: " + repoItem['created_at']
This is using the popular requests library. In your code you'll naturally need to handle the error cases too.
If you need access with authentication it will be a bit more complex.
Answer from Lycha on Stack OverflowSince you mentioned you are a beginner python programmer, I would suggest you to try to use the JSON API without any Github library first. It really isn't that difficult and it will help you a lot later in your programming life since same approach can be applied to any JSON API. Especially if it seems that trying out libraries will take days.
I'm not saying that some library isn't easier to use, I'm just saying the small extra effort to use the API directly might be worth it in the long run. At least it will help you understand why some of those libraries seem "unintuitive" (as you said).
Simple example to fetch creation time of django repository:
import requests
import json
r = requests.get('https://api.github.com/repos/django/django')
if(r.ok):
repoItem = json.loads(r.text or r.content)
print "Django repository created: " + repoItem['created_at']
This is using the popular requests library. In your code you'll naturally need to handle the error cases too.
If you need access with authentication it will be a bit more complex.
In the end, I ended up using PyGithub. It works well, and the author is really receptive for feedback and bug reports. :-)
(Adapted from my edit to the original question, for better visibility)
Videos
I'm new to python and have been ramping up recently. The information at realpython.com is invaluable!
I'm trying to make some GitHub integrations and am using the PyGithub api/package/module (I'm unsure of the nomenclature but you get it). I've not yet had too much experience with python api docs, but this seems a bit difficult to parse.
I'm able to eek by using the lsp to help describe the methods/signatures and attribs. But I do need help understanding how to read this documentation. I see some information using the examples, but it's leading to more questions than answers.
Edit: Specifically, I am having difficulty understanding how the chain of actions work. It is not very clear what methods return what, nor is it clear which methods take input, and what input it is.
The solution to my question is the following
g = Github(token)
user = g.get_user()
repo = user.create_repo(full_name)
I stumbled across this question trying to figure out how to coax PyGithub into creating a Repository within an Organization and thought it would be relevant here.
g = Github(token)
organization = g.get_organization("org-name")
organization.create_repo(
name,
allow_rebase_merge=True,
auto_init=False,
description=description,
has_issues=True,
has_projects=False,
has_wiki=False,
private=True,
)
The full set of keyword arguments may be found here: https://developer.github.com/v3/repos/#input
» pip install PyGithub
I have created a small set of code for this. And it worked. Sharing it here, so others can get benefit from it.
Sample code:
This code will upload a file/replace the existing file.
Local file path: /tmp/file.txt
Github folder name: folder1/
from github import Github
g = Github("username", "password")
repo = g.get_user().get_repo(GITHUB_REPO)
all_files = []
contents = repo.get_contents("")
while contents:
file_content = contents.pop(0)
if file_content.type == "dir":
contents.extend(repo.get_contents(file_content.path))
else:
file = file_content
all_files.append(str(file).replace('ContentFile(path="','').replace('")',''))
with open('/tmp/file.txt', 'r') as file:
content = file.read()
# Upload to github
git_prefix = 'folder1/'
git_file = git_prefix + 'file.txt'
if git_file in all_files:
contents = repo.get_contents(git_file)
repo.update_file(contents.path, "committing files", content, contents.sha, branch="master")
print(git_file + ' UPDATED')
else:
repo.create_file(git_file, "committing files", content, branch="master")
print(git_file + ' CREATED')
This question is also related to this.
(note : not tested)
I think the code you linked does what you want.
This gist, written in ruby, shows a very similar scenario, but has the additional benefit of explicitly naming the API routes queried for each action (create_tree, create_commit, ...)
Check the docs for the PyGithub library : the methods are most probably wrappers around the same API calls.