#!/usr/bin/env python3

import requests
import sys, fileinput, argparse, copy
from urllib.parse import urlparse

url = 'https://cpy.pt/'

parser = argparse.ArgumentParser(description="CLI Utility to interact with pastething")

parser.add_argument('-d', '--delete', help="Delete paste with supplied delete token")
parser.add_argument('-b', '--burn', type=int, help="Maximum number of paste views before deletion")
parser.add_argument('-l', '--lexer', help="Lexer to use (pygments get_lexer_by_name)", default="auto")
parser.add_argument('-t', '--ttl', type=float, help="Time in hours before paste expires", default=1.0)
parser.add_argument('-r', '--raw', help="Weather to have pastebin return raw or html paste url", default='true')

parser.add_argument('FILE', help="Files to upload", nargs="+") #Re-used as paste ID or URL when deleting
args = vars(parser.parse_args())

if args['delete']:
	paste = urlparse(args['FILE'][0])
	if paste[0] != '':
		url = args['FILE'][0]
	else:
		url = url + paste[2]
	
	print("DELETE ", url)
	r = requests.delete(url, data={"token": args['delete']})
	print(r.text)
	if r.status_code != 200:
		exit(1)

else:
	parameters = copy.deepcopy(args)
	del parameters['FILE']

	with fileinput.input(files=args['FILE'], openhook=fileinput.hook_compressed) as f:
		parameters['paste'] = ''.join(f)
		r = requests.post(url, data=parameters)
		print(r.text)
		if r.status_code != 200:
			exit(1)

@@ -104,16 +104,45 @@
 urlparse
+%0Afrom os.path import splitext
 %0A%0Aurl = 
@@ -753,16 +753,117 @@
 ='true')
+%0Aparser.add_argument('-e', '--encoding', help=%22Define file encoding for given file (default: utf-8)%22)
 %0A%0Aparser
@@ -995,16 +995,428 @@
 rgs())%0A%0A
+#add encoding option to compressed hook%0Adef hook_compressed_encoded(encoding):%0A%09def hook_compressed(filename, mode):%0A%09%09ext = splitext(filename)%5B1%5D%0A%09%09if ext == '.gz':%0A%09%09%09import gzip%0A%09%09%09return gzip.open(filename, mode, encoding=encoding)%0A%09%09elif ext == '.bz2':%0A%09%09%09import bz2%0A%09%09%09return bz2.BZ2File(filename, mode, encoding=encoding)%0A%09%09else:%0A%09%09%09return open(filename, mode, encoding=encoding)%0A%09return hook_compressed%0A%0A
 if args%5B
@@ -1786,26 +1786,16 @@
 penhook=
-fileinput.
 hook_com
@@ -1801,16 +1801,42 @@
 mpressed
+_encoded(args%5B'encoding'%5D)
 ) as f:%0A

add support for custom file encoding and compression