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()