import paramiko
import os
from deezer_downloader.configuration import config

def call_scan(directory_path):
    if not config['nextcloud_ssh_scan_files']['host']:
        return
    host = config['nextcloud_ssh_scan_files']['host']
    port = config['nextcloud_ssh_scan_files']['port']
    username = config['nextcloud_ssh_scan_files']['username']
    password = config['nextcloud_ssh_scan_files']['password']

    directory_path = os.path.dirname(directory_path)
    directory_path = directory_path.replace(config['download_dirs']['base'] + "\\", "")
    print("Path Nexcloud Scan " + directory_path)
    # Commande à exécuter
    cmd = 'sudo /home/nextclouddeezer/scan_deezerDl_folder.sh "{dir}"'.format(dir=directory_path)

    # Création de la connexion SSH
    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())  # accepter clés inconnues
    ssh.connect(hostname=host, port=port, username=username, password=password)

    # Exécuter la commande
    stdin, stdout, stderr = ssh.exec_command(cmd)

    # Afficher les résultats
    print("STDOUT:")
    print(stdout.read().decode())

    print("STDERR:")
    print(stderr.read().decode())

    # Fermer la connexion
    ssh.close()