From 973ad06efa37999b652519a32fd76eb6cb3580f3 Mon Sep 17 00:00:00 2001 From: Gnarwhal Date: Wed, 31 Jul 2024 17:50:30 +0000 Subject: [PATCH] Select most recently modified file from directory --- sshare | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/sshare b/sshare index b3a3609..30f2c6b 100755 --- a/sshare +++ b/sshare @@ -3,6 +3,7 @@ import getpass import importlib.util import os +import os.path import pyclip import time import subprocess @@ -11,25 +12,45 @@ import sys def main(): config = Config() - id = time.time_ns() - file_name = f"{id}.png" + file = _latest("/home/gnarwhal/Pictures/Screenshots") + (_, extension) = os.path.splitext(file) - pyclip.copy(f"https://{config.ssh_target}/sshare/{file_name}") + id = time.time_ns() + target_file_name = f"{id}{extension}" + + print(f"=> Selected file '{file}'") + + target_url = f"https://{config.ssh_target}/sshare/{target_file_name}" + pyclip.copy(target_url) subprocess.run([ "scp", f"-P {config.ssh_port}", - f"/home/gnarwhal/Pictures/Screenshots/Screenshot from 2024-07-31 06-34-57.png", - f"{config.ssh_user}@{config.ssh_target}:{config.ssh_dir}/{file_name}", + file, + f"{config.ssh_user}@{config.ssh_target}:{config.ssh_dir}/{target_file_name}", ]) + + print(f"=> File uploaded to {target_url}") + + +def _latest(directory, key=os.path.getmtime): + files = map(lambda file: f"{directory}/{file}", os.listdir(directory)) + selection = next(files) + selection_key = key(selection) + for file in files: + new_key = key(file) + if new_key > selection_key: + selection = file + selection_key = key + return selection class Config: def __init__(self): - self.ssh_port = 2222 - self.ssh_user = getpass.getuser() + self.ssh_port = 2222 + self.ssh_user = getpass.getuser() self.ssh_target = "monodon.me" - self.ssh_dir = "~/sshare/" + self.ssh_dir = "~/sshare/" if __name__ == '__main__':