From 35633b93ca2e8209264a977fc090ce74f51e315e Mon Sep 17 00:00:00 2001 From: hyvenet Date: Fri, 22 Aug 2025 19:59:26 +0100 Subject: [PATCH] Integrated new upload sys with main file --- main.py | 16 ++-------------- uploadresults.py | 50 ++++++++++++++++++++++++------------------------ 2 files changed, 27 insertions(+), 39 deletions(-) diff --git a/main.py b/main.py index caff092..967babf 100644 --- a/main.py +++ b/main.py @@ -2,20 +2,13 @@ import openDrive import time import os from imdb import Cinemagoer -import paramiko +import uploadresults from dotenv import load_dotenv load_dotenv() DVD_DEVICE = "/dev/sr0" -uploadPath = os.getenv('UPLOAD_PATH') - -sftpHost = os.getenv('SFTP_HOST') -sftpPort = int(os.getenv('SFTP_PORT')) -sftpUsername = os.getenv('SFTP_USERNAME') -sftpPassword = os.getenv('SFTP_PASSWORD') - def ripDvd(driveName): print("- DVD has been detected") print("- Searching IMDB for " + driveName) @@ -30,13 +23,8 @@ def ripDvd(driveName): print("- Will now procede to ripping DVD") movieName = driveName os.system('HandBrakeCLI -i /dev/sr0 -o output/"' + movieName + '.mp4" --preset-import-file preset.json') - sftpTransport = paramiko.Transport((sftpHost, sftpPort)) - sftpTransport.connect(username = sftpUsername, password = sftpPassword) - sftp = paramiko.SFTPClient.from_transport(sftpTransport) print("- Movie has been ripped, will now procede to upload file") - sftp.put('output/"' + movieName + '.mp4"', uploadPath + movieName + '.mp4') - sftp.close() - sftpTransport.close() + print("- Movie has been uploaded") def waitForDvd(): diff --git a/uploadresults.py b/uploadresults.py index 2718dc4..6d84a7a 100644 --- a/uploadresults.py +++ b/uploadresults.py @@ -2,35 +2,35 @@ import paramiko import os from dotenv import load_dotenv -load_dotenv() +def upload(): + load_dotenv() + hostname = os.getenv('SFTP_HOST') + port = int(os.getenv('SFTP_PORT')) + username = os.getenv('SFTP_USERNAME') + password = os.getenv('SFTP_PASSWORD') -hostname = os.getenv('SFTP_HOST') -port = int(os.getenv('SFTP_PORT')) -username = os.getenv('SFTP_USERNAME') -password = os.getenv('SFTP_PASSWORD') + remote_directory = os.getenv('UPLOAD_PATH') + local_directory = 'output/' # Where to save the downloaded files -remote_directory = os.getenv('UPLOAD_PATH') -local_directory = 'output/' # Where to save the downloaded files + # Create an SSH client + ssh_client = paramiko.SSHClient() + ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # Automatically add the server's host key, {Link: according to Medium https://medium.com/nerd-for-tech/paramiko-how-to-transfer-files-with-remote-system-sftp-servers-using-python-52d3e51d2cfa} + ssh_client.connect(hostname, port, username, password) -# Create an SSH client -ssh_client = paramiko.SSHClient() -ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # Automatically add the server's host key, {Link: according to Medium https://medium.com/nerd-for-tech/paramiko-how-to-transfer-files-with-remote-system-sftp-servers-using-python-52d3e51d2cfa} -ssh_client.connect(hostname, port, username, password) + # Open an SFTP session + sftp = ssh_client.open_sftp() -# Open an SFTP session -sftp = ssh_client.open_sftp() + # List the files in the remote directory + for filename in os.listdir(local_directory): + remote_filepath = os.path.join(remote_directory, filename) + local_filepath = os.path.join(local_directory, filename) -# List the files in the remote directory -for filename in os.listdir(local_directory): - remote_filepath = os.path.join(remote_directory, filename) - local_filepath = os.path.join(local_directory, filename) + # Download each file + sftp.put(local_filepath, remote_filepath) + print(f"Uploaded: {local_filepath} to {remote_filepath}") - # Download each file - sftp.put(local_filepath, remote_filepath) - print(f"Uploaded: {local_filepath} to {remote_filepath}") - -# Close the SFTP session and SSH connection -sftp.close() -ssh_client.close() -print("All files downloaded and connections closed.") + # Close the SFTP session and SSH connection + sftp.close() + ssh_client.close() + print("All files downloaded and connections closed.")