Created
July 13, 2016 11:47
-
-
Save fayesafe/bb86cae49828395210490d17e16c6a51 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 python3 | |
import argparse | |
import json | |
import requests | |
import sys | |
USER_NAME = '' | |
API_TOKEN = '' | |
def push_file(f, auth): | |
with open(f, 'r') as gist_file: | |
payload = {'public': True, | |
'files': { | |
f: { | |
'content': gist_file.read() | |
} | |
} | |
} | |
r = requests.post('https://api.github.com/gists', json=payload, | |
auth=auth) | |
response = json.loads(r.text) | |
print('HTML:\t{} '.format(response['html_url'])) | |
print(' RAW:\t{} '.format(response['files'][f]['raw_url'])) | |
def main(args): | |
auth = requests.auth.HTTPBasicAuth(USER_NAME, API_TOKEN) | |
def gist_data(gist): | |
gist_id = gist['id'] | |
gist_url = gist['html_url'] | |
gist_files = [] | |
for key in gist['files']: | |
cur_gist = {'raw_url': gist['files'][key]['raw_url'], | |
'filename': gist['files'][key]['filename']} | |
gist_files.append(cur_gist) | |
return {'gist_id': gist_id, 'gist_url': gist_url, | |
'gist_files': gist_files} | |
if (args.list_all): | |
all_gists = json.loads(requests.get('https://api.github.com/gists', | |
auth=auth).text) | |
gists = map(gist_data, all_gists) | |
for gist in gists: | |
print() | |
print('ID:\t', gist['gist_id']) | |
print('URL:\t', gist['gist_url']) | |
print('Files:') | |
for gist_file in gist['gist_files']: | |
print(' Filename:\t', gist_file['filename']) | |
print(' Raw Url:\t', gist_file['raw_url']) | |
print() | |
elif (args.delete): | |
gist_id = args.delete | |
del_gist = requests.delete('https://api.github.com/gists/{}' | |
.format(gist_id), auth=auth) | |
if (del_gist.status_code == 204): | |
print('Deleted {} successfully'.format(gist_id)) | |
else: | |
print('Could not delete Gist {}'.format(gist_id)) | |
elif (args.show): | |
gist_id = args.show | |
single_gist = requests.get('https://api.github.com/gists/{}' | |
.format(gist_id), auth=auth) | |
if (single_gist.status_code == 200): | |
gist_data = json.loads(single_gist.text) | |
for file_key in gist_data['files']: | |
name = gist_data['files'][file_key]['filename'] | |
raw = gist_data['files'][file_key]['raw_url'] | |
gist_content = requests.get(raw, auth=auth) | |
print(name) | |
print('=' * len(name)) | |
print(gist_content.text) | |
else: | |
print('Could not get Gist {}'.format(gist_id)) | |
elif (args.upload_file): | |
push_file(args.upload_file, auth) | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser() | |
group = parser.add_mutually_exclusive_group(required=True) | |
group.add_argument('-l', '--list_all', help='List all Gists', | |
action='store_true') | |
group.add_argument('-d', '--delete', help='Delete Gist', nargs='?') | |
group.add_argument('-s', '--show', help='Show Gist', nargs='?') | |
group.add_argument('upload_file', help='File to upload', nargs='?') | |
args = parser.parse_args() | |
main(args) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment