2024-04-27 21:08:20 +03:00
|
|
|
import re
|
|
|
|
|
|
|
|
from lxml import html, etree # type: ignore
|
2024-04-28 12:44:53 +03:00
|
|
|
|
|
|
|
import shared
|
2024-04-27 21:08:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
# Matches not only latin symbols,
|
|
|
|
# but also cyrillic alphabet, Chinese hyeroglyphics...
|
|
|
|
word_regex = re.compile(r'\w+')
|
|
|
|
|
|
|
|
# Get all <div>s with lyrics
|
|
|
|
lyrics_xpath = etree.XPath('//div[@id="lyrics-root"][1]/div[@data-lyrics-container="true"]')
|
|
|
|
|
|
|
|
# Get <br>s to add newlines after them for correct text parsing
|
|
|
|
br_xpath = etree.XPath('.//br')
|
|
|
|
|
|
|
|
|
2024-04-28 12:44:53 +03:00
|
|
|
def search(title: str, artist: str) -> str:
|
2024-04-27 21:08:20 +03:00
|
|
|
'''Searches for Genius lyrics using SearXNG + Yahoo and returns the first URL.
|
|
|
|
Irrelevant texts should be picked manually'''
|
|
|
|
|
2024-04-28 12:44:53 +03:00
|
|
|
resp = shared.get_http_pool().request(
|
2024-04-27 21:08:20 +03:00
|
|
|
'GET',
|
|
|
|
'https://searx.dc09.ru/search',
|
|
|
|
fields={
|
|
|
|
'q': artist + ' ' + title + ' site:genius.com',
|
|
|
|
'engines': 'yahoo',
|
|
|
|
'safesearch': '0',
|
|
|
|
'format': 'json',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
result: dict[str, str] = resp.json()['results'][0]
|
|
|
|
del resp
|
|
|
|
|
|
|
|
return result['url']
|
|
|
|
|
|
|
|
|
2024-04-28 12:44:53 +03:00
|
|
|
def parse(url: str) -> str:
|
2024-04-27 21:08:20 +03:00
|
|
|
'''Requests a lyrics page and parses with libxml2,
|
|
|
|
leaving only text and line breaks'''
|
|
|
|
|
2024-04-28 12:44:53 +03:00
|
|
|
resp = shared.get_http_pool().request('GET', url)
|
2024-04-27 21:08:20 +03:00
|
|
|
tree = html.document_fromstring(resp.data)
|
|
|
|
divs = lyrics_xpath(tree)
|
|
|
|
del resp, tree
|
|
|
|
|
|
|
|
return '\n'.join(_parse_text(div) for div in divs).strip()
|
|
|
|
|
|
|
|
|
|
|
|
def _parse_text(elem: html.HtmlElement) -> str:
|
|
|
|
|
|
|
|
for br in br_xpath(elem):
|
|
|
|
br.tail = '\n' + (br.tail or '')
|
|
|
|
|
|
|
|
return elem.text_content()
|