Browse Source

коммуникация с сервером без очищения от лишних сеансов

main
commit
8a0865d710
  1. 109
      commun_wth_serv.py

109
commun_wth_serv.py

@ -0,0 +1,109 @@
def connect_to_sftp(hostname, username, password, remote_path):
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # Добавляет неизвестный хост в известные
ssh.connect(hostname, username=username, password=password)
sftp = ssh.open_sftp()
if len(remote_path)>3:
if remote_path[1]=='/': remote_path=remote_path[1:]
sftp.chdir(remote_path) # Переходим в указанную директорию. Обработка ошибок ниже.
return sftp
except paramiko.AuthenticationException:
print("Ошибка аутентификации. Проверьте имя пользователя и пароль.")
return None
except paramiko.SSHException as e:
print(f"Ошибка SSH: {e}")
return None
except FileNotFoundError:
print(f"Директория {remote_path} не найдена на сервере.")
return None
except IOError as e:
print(f"Ошибка ввода-вывода: {e}")
return None
def day_audios(remote_path):
sftp_client = connect_to_sftp("192.168.1.150", "monitor", "Audio4analy6!6", remote_path) #создание сессии (лишнее)
filess = sftp_client.listdir()
files = []#список файлов, подлежащих обработке (несжатые входящие)
for i in filess:
if 'in-' in i and not '_t' in i and not 'mp3' in i:
files.append(i)
# print(len(files))
txtfiles = [i[:-4] for i in os.listdir(f'/media/dev/TEXTFILES{remote_path}')] + os.listdir(
f'/media/dev/TEXTFILES/error_audiofiles/OutOfMemoryError{remote_path}') + os.listdir(
f'/media/dev/TEXTFILES/error_audiofiles/IndexError{remote_path}')
# print(set(files)-set(txtfiles))
files = list(set(files) - set(txtfiles))
#if len(files) > 0: print(files)
#print(len(files))
# print(files)
flush()
if files:
for i in files:
if 'in-' in i and not '_t' in i and not 'mp3' in i:
sftp_client.get(remote_path + '/' + i, f"{archive_path}{remote_path}{i}")#скачивание
print('1ok')
# print(f"{archive_path}{remote_path}{i}")
while not is_file_fully_loaded(os.path.join(f"{archive_path}{remote_path}{i}")):
time.sleep(1)
print('2ok')
try:
print('3ok')
call_text = recognition.to_txt_wdb(archive_path + remote_path + i)
print('4ok')
# Call.add_call(i,'-', call_text, '-', '-', '-', '-', '-')
except torch.OutOfMemoryError:
clean()
recognition.init_model()
try:
recognition.to_txt_wdb(archive_path + remote_path + i)
except torch.OutOfMemoryError:
os.makedirs(f'/media/dev/TEXTFILES/error_audiofiles/OutOfMemoryError{remote_path}{i}',
exist_ok=True)
shutil.copy(archive_path + remote_path + i,
f'/media/dev/TEXTFILES/error_audiofiles/OutOfMemoryError{remote_path}{i}')
except IndexError:
os.makedirs(f'/media/dev/TEXTFILES/error_audiofiles/IndexError{remote_path}{i}',
exist_ok=True)
shutil.copy((archive_path + remote_path + i),
f'/media/dev/TEXTFILES/error_audiofiles/IndexError{remote_path}{i}')
def today_audios():
while True:
try:
for i in weekd(datetime.datetime.today().weekday()): #заглушка, вместо выбора "несжатых" дней возвращается [-1,-2,-3,-4,-5,-6,-7], после чего каждая аудиозапись проверятеся на 'mp3' in file
date = str(datetime.datetime.today()).split(' ')[0].split('-')
hour = str(datetime.datetime.today()).split(' ')[1].split(':')[0]
# формирпование remote_path
if int(hour)<9: #до 9 утра звонки не поступают, обрабатывается вчерашний день
yesterday = datetime.datetime.now() - timedelta(days=-i+1)
year = yesterday.strftime("%Y")
month = yesterday.strftime("%m")
day = yesterday.strftime("%d")
if int(day) >= 10: remote_path = f"/{year}/{month}/{int(day)}/"
else: remote_path = f"/{year}/{month}/0{int(day)}/"
else:
yesterday = datetime.datetime.now() - timedelta(days=-i)
year = yesterday.strftime("%Y")
month = yesterday.strftime("%m")
day = yesterday.strftime("%d")
if int(day) >= 10: remote_path = f"/{year}/{month}/{str(int(day))}/"
else: remote_path = f"/{year}/{month}/0{int(day)}/"
os.makedirs('/media/dev/TEXTFILES'+remote_path, exist_ok=True)
os.makedirs(f'/media/dev/TEXTFILES/error_audiofiles/OutOfMemoryError{remote_path}', exist_ok=True)
os.makedirs(f'/media/dev/TEXTFILES/error_audiofiles/IndexError{remote_path}', exist_ok=True)
os.makedirs('/media/dev/TEXTFILES/audiofiles' + remote_path, exist_ok=True)
sftp_client = connect_to_sftp("192.168.1.150", "monitor", "Audio4analy6!6", remote_path) # создание сеанса
files = sftp_client.listdir() #получение списка файлов в сформированном выше remote_path
txtfiles = [i[:-4] for i in os.listdir(f'/media/dev/TEXTFILES{remote_path}')] + os.listdir(
f'/media/dev/TEXTFILES/error_audiofiles/OutOfMemoryError{remote_path}') + os.listdir(
f'/media/dev/TEXTFILES/error_audiofiles/IndexError{remote_path}')# формирование списка уже обработанных аудиозаписей
files = list(set(files)-set(txtfiles))# формирование списка необработанных аудиозаписей
day_audios(remote_path)#обработка всех аудио в указанном remote_path
time.sleep(900)
except AttributeError as e:
print('SSH error',e)
time.sleep(1800)
today_audios()
Loading…
Cancel
Save