From 6d0fa461ddcfd46fd365475427227321cb1123df Mon Sep 17 00:00:00 2001 From: kageru <8497max@gmail.com> Date: Wed, 18 Oct 2017 14:38:36 +0200 Subject: [PATCH] screenshots and files via curl should work now --- pyshare.py | 51 +++++++++++++++++++++++++++++++++++++-------------- users.py | 2 ++ 2 files changed, 39 insertions(+), 14 deletions(-) diff --git a/pyshare.py b/pyshare.py index d9c5f82..3342c4e 100644 --- a/pyshare.py +++ b/pyshare.py @@ -12,10 +12,10 @@ import config character_pool = ascii_letters + digits -def find_filename(prefix, length, ext, conn): - def generate_filename(prefix, length, ext): - return prefix + ''.join(choices(character_pool, k=length)) + '.' + ext +def generate_filename(prefix, length, ext): + return prefix + ''.join(choices(character_pool, k=length)) + '.' + ext +def find_filename(prefix, length, ext, conn): filename = generate_filename(prefix, length, ext) i = 0 while conn.exists(filename): @@ -26,28 +26,37 @@ def find_filename(prefix, length, ext, conn): find_filename(prefix, length + 1, ext, conn) -def upload_local_file(path: str, conn: Connection) -> Exception: # does this even return an exception? probably not. does it matter? definitely not +def upload_local_file(path: str, + conn: Connection) -> Exception: # does this even return an exception? probably not. does it matter? definitely not raise NotImplementedError('soon(tm)') -def upload_screenshot(filename: str, conn: Connection) -> None: +def take_screenshot(filename: str) -> None: call(["escrotum", "{}".format(filename), "-s"]) - conn.put(filename) -def prepare_upload(mode='screenshot', ext=None) -> tuple: - if mode == 'screenshot' and ext == None: +def ftp_upload(mode='screenshot', ext=None) -> tuple: + if mode == 'screenshot' and ext is None: ext = 'png' with Connection(config.sftp_address, username=config.username, password=config.password) as conn: with conn.cd(config.remote_directory): filename = find_filename(config.prefix, config.length, ext, conn) + '.{}'.format(ext) - fullpath = os.path.join(config.local_directory, filename) + take_screenshot(filename) + + conn.put(filename) return fullpath, filename +def curl_upload(filename): + if config.custom_curl_command is not None: + return call(config.custom_curl_command) + else: + return call(f'curl -k -F"file=@{filename}" -F"name={config.username}" -F"passwd={config.password}" {config.curl_target}') + + def notify_user(url): print(url) # copy link to clipboard @@ -66,13 +75,27 @@ def notify_user(url): if __name__ == '__main__': if len(sys.argv) != 1: - mode = sys.argv[1] - file = sys.argv[2] - ext = file.splitr('.', 1)[1] + mode = 'file' + # mode = sys.argv[1] + file = sys.argv[1] else: mode = 'screenshot' ext = 'png' - fullpath, filename = prepare_upload(mode, ext) - + if config.uploader in ['ftp', 'sftp']: + if mode != 'screenshot' and '.' in file: + ext = '.' + file.rsplit('.', 1)[1] + # TODO: mode file for FTP + fullpath, filename = ftp_upload(mode, ext) + elif config.uploader == 'curl': + if mode=='screenshot': + filename = generate_filename(length=config.length, ext='.png') + fullpath = os.path.join(config.local_directory, filename) + take_screenshot(fullpath) + else: + fullpath = file + curl_upload(fullpath) + else: + print('Unknown mode') + sys.exit(-1) url = config.url_template.format(filename) notify_user(url) diff --git a/users.py b/users.py index 6566a3e..33b8dbf 100644 --- a/users.py +++ b/users.py @@ -3,6 +3,8 @@ # The password hash is generated by # hashlib.sha3_256(bytes((password + salt).encode('utf8'))).hexdigest() +# If you actually want to use this for something serious, you should probably use argon2 or something + users = { 'user1': ['c7d9c9621e417ea09141edbac126cd1f3ab1b2b94b2ad3b155a1e26a88b216c0', 'user1salt'], }