Created
March 23, 2021 11:23
-
-
Save dandrei279/1cb0ba93ae165e63be61b01d1e63ae7a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
''' | |
Author: Andrei David | |
Basic program to upload files to Github Gists | |
Go to [1] to create a token | |
[1] https://github.com/settings/tokens | |
''' | |
import json | |
import requests | |
import sys | |
GITHUB_API="https://api.github.com/gists" | |
API_TOKEN='github_token' | |
def printUsage(): | |
print('Usage: gist.py <option> File') | |
print('Options:') | |
print('\t-p:\tprivate gist (default)') | |
print('\t-P:\tPublic gist') | |
print('\t-h:\tprint this message') | |
exit(1) | |
if __name__ == "__main__": | |
# request headers | |
headers = { | |
'Authorization':'token ' + API_TOKEN | |
} | |
fileLoaded = False | |
payload = {} | |
payload['files'] = {} | |
payload['public'] = False | |
if len(sys.argv) == 1: | |
printUsage() | |
for i in range(1, len(sys.argv)): | |
if sys.argv[i] == '-p': | |
payload['public'] = False | |
elif sys.argv[i] == '-P': | |
payload['public'] = True | |
elif sys.argv[i] == '-h': | |
printUsage() | |
elif fileLoaded: | |
printUsage() | |
else: | |
fileLoaded = True | |
file = open(sys.argv[i], 'r') | |
fileContent = file.read() | |
payload['files'][sys.argv[i]] = { | |
"content":fileContent | |
} | |
#make a requests | |
response = requests.post(GITHUB_API, headers = headers, data = json.dumps(payload)) | |
# print the url or the error code returned | |
if response.status_code == 201: | |
print(json.loads(response.text)['html_url']) | |
else: | |
print(response.status_code) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment