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

37
sshare
View file

@ -3,6 +3,7 @@
import getpass import getpass
import importlib.util import importlib.util
import os import os
import os.path
import pyclip import pyclip
import time import time
import subprocess import subprocess
@ -11,25 +12,45 @@ import sys
def main(): def main():
config = Config() config = Config()
id = time.time_ns() file = _latest("/home/gnarwhal/Pictures/Screenshots")
file_name = f"{id}.png" (_, 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([ subprocess.run([
"scp", "scp",
f"-P {config.ssh_port}", f"-P {config.ssh_port}",
f"/home/gnarwhal/Pictures/Screenshots/Screenshot from 2024-07-31 06-34-57.png", file,
f"{config.ssh_user}@{config.ssh_target}:{config.ssh_dir}/{file_name}", 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: class Config:
def __init__(self): def __init__(self):
self.ssh_port = 2222 self.ssh_port = 2222
self.ssh_user = getpass.getuser() self.ssh_user = getpass.getuser()
self.ssh_target = "monodon.me" self.ssh_target = "monodon.me"
self.ssh_dir = "~/sshare/" self.ssh_dir = "~/sshare/"
if __name__ == '__main__': if __name__ == '__main__':