filename = "Aitken, Alexander C..tex" encoded_filename = urllib.quote(filename) path = 'tex_files/' + encoded_filename data = {'ref': "changes"} ## Step ONE: get the current file so we can extract its hash ## this isn't the "canonical" way to go about making a commit ## described variously with five or seven steps: ## http://www.mdswanson.com/blog/2011/07/23/digging-around-the-github-api-take-2.html ## http://stackoverflow.com/a/14672793/821010 ## but this is a cheap and dirty method that will work without hassle. ## and the "proper" way to go about it can be investigated separately. resp = github.repos(owner, repo).contents(path).GET( auth=(user, password), headers={'Content-type': 'application/json'}, data=json.dumps(data)) sha = json.loads(resp._content)['sha'] ## Step TWO: add the new content data = { 'message': 'Programmatic update to "' + filename + '".', 'committer': { 'name': 'Joe Corneli', 'email': '*****@*****.**' }, 'content': base64.b64encode("Hello github, new content here."), 'sha': sha, 'branch': 'changes' }
file.close() #upload file to github with open(filename, "rb") as text_file: encoded_string = base64.b64encode(text_file.read()) data = { 'message': 'Adding "' + filename + '".', 'committer': { 'name': 'Madeleine Corneli', 'email': '*****@*****.**' }, 'content': encoded_string, 'branch': 'master' } github = Github('https://api.github.com') user = '******' password = raw_input("Github password:") repo = 'miniBibServer' resp = github.repos(user, repo).contents(filename).PUT( auth=(user, password), headers={'Content-type': 'textfile'}, data=json.dumps(data)) pprint(vars(resp)) #edit to test git :/
import filecmp from pprint import pprint github = Github('https://api.github.com') owner = 'maddyloo' repo = 'BibProject' user = '******' password = '******' #Github token goes here filename = "Aitken, Alexander C..tex" # Maps to AitkenAlexanderC as a "canonical name" basename = re.sub('[ ,.]', '', filename.split(".")[0]) ## Step ZERO: get a reference resp = github.repos(owner, repo).git.refs('heads/master').GET( auth = (user, password)) sha_latest_commit = json.loads(resp._content)['object']['sha'] ## Step ONE: create the branch derived from that reference data={"ref": "refs/heads/"+basename, "sha": sha_latest_commit} resp = github.repos(owner, repo).git.refs.POST( auth = (user, password), headers = {'Content-type': 'application/json'}, data=json.dumps(data)) pprint(resp._content)
from pprint import pprint # Let's create the first chain of hammock using base api url github = Github('https://api.github.com') user = '******' # In the future this will be running on a server # somewhere, so password can just be hard coded password = raw_input("Enter your github password: ") repo = 'py-blot' # The most interesting things for us are in this part of the API # https://developer.github.com/v3/repos/contents/ # get the contents of a given file, the README in this case resp = github.repos(user, repo).contents.GET('README.md') # examine the file we retrieved, # here's how to have a look at everything: #pprint(vars(resp)) # And from that, the most interesting part(s) can be extracted: text = base64.b64decode(json.loads(resp._content)['content']) # (we will need the sha later) sha = json.loads(resp._content)['sha'] print text # Now to further explore the API, let's loop back and # update the file. Let's prompt the user to add # some text to the file.
filename = "Aitken, Alexander C..tex" encoded_filename=urllib.quote(filename) path = 'tex_files/'+encoded_filename data={'ref':"changes"} ## Step ONE: get the current file so we can extract its hash ## this isn't the "canonical" way to go about making a commit ## described variously with five or seven steps: ## http://www.mdswanson.com/blog/2011/07/23/digging-around-the-github-api-take-2.html ## http://stackoverflow.com/a/14672793/821010 ## but this is a cheap and dirty method that will work without hassle. ## and the "proper" way to go about it can be investigated separately. resp = github.repos(owner, repo).contents(path).GET( auth = (user, password), headers = {'Content-type': 'application/json'}, data=json.dumps(data)) sha = json.loads(resp._content)['sha'] ## Step TWO: add the new content data={'message':'Programmatic update to "' + filename + '".', 'committer':{'name':'Joe Corneli', 'email':'*****@*****.**'}, 'content':base64.b64encode("Hello github, new content here."), 'sha':sha, 'branch':'changes'} # Forming the correct URL (by following the instructions from Hammock docs!) # In particular, we need to specify the :path as part of the url, whereas # other arguments are passed along as a JSON body
if __name__ == "__main__": reload(sys) sys.setdefaultencoding("utf-8") #write each headline to new line file.write(span.string + "\n") file.close() #upload file to github with open( filename, "rb") as text_file: encoded_string = base64.b64encode(text_file.read()) data = {'message':'Adding "'+filename+'".', 'committer':{'name':'Madeleine Corneli', 'email':'*****@*****.**'}, 'content':encoded_string, 'branch':'master'} github = Github('https://api.github.com') user = '******' password = raw_input("Github password:") repo = 'miniBibServer' resp = github.repos(user, repo).contents(filename).PUT( auth = (user, password), headers = {'Content-type': 'textfile'}, data = json.dumps(data)) pprint (vars(resp)) #edit to test git :/
user = '******' password = raw_input("Github password:") repo = 'miniBibServer' resp = github.repos(user, repo).contents(filename).PUT( auth = (user, password), headers = {'Content-type': 'textfile'}, data = json.dumps(data)) pprint (vars(resp)) ''' #edit to test git :/ #data = {'owner':'maddyloo', # #might need full path: # 'path':'/master/test_LaTeXML/corneli-citations.bib', # 'repo':'miniBibServer'} github = Github('https://api.github.com') owner = 'maddyloo' repo = 'miniBibServer' path = 'test_LaTeXML/corneli-citations.bib' #GET /repos/:owner/:repo/contents/:path resp = github.repos(owner, repo).contents(path).GET( #auth = (user, password), headers={'Content-type': 'textfile'}) #data = json.dumps(data)) #j = json.loads(resp.text) j = resp.json() print(base64.b64decode(j['content'])) #pprint(resp['_content'])
from hammock import Hammock as Github import json import urllib import sys import base64 from pprint import pprint github = Github('https://api.github.com') owner = 'maddyloo' repo = 'BibProject' path = 'tex_files/Aitken%2C%20Alexander%20C..tex' password = '' #BibPoject token #Aitken%2C Alexander C..tex #GET /repos/:owner/:repo/contents/:path resp = github.repos(owner, repo).contents(path).GET( auth = (owner, password), headers = {'Content-type': 'textfile'}) #data = json.dumps(data)) #j = json.loads(resp.text) j = resp.json() print(base64.b64decode(j['content'])) #pprint(resp.content)
from hammock import Hammock as Github import json import urllib import sys import base64 import filecmp from pprint import pprint github = Github('https://api.github.com') owner = 'maddyloo' repo = 'BibProject' user = '******' password = '******' #Github token goes here filename = "Aitken, Alexander C..tex" data = {"title": "Update to " + filename, "body": "Please pull this in!", "head": "changes", "base": "master"} resp = github.repos(owner, repo).pulls.POST( auth = (user, password), headers = {'Content-type': 'application/json'}, data=json.dumps(data)) pprint(resp._content)
import json import urllib import sys import base64 import filecmp from pprint import pprint github = Github('https://api.github.com') owner = 'maddyloo' repo = 'BibProject' user = '******' password = '******' #Github token goes here ## Step ZERO: get a reference resp = github.repos(owner, repo).git.refs('heads/changes').GET( auth = (user, password)) sha_latest_commit = json.loads(resp._content)['object']['sha'] print sha_latest_commit ## Step ONE: get latest commit at that reference resp = github.repos(owner, repo).git.commits(sha_latest_commit).GET( auth = (user, password)) sha_base_tree = json.loads(resp._content)['tree']['sha'] print sha_base_tree ## Step TWO: make a new commit, which goes in several substeps ## Step A: update the tree filename = "Aitken, Alexander C..tex"
import sys import base64 import filecmp from pprint import pprint github = Github('https://api.github.com') owner = 'maddyloo' repo = 'BibProject' user = '******' password = '******' #Github token goes here filename = "Aitken, Alexander C..tex" # Maps to AitkenAlexanderC as a "canonical name" basename = re.sub('[ ,.]', '', filename.split(".")[0]) ## Step ZERO: get a reference resp = github.repos(owner, repo).git.refs('heads/master').GET(auth=(user, password)) sha_latest_commit = json.loads(resp._content)['object']['sha'] ## Step ONE: create the branch derived from that reference data = {"ref": "refs/heads/" + basename, "sha": sha_latest_commit} resp = github.repos(owner, repo).git.refs.POST( auth=(user, password), headers={'Content-type': 'application/json'}, data=json.dumps(data)) pprint(resp._content)
from hammock import Hammock as Github import json import urllib import sys import base64 import filecmp from pprint import pprint github = Github('https://api.github.com') owner = 'maddyloo' repo = 'BibProject' user = '******' password = '' #Github token goes here resp = github.repos(owner, repo).git.refs('heads/changes').GET(auth=(user, password)) sha_latest_commit = json.loads(resp._content)['object']['sha'] print sha_latest_commit ## We may also need this other sha? resp = github.repos(owner, repo).git.commits(sha_latest_commit).GET(auth=(user, password)) sha_base_tree = json.loads(resp._content)['tree']['sha'] print sha_base_tree