diff --git a/bot.py b/bot.py index 68299ab..65f0dc8 100644 --- a/bot.py +++ b/bot.py @@ -1,3 +1,4 @@ +import gc import logging from aiogram import Bot, Dispatcher, types, executor import os @@ -25,39 +26,23 @@ # хэндлер на команду /start @dp.message_handler(commands='start') async def cmd_start(message: types.Message): - await message.answer('Hello!') + await message.answer('Hello!') -unique_id = '' -@dp.message_handler(content_types=['audio', 'photo']) -async def show_spectrogram(message: types.Message): - audio_id = message.audio.file_id - global unique_id - unique_id = message.audio.file_unique_id - file_info = await bot.get_file(audio_id) - path_to_file = f'D:\\PyCharm\\PyCharm 2022.3\\pythonProject\\{message.audio.file_unique_id}.mp3' - try: - await bot.download_file(file_info.file_path, - destination=f'D:\\PyCharm\\PyCharm 2022.3\\pythonProject\\{message.audio.file_unique_id}.mp3') - await message.answer("Your audio is successfully downloaded") - except: - await message.answer("Error downloading") - +def create_spectrogram(path_to_file: str, unique_id: str): input_file = f'{path_to_file}' output_file = f'{path_to_file}.wav' sound = AudioSegment.from_mp3(input_file) sound.export(output_file, format='wav') - input_file2 = output_file output_file_mono = f'{output_file}_mono.wav' - stereo_audio = AudioSegment.from_file(input_file2, format='wav') + stereo_audio = AudioSegment.from_file(output_file, format='wav') mono_audios = stereo_audio.split_to_mono() mono_left = mono_audios[0].export(output_file_mono, format='wav') - input_file_spectrogram = output_file_mono - samplerate, data = wavfile.read(input_file_spectrogram) + samplerate, data = wavfile.read(output_file_mono) f, t, Sxx = spectrogram(data, samplerate) plt.pcolormesh(1000 * t, f / 1000, 10 * np.log10(Sxx / Sxx.max()), vmin=-120, vmax=0, cmap='inferno') @@ -66,10 +51,31 @@ async def show_spectrogram(message: types.Message): plt.colorbar() img = f'{unique_id}.png' plt.savefig(img) + plt.clf() + + +@dp.message_handler(content_types=['audio', 'photo']) +async def show_spectrogram(message: types.Message): + unique_id = '' + audio_id = message.audio.file_id + unique_id = message.audio.file_unique_id + file_info = await bot.get_file(audio_id) + path_to_file = f'D:\\PyCharm\\PyCharm 2022.3\\pythonProject\\{message.audio.file_unique_id}.mp3' + + try: + await bot.download_file(file_info.file_path, + destination=f'D:\\PyCharm\\PyCharm 2022.3\\pythonProject\\{message.audio.file_unique_id}.mp3') + await message.answer("Your audio is successfully downloaded") + except: + await message.answer("Error downloading") + create_spectrogram(path_to_file, unique_id) + try: - photo = open(f'{unique_id}.png', 'rb') - await message.answer_photo(photo, caption='Your spectrogram') + with open(f"{unique_id}.png", 'rb') as photo: + await message.answer_photo(photo, caption='Your spectrogram') + photo.close() + del photo except: await message.answer("Error showing")