Select most recently modified file from directory

This commit is contained in:
Gnarwhal 2024-07-31 17:50:30 +00:00
parent 37a8c4bb49
commit 973ad06efa
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174

31
sshare
View file

@ -3,6 +3,7 @@
import getpass
import importlib.util
import os
import os.path
import pyclip
import time
import subprocess
@ -11,18 +12,38 @@ 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):