Last active
February 11, 2018 04:51
-
-
Save demacdolincoln/e018f650022afc7f7c3e14a28962eb58 to your computer and use it in GitHub Desktop.
create gist in command line
This file contains hidden or 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/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"]) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
/opt
sudo chmod a+x gistit.py
sudo ln -s (pwd)/gistit.py /bin
// tested in fish shelland have fun :-)