Select most recently modified file from directory
This commit is contained in:
parent
37a8c4bb49
commit
973ad06efa
1 changed files with 29 additions and 8 deletions
37
sshare
37
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__':
|
||||
|
|
Loading…
Reference in a new issue