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 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,83 +21,112 @@ 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 = []
|
||||||
|
|
||||||
while True:
|
|
||||||
try:
|
|
||||||
queues = client.queues_list()
|
|
||||||
last_queue = client.queue(queues[0].id)
|
|
||||||
|
|
||||||
last_track_id = last_queue.get_current_track()
|
async def main():
|
||||||
last_track: Track = last_track_id.fetch_track()
|
global latest_podcast, latest_sent
|
||||||
|
|
||||||
if "podcast" in last_track.type:
|
while True:
|
||||||
if last_track_id not in podcasts_listened:
|
try:
|
||||||
if last_track_id == latest_podcast and not latest_sent:
|
|
||||||
latest_sent = True
|
|
||||||
podcasts_listened.append(last_track_id)
|
|
||||||
|
|
||||||
title = last_track.title
|
queues = client.queues_list() # Ques are now not working
|
||||||
album = last_track.albums[0]
|
if not queues:
|
||||||
url = f"https://music.yandex.ru/track/{last_track.id}"
|
print(
|
||||||
desc = last_track.short_description.split("\n")[0]
|
"No active queues found. Waiting for 60 seconds before checking again."
|
||||||
with tempfile.TemporaryDirectory() as temp:
|
)
|
||||||
last_track.download_cover(filename=temp + "cover.png")
|
await asyncio.sleep(5)
|
||||||
img_path = os.path.abspath(temp + "cover.png")
|
continue
|
||||||
|
|
||||||
last_track.download(filename=temp + "file", codec="mp3")
|
last_queue = client.queue(queues[0].id)
|
||||||
orig_path = os.path.abspath(temp + "file")
|
last_track_id = last_queue.get_current_track()
|
||||||
path = os.path.abspath(temp + "file.mp3")
|
|
||||||
|
|
||||||
AudioSegment.from_file(orig_path).export(path)
|
if last_track_id is None:
|
||||||
os.remove(orig_path)
|
print(
|
||||||
|
"No track currently playing. Waiting for 60 seconds before checking again."
|
||||||
|
)
|
||||||
|
await asyncio.sleep(60)
|
||||||
|
continue
|
||||||
|
|
||||||
# set music meta
|
last_track: Track = last_track_id.fetch_track()
|
||||||
tag = MP3(path, ID3=ID3)
|
|
||||||
tag.tags.add(
|
artists = ", ".join(last_track.artists_name())
|
||||||
APIC(
|
title = last_track.title
|
||||||
encoding=3, # 3 is for utf-8
|
print(f"Currently playing: {artists} - {title}")
|
||||||
mime="image/png", # image/jpeg or image/png
|
|
||||||
type=3, # 3 is for the cover image
|
if "podcast" in last_track.type:
|
||||||
desc="Cover",
|
if last_track_id not in podcasts_listened:
|
||||||
data=open(img_path, "rb").read(),
|
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
|
with tempfile.TemporaryDirectory() as temp:
|
||||||
tag["album"] = album.title
|
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:
|
AudioSegment.from_file(orig_path).export(path)
|
||||||
obj = BytesIO(tmp.read())
|
os.remove(orig_path)
|
||||||
obj.name = f"{title}.mp3"
|
|
||||||
loop = asyncio.get_event_loop()
|
# set music meta
|
||||||
coroutine = bot.send_audio(
|
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,
|
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())
|
||||||
|
|
|
@ -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
|
||||||
|
|
Loading…
Reference in New Issue
Block a user