Created
February 4, 2020 19:45
-
-
Save driazati/43bb1ba30ee79e4ff017e95de82d3c88 to your computer and use it in GitHub Desktop.
auto-created gist
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
import fileinput | |
import configparser | |
import os | |
import requests | |
class col: | |
HEADER = "\033[95m" | |
BLUE = "\033[94m" | |
GREEN = "\033[92m" | |
YELLOW = "\033[93m" | |
RED = "\033[91m" | |
RESET = "\033[0m" | |
BOLD = "\033[1m" | |
UNDERLINE = "\033[4m" | |
config = { | |
"quiet": False | |
} | |
ghstackrc = configparser.ConfigParser() | |
ghstackrc.read(os.path.expanduser('~/.ghstackrc')) | |
config['github_oauth'] = ghstackrc['ghstack']['github_oauth'] | |
username = ghstackrc['ghstack']['github_username'] | |
def log(*args, **kwargs): | |
if not config['quiet']: | |
print(*args, **kwargs) | |
def color(color, text): | |
return col.BOLD + color + str(text) + col.RESET | |
def github(method, path, payload=None, note=""): | |
if payload is None: | |
payload = {} | |
headers = { | |
"Content-Type": "application/json", | |
"Host": "api.github.com", | |
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8", | |
"Accept-Encoding": "gzip, deflate, br", | |
"Accept-Language": "en-US,en;q=0.9", | |
} | |
if config['github_oauth'] is not None: | |
headers["Authorization"] = "token " + config['github_oauth'] | |
url = "https://api.github.com/" + path | |
log(color(col.GREEN, "{} {}".format(method, url)), "{}{}{}".format(col.BLUE, note, col.RESET)) | |
r = getattr(requests, method)( | |
url, | |
json=payload, | |
headers=headers, | |
) | |
return r.json() | |
stdin = [line for line in fileinput.input()] | |
stdin = ''.join(stdin) | |
payload = { | |
"files": { | |
"main.py": { | |
"content": stdin | |
} | |
}, | |
"description": "auto-created gist", | |
"public": True | |
} | |
url = 'users/{}/gists'.format(username) | |
url = 'gists' | |
x = github('post', url, payload=payload, note='(uploading content from stdin)') | |
if 'html_url' not in x: | |
raise RuntimeError("Error creating gist") | |
print(x['html_url']) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment