Use delugeClient-kevin package instead of exec in os thread

This commit is contained in:
2022-11-28 00:48:24 +01:00
parent 8af5301d34
commit 6ff33dcef1

View File

@@ -1,8 +1,16 @@
#!/usr/bin/python3 #!/usr/bin/python3
import os, sys import os, sys
import shutil
from subprocess import check_output, Popen, PIPE from subprocess import check_output, Popen, PIPE
from datetime import timedelta
from time import time
try:
from delugeClient import Deluge
except Exception:
print('Download delugeClient package using: pip3 install delugeClient-kevin')
sys.exit(1)
# Local files
from logger import logger from logger import logger
from utils import getConfig, readAvgSpeedFromDisk, writeAvgSpeedToDisk from utils import getConfig, readAvgSpeedFromDisk, writeAvgSpeedToDisk
@@ -78,15 +86,14 @@ def getFiles(path, host=None, user=None):
cmd = "ls '{}'".format(path) cmd = "ls '{}'".format(path)
contents = check_output(cmd, shell=True) contents = check_output(cmd, shell=True)
if contents != None:
if (contents):
contents = contents.decode('utf-8').split('\n') contents = contents.decode('utf-8').split('\n')
contents = list(filter(lambda x: len(x) > 0, contents)) contents = list(filter(lambda x: len(x) > 0, contents))
return contents return contents
def filesNotShared(local, remote): def filesNotShared(remote, local):
c = set(local) - set(remote) c = set(remote) - set(local)
if c == set(): if c == set():
return False return False
@@ -96,15 +103,14 @@ def transferFiles(files, localPath, remotePath, host=None, user=None):
transferedFiles = [] transferedFiles = []
for file in files: for file in files:
if file in getFiles(remotePath, host, user): if file in getFiles(localPath):
logger.info('File already exists at remote path. Skipping.') logger.info('File already exists at remote path. Skipping.')
continue continue
logger.info('Moving file: {}'.format(file), es={'filename': file})
file = os.path.join(localPath, file) file = os.path.join(localPath, file)
if (host and user): if host and user:
cmd = "rsync -rz '{}' {}@{}:{}".format(file, user, host, remotePath) cmd = "rsync -rz {}@{}:'{}' '{}'".format(user, host, spaceEscapedFile, localPath)
else: else:
cmd = "rsync -rz '{}' {}".format(file, remotePath) cmd = "rsync -rz '{}' {}".format(file, remotePath)
@@ -114,63 +120,56 @@ def transferFiles(files, localPath, remotePath, host=None, user=None):
if stderr: if stderr:
logger.error('Rsync error: {}'.format(stderr)) logger.error('Rsync error: {}'.format(stderr))
logger.debug('Rsync output: {}'.format(stdout))
transferedFiles.append(file) transferedFiles.append(file)
return transferedFiles return transferedFiles
def removeFromDeluge(execScript, files): def removeFromDeluge(files):
execPython = '/usr/bin/python2' deluge = Deluge()
for file in files: for file in files:
file = file.split('/')[-1] file = file.split('/')[-1]
logger.info('Removing {} from deluge'.format(file), es={"filename": file}) logger.info('Removing {} from deluge'.format(file), es={"filename": file})
cmd = "{} {} rm '{}'".format(execPython, execScript, file) try:
response = deluge.removeByName(file, True)
if response == None:
raise Exception('No torrent with that name found')
delugeProcess = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
stdout, stderr = delugeProcess.communicate()
if stderr:
logger.error('Deluge error: {}'.format(stderr))
logger.debug('Deluge output: {}'.format(stdout))
logger.info('Successfully removed: {}'.format(file), es={'filename': file}) logger.info('Successfully removed: {}'.format(file), es={'filename': file})
except Exception as err:
logger.error('Deluge error: {}'.format(err), es={'filename': file})
def main(): def main():
config = getConfig() config = getConfig()
host = config['SSH']['host'] host = config['SSH']['host']
user = config['SSH']['user'] user = config['SSH']['user']
remotePath = config['FILES']['remote'] remotePath = config['FILES']['remote']
localPath = config['FILES']['local'] localPath = config['FILES']['local']
delugeScript = config['DELUGE']['script']
remoteFiles = getFiles(remotePath, host, user) remoteFiles = getFiles(remotePath, host, user)
if len(remoteFiles) > 0: if len(remoteFiles) > 0:
logger.info('Remote files found: {}'.format(remoteFiles), es={'files': remoteFiles}) logger.info('Remote files found: {}'.format(remoteFiles), es={'files': remoteFiles})
else: else:
logger.info('No remote files found') logger.info('No remote files found')
# print('Remote found: {}'.format(remoteFiles))
localFiles = getFiles(localPath) localFiles = getFiles(localPath)
# print('Local files: {}'.format(localFiles))
if len(localFiles) > 0: if len(localFiles) > 0:
logger.info('Local files found: {}'.format(localFiles), es={'files': localFiles}) logger.info('Local files found: {}'.format(localFiles), es={'files': localFiles})
else: else:
logger.info('No local files found') logger.info('No local files found')
newFiles = filesNotShared(localFiles, remoteFiles) newFiles = filesNotShared(remoteFiles, localFiles)
if (newFiles): if newFiles:
logger.info('New files: {}'.format(newFiles), es={'files': newFiles}) logger.info('New files: {}'.format(newFiles), es={'files': newFiles})
exisitingFiles = list(set(remoteFiles).intersection(localFiles)) exisitingFiles = list(set(remoteFiles).intersection(localFiles))
logger.info('Existing files: {}'.format(exisitingFiles), es={'files': exisitingFiles}) logger.info('Existing files: {}'.format(exisitingFiles), es={'files': exisitingFiles})
transferedFiles = transferFiles(newFiles, localPath, remotePath, host, user) transferedFiles = transferFiles(newFiles, localPath, remotePath, host, user)
removeFromDeluge(delugeScript, transferedFiles) removeFromDeluge(transferedFiles)
else: else:
# print('No new files found to travel on the great transatlantic express')
logger.info('No new files found to travel on the great transatlantic express') logger.info('No new files found to travel on the great transatlantic express')
if __name__ == '__main__': if __name__ == '__main__':