python利用github的api实现文件的上传和更新

python利用官方的api实现文件的上传和更新。

申请github的tokens

根据官方api说明进行设定

上传新文件代码实例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import requests,base64,random,time,json

def creat_file(strx=''):
randx=random.randint(1000, 9999)
fn= ''+ time.strftime("%Y%m%d%H%M%S", time.localtime())+str(randx)+'.txt'
with open(fn, 'a+') as f:
f.write(fn + '\n') # 加\n换行显示
f.write(strx)
return fn

def fn_base64(fn):
# 打开本地图片,并转化为base64
f = open(fn, 'rb')
fnb64 = base64.b64encode(f.read()).decode('utf-8')
return fnb64

tokens='6b987297b89c4f8e0'#

fn=creat_file('sxs')
url = "https://api.github.com/repos/[user]/[proj]/contents/[path]/"+ fn
# d = {
"message": "my commit message",
"committer": {
"name": "user",
"email": "user@163.com"
},
"content": fn_base64(fn)
}


headers = {"Authorization": 'token '+tokens} # 前两行会在后面的代码中忽略掉不写
r = requests.put(url=url,data=json.dumps(d), headers=headers)
rs=r.status_code
if rs==201:
print('sucess')
#出现422错误,有可能是文件在项目中已经存在了。

获取sha为update更新文件做准备

1
2
3
4
5
6
7
#get content
url = "https://api.github.com/repos/[user]/[proj]/contents/[path]/"+ fn
r=requests.get(url=url,headers=headers)
if r.status_code==200:
sha=json.loads(r.text)['sha']
else:
sha=''

更新文件,需要核对文件名和sha一致时候方可进行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
d={
"message": "my commit message update",
"committer": {
"name": "Scott Chacon",
"email": "schacon@gmail.com"
},
"content": fn_base64(fn),
"sha":sha
}
url = "https://api.github.com/[user]/[proj]/contents/[path]/"+ fn

headers = {"Authorization": 'token '+tokens} # 前两行会在后面的代码中忽略掉不写
r = requests.put(url=url,data=json.dumps(d), headers=headers)

print(r.status_code)

#200说明成功

添加comment

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#!/usr/bin/env python 
# -*- coding:utf-8 -*-
"""info

"""
import requests,base64,random,time,json
tokens='7b89c4f8e0'#
url = "https://api.github.com/repos/[user]/[proj]/issues/[n]/comments"

d={
"body": "i am a boy"
}

headers = {"Authorization": 'token '+tokens} # 前两行会在后面的代码中忽略掉不写
r = requests.post(url=url,data=json.dumps(d), headers=headers)

rs=json.loads(r.text)
print(r.status_code)
print(rs)

饮水思源