updated ynadex podcasts

This commit is contained in:
Alexander Karpov 2024-07-20 02:41:32 +03:00
parent 0dc45b8935
commit 170f7e12e7
2 changed files with 111 additions and 80 deletions

View File

@ -1,12 +1,10 @@
import asyncio import asyncio
import os import os
import tempfile import tempfile
from io import BytesIO
from time import sleep from aiogram import Bot, Dispatcher
from aiogram.client.telegram import TelegramAPIServer
from aiogram import Bot from aiogram.types import InputFile
from aiogram.bot.api import TelegramAPIServer
from mutagen.easyid3 import EasyID3 from mutagen.easyid3 import EasyID3
from mutagen.mp3 import MP3 from mutagen.mp3 import MP3
from mutagen.id3 import APIC, ID3, TORY from mutagen.id3 import APIC, ID3, TORY
@ -23,34 +21,62 @@ TELEGRAM_SERVER = os.getenv("TELEGRAM_SERVER", default=None)
if TELEGRAM_SERVER: if TELEGRAM_SERVER:
local_server = TelegramAPIServer.from_base(TELEGRAM_SERVER) local_server = TelegramAPIServer.from_base(TELEGRAM_SERVER)
bot = Bot(TOKEN, server=local_server) bot = Bot(TOKEN, base_url=local_server.base)
else: else:
bot = Bot(TOKEN) bot = Bot(TOKEN)
dp = Dispatcher()
client = Client(YANDEX_TOKEN).init() client = Client(YANDEX_TOKEN).init()
latest_podcast = None latest_podcast = None
latest_sent = True latest_sent = True
podcasts_listened = [] podcasts_listened = []
async def main():
global latest_podcast, latest_sent
while True: while True:
try: try:
queues = client.queues_list()
last_queue = client.queue(queues[0].id)
queues = client.queues_list() # Ques are now not working
if not queues:
print(
"No active queues found. Waiting for 60 seconds before checking again."
)
await asyncio.sleep(5)
continue
last_queue = client.queue(queues[0].id)
last_track_id = last_queue.get_current_track() last_track_id = last_queue.get_current_track()
if last_track_id is None:
print(
"No track currently playing. Waiting for 60 seconds before checking again."
)
await asyncio.sleep(60)
continue
last_track: Track = last_track_id.fetch_track() last_track: Track = last_track_id.fetch_track()
artists = ", ".join(last_track.artists_name())
title = last_track.title
print(f"Currently playing: {artists} - {title}")
if "podcast" in last_track.type: if "podcast" in last_track.type:
if last_track_id not in podcasts_listened: if last_track_id not in podcasts_listened:
if last_track_id == latest_podcast and not latest_sent: if last_track_id == latest_podcast and not latest_sent:
latest_sent = True latest_sent = True
podcasts_listened.append(last_track_id) podcasts_listened.append(last_track_id)
title = last_track.title
album = last_track.albums[0] album = last_track.albums[0]
url = f"https://music.yandex.ru/track/{last_track.id}" url = f"https://music.yandex.ru/track/{last_track.id}"
desc = last_track.short_description.split("\n")[0] desc = (
last_track.short_description.split("\n")[0]
if last_track.short_description
else ""
)
with tempfile.TemporaryDirectory() as temp: with tempfile.TemporaryDirectory() as temp:
last_track.download_cover(filename=temp + "cover.png") last_track.download_cover(filename=temp + "cover.png")
img_path = os.path.abspath(temp + "cover.png") img_path = os.path.abspath(temp + "cover.png")
@ -82,24 +108,25 @@ while True:
tag.save() tag.save()
with open(path, "rb") as tmp: audio_file = InputFile(path)
obj = BytesIO(tmp.read()) await bot.send_audio(
obj.name = f"{title}.mp3"
loop = asyncio.get_event_loop()
coroutine = bot.send_audio(
chat_id=CHAT_ID, chat_id=CHAT_ID,
audio=obj, audio=audio_file,
caption=f"{title} - {album.title}\n{desc}\n\n{url}", caption=f"{title} - {album.title}\n{desc}\n\n{url}",
title=title, title=title,
performer=album.title, performer=album.title,
) )
loop.run_until_complete(coroutine)
else: else:
latest_podcast = last_track_id latest_podcast = last_track_id
latest_sent = False latest_sent = False
except BaseException as e: else:
loop = asyncio.get_event_loop() print(f"Current track is not a podcast: {artists} - {title}")
coroutine = bot.send_message(868474142, text=str(e)) except Exception as e:
loop.run_until_complete(coroutine) print(f"An error occurred: {str(e)}")
sleep(60) await bot.send_message(868474142, text=f"An error occurred: {str(e)}")
await asyncio.sleep(5)
if __name__ == "__main__":
asyncio.run(main())

View File

@ -1,23 +1,27 @@
aiofiles==23.1.0 aiofiles==23.2.1
aiogram==2.25.1 aiogram==3.10.0
aiohttp==3.8.4 aiohttp==3.9.5
aiosignal==1.3.1 aiosignal==1.3.1
async-timeout==4.0.2 annotated-types==0.7.0
attrs==22.2.0 attrs==23.2.0
Babel==2.9.1 certifi==2024.7.4
certifi==2022.12.7 charset-normalizer==3.3.2
charset-normalizer==3.0.1 docutils==0.21.2
frozenlist==1.3.3 frozenlist==1.4.1
idna==3.4 idna==3.7
magic-filter==1.0.9 lockfile==0.12.2
multidict==6.0.4 magic-filter==1.0.12
mutagen==1.45.1 multidict==6.0.5
mutagen==1.47.0
pydantic==2.8.2
pydantic_core==2.20.1
pydub==0.25.1 pydub==0.25.1
PySocks==1.7.1 PySocks==1.7.1
python-dotenv==0.21.1 python-daemon==3.0.1
pytz==2022.7.1 python-dotenv==1.0.1
requests==2.28.2 requests==2.32.3
urllib3==1.26.14 setuptools==71.0.4
yandex-music==2.0.1 typing_extensions==4.12.2
yarl==1.8.2 urllib3==2.2.2
python-daemon==2.3.2 yandex-music==2.2.0
yarl==1.9.4