mirror of
https://github.com/Alexander-D-Karpov/scripts.git
synced 2024-11-21 19:46:38 +03:00
updated ynadex podcasts
This commit is contained in:
parent
0dc45b8935
commit
170f7e12e7
|
@ -1,12 +1,10 @@
|
|||
import asyncio
|
||||
import os
|
||||
import tempfile
|
||||
from io import BytesIO
|
||||
|
||||
from time import sleep
|
||||
|
||||
from aiogram import Bot
|
||||
from aiogram.bot.api import TelegramAPIServer
|
||||
from aiogram import Bot, Dispatcher
|
||||
from aiogram.client.telegram import TelegramAPIServer
|
||||
from aiogram.types import InputFile
|
||||
from mutagen.easyid3 import EasyID3
|
||||
from mutagen.mp3 import MP3
|
||||
from mutagen.id3 import APIC, ID3, TORY
|
||||
|
@ -23,83 +21,112 @@ TELEGRAM_SERVER = os.getenv("TELEGRAM_SERVER", default=None)
|
|||
|
||||
if TELEGRAM_SERVER:
|
||||
local_server = TelegramAPIServer.from_base(TELEGRAM_SERVER)
|
||||
bot = Bot(TOKEN, server=local_server)
|
||||
bot = Bot(TOKEN, base_url=local_server.base)
|
||||
else:
|
||||
bot = Bot(TOKEN)
|
||||
|
||||
dp = Dispatcher()
|
||||
|
||||
client = Client(YANDEX_TOKEN).init()
|
||||
latest_podcast = None
|
||||
latest_sent = True
|
||||
podcasts_listened = []
|
||||
|
||||
while True:
|
||||
try:
|
||||
queues = client.queues_list()
|
||||
last_queue = client.queue(queues[0].id)
|
||||
|
||||
last_track_id = last_queue.get_current_track()
|
||||
last_track: Track = last_track_id.fetch_track()
|
||||
async def main():
|
||||
global latest_podcast, latest_sent
|
||||
|
||||
if "podcast" in last_track.type:
|
||||
if last_track_id not in podcasts_listened:
|
||||
if last_track_id == latest_podcast and not latest_sent:
|
||||
latest_sent = True
|
||||
podcasts_listened.append(last_track_id)
|
||||
while True:
|
||||
try:
|
||||
|
||||
title = last_track.title
|
||||
album = last_track.albums[0]
|
||||
url = f"https://music.yandex.ru/track/{last_track.id}"
|
||||
desc = last_track.short_description.split("\n")[0]
|
||||
with tempfile.TemporaryDirectory() as temp:
|
||||
last_track.download_cover(filename=temp + "cover.png")
|
||||
img_path = os.path.abspath(temp + "cover.png")
|
||||
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_track.download(filename=temp + "file", codec="mp3")
|
||||
orig_path = os.path.abspath(temp + "file")
|
||||
path = os.path.abspath(temp + "file.mp3")
|
||||
last_queue = client.queue(queues[0].id)
|
||||
last_track_id = last_queue.get_current_track()
|
||||
|
||||
AudioSegment.from_file(orig_path).export(path)
|
||||
os.remove(orig_path)
|
||||
if last_track_id is None:
|
||||
print(
|
||||
"No track currently playing. Waiting for 60 seconds before checking again."
|
||||
)
|
||||
await asyncio.sleep(60)
|
||||
continue
|
||||
|
||||
# set music meta
|
||||
tag = MP3(path, ID3=ID3)
|
||||
tag.tags.add(
|
||||
APIC(
|
||||
encoding=3, # 3 is for utf-8
|
||||
mime="image/png", # image/jpeg or image/png
|
||||
type=3, # 3 is for the cover image
|
||||
desc="Cover",
|
||||
data=open(img_path, "rb").read(),
|
||||
)
|
||||
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 last_track_id not in podcasts_listened:
|
||||
if last_track_id == latest_podcast and not latest_sent:
|
||||
latest_sent = True
|
||||
podcasts_listened.append(last_track_id)
|
||||
|
||||
album = last_track.albums[0]
|
||||
url = f"https://music.yandex.ru/track/{last_track.id}"
|
||||
desc = (
|
||||
last_track.short_description.split("\n")[0]
|
||||
if last_track.short_description
|
||||
else ""
|
||||
)
|
||||
tag.tags.add(TORY(text=str(album.year)))
|
||||
tag.save()
|
||||
tag = EasyID3(path)
|
||||
|
||||
tag["title"] = title
|
||||
tag["album"] = album.title
|
||||
with tempfile.TemporaryDirectory() as temp:
|
||||
last_track.download_cover(filename=temp + "cover.png")
|
||||
img_path = os.path.abspath(temp + "cover.png")
|
||||
|
||||
tag.save()
|
||||
last_track.download(filename=temp + "file", codec="mp3")
|
||||
orig_path = os.path.abspath(temp + "file")
|
||||
path = os.path.abspath(temp + "file.mp3")
|
||||
|
||||
with open(path, "rb") as tmp:
|
||||
obj = BytesIO(tmp.read())
|
||||
obj.name = f"{title}.mp3"
|
||||
loop = asyncio.get_event_loop()
|
||||
coroutine = bot.send_audio(
|
||||
AudioSegment.from_file(orig_path).export(path)
|
||||
os.remove(orig_path)
|
||||
|
||||
# set music meta
|
||||
tag = MP3(path, ID3=ID3)
|
||||
tag.tags.add(
|
||||
APIC(
|
||||
encoding=3, # 3 is for utf-8
|
||||
mime="image/png", # image/jpeg or image/png
|
||||
type=3, # 3 is for the cover image
|
||||
desc="Cover",
|
||||
data=open(img_path, "rb").read(),
|
||||
)
|
||||
)
|
||||
tag.tags.add(TORY(text=str(album.year)))
|
||||
tag.save()
|
||||
tag = EasyID3(path)
|
||||
|
||||
tag["title"] = title
|
||||
tag["album"] = album.title
|
||||
|
||||
tag.save()
|
||||
|
||||
audio_file = InputFile(path)
|
||||
await bot.send_audio(
|
||||
chat_id=CHAT_ID,
|
||||
audio=obj,
|
||||
audio=audio_file,
|
||||
caption=f"{title} - {album.title}\n{desc}\n\n{url}",
|
||||
title=title,
|
||||
performer=album.title,
|
||||
)
|
||||
loop.run_until_complete(coroutine)
|
||||
|
||||
else:
|
||||
latest_podcast = last_track_id
|
||||
latest_sent = False
|
||||
except BaseException as e:
|
||||
loop = asyncio.get_event_loop()
|
||||
coroutine = bot.send_message(868474142, text=str(e))
|
||||
loop.run_until_complete(coroutine)
|
||||
sleep(60)
|
||||
else:
|
||||
latest_podcast = last_track_id
|
||||
latest_sent = False
|
||||
else:
|
||||
print(f"Current track is not a podcast: {artists} - {title}")
|
||||
except Exception as e:
|
||||
print(f"An error occurred: {str(e)}")
|
||||
await bot.send_message(868474142, text=f"An error occurred: {str(e)}")
|
||||
await asyncio.sleep(5)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
|
|
|
@ -1,23 +1,27 @@
|
|||
aiofiles==23.1.0
|
||||
aiogram==2.25.1
|
||||
aiohttp==3.8.4
|
||||
aiofiles==23.2.1
|
||||
aiogram==3.10.0
|
||||
aiohttp==3.9.5
|
||||
aiosignal==1.3.1
|
||||
async-timeout==4.0.2
|
||||
attrs==22.2.0
|
||||
Babel==2.9.1
|
||||
certifi==2022.12.7
|
||||
charset-normalizer==3.0.1
|
||||
frozenlist==1.3.3
|
||||
idna==3.4
|
||||
magic-filter==1.0.9
|
||||
multidict==6.0.4
|
||||
mutagen==1.45.1
|
||||
annotated-types==0.7.0
|
||||
attrs==23.2.0
|
||||
certifi==2024.7.4
|
||||
charset-normalizer==3.3.2
|
||||
docutils==0.21.2
|
||||
frozenlist==1.4.1
|
||||
idna==3.7
|
||||
lockfile==0.12.2
|
||||
magic-filter==1.0.12
|
||||
multidict==6.0.5
|
||||
mutagen==1.47.0
|
||||
pydantic==2.8.2
|
||||
pydantic_core==2.20.1
|
||||
pydub==0.25.1
|
||||
PySocks==1.7.1
|
||||
python-dotenv==0.21.1
|
||||
pytz==2022.7.1
|
||||
requests==2.28.2
|
||||
urllib3==1.26.14
|
||||
yandex-music==2.0.1
|
||||
yarl==1.8.2
|
||||
python-daemon==2.3.2
|
||||
python-daemon==3.0.1
|
||||
python-dotenv==1.0.1
|
||||
requests==2.32.3
|
||||
setuptools==71.0.4
|
||||
typing_extensions==4.12.2
|
||||
urllib3==2.2.2
|
||||
yandex-music==2.2.0
|
||||
yarl==1.9.4
|
||||
|
|
Loading…
Reference in New Issue
Block a user