# На модуль распространяется лицензия "GNU General Public License v3.0"
# https://github.com/all-licenses/GNU-General-Public-License-v3.0
# meta developer: @PyModule
import requests
from bs4 import BeautifulSoup, Tag, NavigableString
import re
from .. import loader, utils
@loader.tds
class LyricsMod(loader.Module):
"""Модуль для поиска текста песни через Genius API напрямую"""
strings = {"name": "Lyrics"}
def __init__(self):
self.config = loader.ModuleConfig(
"GENIUS_TOKEN",
None,
lambda: "Токен для доступа к Genius API. Получите его на https://genius.com/api-clients",
)
def get_token(self):
token = self.config["GENIUS_TOKEN"]
if not token:
return None
return token
def lyrics(self, html: str) -> str:
soup = BeautifulSoup(html, "html.parser")
for remove in soup.find_all("div", class_=re.compile("LyricsHeader__Container")):
remove.decompose()
containers = soup.find_all("div", attrs={"data-lyrics-container": "true"})
if not containers:
return None
lyrics = ""
for container in containers:
for elem in container.contents:
if isinstance(elem, Tag):
if elem.name == "br":
lyrics += "\n"
else:
lyrics += elem.get_text(separator="\n")
elif isinstance(elem, NavigableString):
lyrics += str(elem)
lyrics = re.sub(r"\n{3,}", "\n\n", lyrics)
return lyrics.strip()
async def lyricscmd(self, message):
"""[запрос] - Найти текст песни по запросу"""
token = self.get_token()
if not token:
return await message.edit(
".cfg Lyrics."
)
query = utils.get_args_raw(message)
if not query:
return await message.edit("
{lyrics}" else: text += f"