Skip to content

Instantly share code, notes, and snippets.

@MineRobber9000
Last active November 14, 2019 15:59
Show Gist options
  • Save MineRobber9000/09c032f5a8bd9fa5baee4da63adcfd20 to your computer and use it in GitHub Desktop.
Save MineRobber9000/09c032f5a8bd9fa5baee4da63adcfd20 to your computer and use it in GitHub Desktop.
Command line tool to make gists
#!/usr/bin/env python3
import requests, argparse, configparser, os.path, json
config = configparser.ConfigParser()
config.read(os.path.expanduser("~/.config/github.ini"))
creds = config["credentials"]
def read_file(fn):
with open(fn) as f: return dict(content=f.read())
def give_filenames(filenames):
ret = dict()
for file in filenames:
if "=" in file:
tofile, fromfile = file.split("=")
ret[os.path.basename(tofile)]=read_file(fromfile)
else:
ret[os.path.basename(file)]=read_file(file)
return ret
parser = argparse.ArgumentParser(prog="mkgist",description="Creates a gist.")
parser.add_argument("-p","--public",help="Whether the gist will be public. Defaults to false.",action="store_true")
parser.add_argument("description",help="Description of the gist's contents.")
parser.add_argument("file",nargs="*",help="File names to be put in the gist.\nIf you want a file to be uploaded under a different name, use the format `uploadname=localname`.")
args = parser.parse_args()
data = dict()
data["description"]=args.description
data["files"]=give_filenames([os.path.expanduser(f) for f in args.file])
data["public"]=args.public
print(json.dumps(data,indent=2))
r = requests.post("https://api.github.com/gists",json=data,auth=(creds["user"],creds["pass"]))
r.raise_for_status()
print(r.json()["html_url"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment