-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupload.py
52 lines (38 loc) · 1.31 KB
/
upload.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import os
import json
import argparse
import requests
from collections import OrderedDict
downloads_url = "https://api.github.com/repos/{user}/{repo}/downloads".format(
user="kyleconroy", repo="tmx2lua")
auth = ("kyleconroy", os.environ['GITHUB_PASSWORD'])
parser = argparse.ArgumentParser(description="Upload files to Github")
parser.add_argument("path", help="File to upload")
args = parser.parse_args()
name = os.path.basename(args.path)
downloads = json.loads(requests.get(downloads_url).text)
download = [d for d in downloads if d['name'] == name]
if download:
print "Deleting {}".format(download[0]['url'])
requests.delete(download[0]['url'], auth=auth).raise_for_status()
upload_info = {
'name': name,
'size': os.path.getsize(args.path),
}
resp = requests.post(downloads_url, data=json.dumps(upload_info), auth=auth)
resp.raise_for_status()
s3 = json.loads(resp.text)
files = {'file' : open(args.path, 'rb')}
s3_info = OrderedDict([
('key', s3['path']),
('acl', s3['acl']),
('success_action_status', 201),
('Filename', s3['name']),
('AWSAccessKeyId', s3['accesskeyid']),
('Policy', s3['policy']),
('Signature', s3['signature']),
('Content-Type', s3['mime_type']),
])
resp = requests.post(s3['s3_url'], data=s3_info, files=files)
resp.raise_for_status()
print resp.text