from autoLoader.config import FILESAPTH from autoLoader.loader import ConnectorSFTP import datetime, os from autoLoader.database import * local_path = os.path.join(os.getcwd(), FILESAPTH) class Loader(): def __init__(self): self.call_types = ['in'] pass def filter_call(self, filename: str): if filename.split("-")[0] in self.call_types: return True return False def load(self): date_now = datetime.datetime.now()# - datetime.timedelta(days=1) remote_path = f"/{date_now.strftime('%Y/%m/%d')}" connector = ConnectorSFTP() connector.connect(remote_path=remote_path) sftp_client = connector.sftp ssh_client = connector.ssh if sftp_client is None or ssh_client is None: print("Не удалось подключиться к SFTP. Завершение работы.") exit(1) db = get_db() try: listdir = sftp_client.listdir() os.makedirs(local_path, exist_ok = True) diskdir = os.listdir(local_path) for file in listdir: if self.filter_call(filename=file): remote_file = f"{file}".lstrip('/') filepath = os.path.join(local_path, file) try: sftp_client.get(remote_file, filepath) # Скачиваем файл audio = Audio() audio.index_date = datetime.datetime.now() audio.filename = file db.session.add(audio) db.session.commit() print(f"Скачан файл: {remote_file}") except Exception as e: print(f"Ошибка при скачивании файла {remote_file}: {e}") finally: # Закрываем соединения sftp_client.close() ssh_client.close()