Created
October 22, 2019 14:55
-
-
Save diath/fba83dcd1d99fa323b4928f84ecf79b1 to your computer and use it in GitHub Desktop.
Upload script for paste.diath.net
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
# alias pasted="PASTE_D_TOKEN=<TOKEN> python pasted.py --exposure private --notify --simple" | |
import argparse | |
import enum | |
import os | |
import sys | |
import requests | |
Link = "https://paste.diath.net" | |
Endpoint = "https://paste.diath.net/api" | |
Result = enum.IntEnum( | |
"Result", | |
"Success InvalidData InvalidSyntax InvalidExpiration InvalidExposure InvalidToken Throttled", | |
) | |
ResultMessage = {} | |
ResultMessage[Result.Success] = "Paste added successfully." | |
ResultMessage[Result.InvalidData] = "Invalid form data supplied." | |
ResultMessage[Result.InvalidSyntax] = "Submitted syntax is not supported." | |
ResultMessage[Result.InvalidExpiration] = "Invalid expiration range specified." | |
ResultMessage[Result.InvalidExposure] = "Invalid exposure value specified." | |
ResultMessage[Result.InvalidToken] = "Invalid API token provided." | |
ResultMessage[Result.Throttled] = "You have been temporarily throttled by the system." | |
def main(): | |
parser = argparse.ArgumentParser( | |
description="Upload a local file or stdin contents to {}.".format(Link) | |
) | |
parser.add_argument( | |
"--notify", action="store_true", default=False, help="Display notification." | |
) | |
parser.add_argument( | |
"--expiration", action="store", default=0, help="Set paste expiration." | |
) | |
parser.add_argument( | |
"--exposure", | |
default="private", | |
choices=("public", "private"), | |
help="Set paste exposure.", | |
) | |
parser.add_argument("--syntax", default="None", help="Set paste syntax.") | |
parser.add_argument( | |
"filepath", nargs="?", help="The path to the file that you want to upload." | |
) | |
group = parser.add_mutually_exclusive_group() | |
group.add_argument( | |
"--raw", | |
action="store_true", | |
default=False, | |
help="Return a link to a raw version of the entry.", | |
) | |
group.add_argument( | |
"--simple", | |
action="store_true", | |
default=False, | |
help="Return a link to a simple version of the entry.", | |
) | |
args = parser.parse_args() | |
if not os.environ.get("PASTE_D_TOKEN"): | |
print( | |
"This script requires an API token in the PASTE_D_TOKEN environment variable." | |
) | |
sys.exit(0) | |
data = {} | |
data["token"] = os.environ.get("PASTE_D_TOKEN") | |
data["exposure"] = 0 if args.exposure == "public" else 1 | |
data["expiration"] = args.expiration | |
data["syntax"] = args.syntax | |
if args.filepath: | |
try: | |
with open(args.filepath) as handle: | |
data["content"] = handle.read() | |
except: | |
print("{}: File doesn't exist.".format(sys.argv[0])) | |
sys.exit(0) | |
else: | |
if not sys.stdin.isatty(): | |
data["content"] = sys.stdin.read() | |
else: | |
parser.print_help() | |
sys.exit(0) | |
request = requests.post(Endpoint, data=data) | |
if request.status_code == requests.codes.ok: | |
data = request.text.split(":") | |
assert len(data) >= 2 | |
code = int(data[0]) | |
hash = data[1] | |
if code == Result.Success: | |
def get_link_suffix(): | |
if args.raw: | |
return "/raw" | |
elif args.simple: | |
return "/simple" | |
return "" | |
link = "{}/{}{}".format(Link, hash, get_link_suffix()) | |
print("{}".format(ResultMessage.get(Result.Success))) | |
print("Hash: {}".format(hash)) | |
print("Link: {}".format(link)) | |
os.system('echo "{}" | xclip -selection c'.format(link)) | |
os.system('echo "{}" | xclip'.format(link)) | |
if args.notify: | |
os.system( | |
'notify-send -i emblem-default "Paste" "Paste has been added succesfully."' | |
) | |
else: | |
print("Invalid return code returned by the endpoint.") | |
print("#{}: {}".format(code, ResultMessage.get(code))) | |
if args.notify: | |
os.system( | |
'notify-send -i emblem-unreadable "Paste" "Invalid return code returned by the endpoint.\n{}"'.format( | |
ResultMessage.get(code) | |
) | |
) | |
else: | |
print("Invalid status code returned by the endpoint.") | |
print("#{}".format(request.status_code)) | |
if args.notify: | |
os.system( | |
'notify-send -i emblem-unreadable "Paste" "Invalid status code returned by the endpoint."' | |
) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment