Last active
April 28, 2024 20:55
-
-
Save 0xHumban/bcd46eaf8ad1f0cb69d17081d814a430 to your computer and use it in GitHub Desktop.
Gist auto-generated by a python script
This file contains 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
""" | |
AUTHOR: 0xHumban | |
LICENSE: MIT | |
VERSION: 1.0.1 | |
CONTACT: [email protected] | |
GITHUB: https://github.com/0xHumban | |
""" | |
#!/usr/bin/env python | |
""" | |
The goal is to create a gist for a file given on your github account | |
To use the script, you have to create a token on your github settings | |
On linux: export GIST_TOKEN=your_token | |
and OPTIONAL discord webhook url: | |
export DISCORD_WEBHOOK_URL="webhook_url" | |
USE: python gist-creator.py your_file.txt | |
OPTIONS: | |
To set if the gist will be in public (default false) | |
(true: 1, no: 0) | |
--public <1 or 0> | |
To set if you want to publish the link of the gist on your discord with webhook (default false) | |
(true: 1, no: 0) | |
-d <1 or 0> | |
By default: the gist is private | |
""" | |
# to share with people | |
GITHUB_USERNAME="0xHumban" | |
# ========================================================================= | |
import requests | |
import json | |
import os | |
import argparse | |
# ------------------------------ | |
# Colors for the terminal | |
COLORS = { | |
'RED': '\033[91m', | |
'GREEN': '\033[92m', | |
'WARNING': '\033[93m' | |
} | |
END_COLOR = '\033[0m' | |
# ------------------------------ | |
GISTS_PUBLIC_URL="https://gist.github.com/" + GITHUB_USERNAME + "/" | |
GITHUB_API_URL="https://api.github.com" | |
GISTS_URL=GITHUB_API_URL + "/gists" | |
print(COLORS['WARNING'] + "Current API url: " + GISTS_URL + END_COLOR) | |
# get the token | |
USER_TOKEN = os.getenv('GIST_TOKEN') | |
# get the discord webhook url | |
WEBHOOK_URL = os.getenv('DISCORD_WEBHOOK_URL') | |
# check if token is ok | |
if not USER_TOKEN: | |
print(COLORS['RED'] + "\nERROR: GIST_TOKEN ENVIRONNEMENT VAR NOT FOUND!\n" + END_COLOR) | |
# setup the headers | |
headers = { | |
'Authorization': 'Bearer %s'%USER_TOKEN, | |
'Content-Type': 'application/json' | |
} | |
# store the privacy status of the gist | |
public_status = 0 | |
# store if user want to publish on discord | |
discord_publish = 0 | |
# get user arguments | |
parser = argparse.ArgumentParser(description='This script create a gist for a file on your github account') | |
parser.add_argument('filename', type=str, help="The filename of the file to send for the gist") | |
parser.add_argument('--public', type=int, help="If the gist will be public or not, default no, (yes: 1, no: 0)") | |
parser.add_argument('-d', type=int, help="If the gist will be publish on discord with webhook, default no, (yes: 1, no: 0)") | |
args = parser.parse_args() | |
if args.public: | |
public_status = args.public | |
if args.d: | |
discord_publish = args.d | |
if public_status == 1: | |
public_status = True | |
else: | |
public_status = False | |
if not args.filename : | |
print(COLORS['RED'] + "\nERROR: NO FILENAME PROVIDE\nPlease provide a filename to run the script\n" + END_COLOR) | |
exit(1) | |
# get the content in the file | |
with open(args.filename, 'r') as file: | |
file_content = file.read() | |
# data for the Gist | |
gist_data = { | |
"description": "Gist auto-generated by a python script", | |
"public": public_status, | |
"files": { | |
args.filename: { | |
"content": file_content | |
} | |
} | |
} | |
res = requests.post(GISTS_URL, headers=headers, data=json.dumps(gist_data)) | |
# debug response | |
# print(res.text) | |
# print(res.status_code) | |
j=json.loads(res.text) | |
# Check the response status code | |
if res.status_code == 201: | |
gist_id = j["id"] | |
print(COLORS['GREEN'] + "\nYour gist for the file '" + args.filename +"' has been created at the url: \n" + GISTS_PUBLIC_URL + gist_id + END_COLOR) | |
else: | |
print(COLORS['RED'] + "\nERROR: during the Gist creation: "+END_COLOR) | |
print(res.json()['message']) | |
# send message on discord with webhook | |
if WEBHOOK_URL and res.status_code == 201: | |
gist_id = j["id"] | |
# print(j["files"]) | |
header = { | |
'Content-Type': 'application/json' | |
} | |
message = ("\n## New gist created by [" + GITHUB_USERNAME + "](https://github.com/" + GITHUB_USERNAME + | |
"):\n**Link:** " + GISTS_PUBLIC_URL + gist_id + | |
"\nFile: __" + args.filename + "__") | |
message_data = { | |
'content': message | |
} | |
res = requests.post(WEBHOOK_URL, headers=header, data=json.dumps(message_data)) | |
# check response status | |
if res.status_code == 204: | |
print(COLORS['GREEN'] + "\nThe gist has been share on discord!\n" + END_COLOR) | |
else: | |
print(COLORS['RED'] + "\nERROR: during the Gist creation: "+END_COLOR) | |
print(res) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
2nd revision:
Implement discord webhook
Choose if you want to share the gist link on a discord channel