Skip to content

Instantly share code, notes, and snippets.

@maehrm
Last active November 23, 2022 05:12
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 maehrm/15650bfa2118a7b6f633c088446a8942 to your computer and use it in GitHub Desktop.
Save maehrm/15650bfa2118a7b6f633c088446a8942 to your computer and use it in GitHub Desktop.
Gist upload Script.
from urllib import request
import os
import json
import argparse
class Gist:
token = os.environ["GIST_TOKEN"]
url = "https://api.github.com/gists"
def post(self, description, file_name, content, public="true"):
headers = {
"Accept": "application/vnd.github+json",
"Authorization": f"Bearer {self.token}",
}
params = {
"description": f"{description}",
"public": f"{public}",
"files": {f"{file_name}": {"content": f"{content}"}},
}
params_json = json.dumps(params)
data = params_json.encode("utf-8")
req = request.Request(self.url, data, headers)
with request.urlopen(req) as res:
result = res.read().decode("utf-8")
print(json.loads(result)['html_url'])
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-d", help="description")
parser.add_argument("file_name", help="file_name")
args = parser.parse_args()
with open(args.file_name) as f:
content = "".join(f.readlines())
gist = Gist()
gist.post(description=args.d, file_name=args.file_name, content=content)
@maehrm
Copy link
Author

maehrm commented Nov 23, 2022

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