Skip to content

Instantly share code, notes, and snippets.

@demacdolincoln
Last active February 11, 2018 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save demacdolincoln/e018f650022afc7f7c3e14a28962eb58 to your computer and use it in GitHub Desktop.
Save demacdolincoln/e018f650022afc7f7c3e14a28962eb58 to your computer and use it in GitHub Desktop.
create gist in command line
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from urllib.request import urlopen, Request
import json
from sys import argv
from os.path import expanduser as expand
data = {
"description" : "",
"public" : True,
"files" : {}
}
if "-h" in argv:
print("""create a gist in github
arguments:
-h ~> this help
-f ~> filename
-d ~> description (default = '')
-p ~> set to private gist
""")
elif "-f" not in argv:
print("selecione um arquivo usando -f ou veja as opções com -h")
else:
for i in range(1, len(argv)-1, 2):
if argv[i] == "-f":
filename = argv[i+1]
data["files"][filename] = {"content" : open(filename).read()}
elif argv[i] == "-d":
data["description"] = argv[i+1]
if "-p" in argv:
data["public"] = False
url = "https://api.github.com/gists"
token = open(expand("~/.config/github_api.token")).read()
req = Request(url)
req.add_header('Authorization', 'token {0}'.format(token))
response = urlopen(req, data=json.dumps(data).encode())
res = json.loads(response.read())
#print(json.dumps(res, indent=True))
#print("_"*70)
print(res["html_url"])
@demacdolincoln
Copy link
Author

how to generate tokens:

access https://github.com/settings/tokens > "generate new token"

save token as simple text file in ~/.config/github_api.token

recommendations

obs: gistit.py == this gist

  1. copy this file to /opt
  2. sudo chmod a+x gistit.py
  3. sudo ln -s (pwd)/gistit.py /bin // tested in fish shell

and have fun :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment