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')
print(Result.Success)
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 = 'Uploads a local file to remote NoPaste website.')
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=0, 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.')
if len(sys.argv) == 1:
parser.print_help()
sys.exit(0)
args = parser.parse_args()
data = {}
data['token'] = os.environ.get('PASTE_D_TOKEN')
data['exposure'] = args.exposure
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:
print('{}: No data available in stdin.'.format(sys.argv[0]))
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()