Skip to content

Instantly share code, notes, and snippets.

@kaecy
Last active May 16, 2018 13:21
Show Gist options
  • Save kaecy/e2da66edf5b274f281ecb34b92bb79ed to your computer and use it in GitHub Desktop.
Save kaecy/e2da66edf5b274f281ecb34b92bb79ed to your computer and use it in GitHub Desktop.
# makegist.py - v2
# create a gist from a file on your computer
# you'll need a personal access key with the gists permissions to use it: https://github.com/settings/tokens
# this script makes use of windows' data streams and will only work on windows, feel free to modify it and use it for your own purpose
# init
# first off run
# script.py key <key>
# this will store your key
# then you can use
# script.py <text_file>
# from anywhere to create a gist
# thats all there is to it
import json
from sys import argv
from os.path import basename
from urllib2 import Request, urlopen
filename = basename(__file__)
keyfile = filename+":key"
def post_file(dat):
req = Request("https://api.github.com/gists", data=json.dumps(dat))
try:
req.add_header("Authorization", "token " + open(keyfile).read())
res = urlopen(req)
except:
return -1
try:
if res.getcode() == 201:
return json.loads(res.read())
except:
print "error - some unknown error"
def have_args(n):
if len(argv)-1 == n:
return True
return False
# program
if have_args(2):
if argv[1] == "/key":
print basename(__file__)
keyfile = open(keyfile, "w")
keyfile.write(argv[2])
print "saved"
if have_args(1):
gistFileData = {
"description": "",
"public": True,
"files": {}
}
entry = { "content": open(argv[1]).read() }
gistFileData["files"][basename(argv[1])] = entry
# if you want to see the req-json: print json.dumps(gistFileData)
json_obj = post_file(gistFileData)
if type(json_obj) == dict:
print json_obj["html_url"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment