From 3a25d86b4fcbc7baf53b11fc7faba20daf3e798f Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Sun, 28 Apr 2024 13:44:53 +0400 Subject: [PATCH] Shared http pool, lyrics in ID3TagsPP --- backend/genius.py | 11 ++++++----- backend/id3pp.py | 12 +++++++++--- backend/shared.py | 10 ++++++++++ 3 files changed, 25 insertions(+), 8 deletions(-) create mode 100644 backend/shared.py diff --git a/backend/genius.py b/backend/genius.py index a839d6a..de70b89 100644 --- a/backend/genius.py +++ b/backend/genius.py @@ -1,7 +1,8 @@ import re from lxml import html, etree # type: ignore -import urllib3 + +import shared # Matches not only latin symbols, @@ -15,11 +16,11 @@ lyrics_xpath = etree.XPath('//div[@id="lyrics-root"][1]/div[@data-lyrics-contain br_xpath = etree.XPath('.//br') -def search(http: urllib3.PoolManager, title: str, artist: str) -> str: +def search(title: str, artist: str) -> str: '''Searches for Genius lyrics using SearXNG + Yahoo and returns the first URL. Irrelevant texts should be picked manually''' - resp = http.request( + resp = shared.get_http_pool().request( 'GET', 'https://searx.dc09.ru/search', fields={ @@ -36,11 +37,11 @@ def search(http: urllib3.PoolManager, title: str, artist: str) -> str: return result['url'] -def parse(http: urllib3.PoolManager, url: str) -> str: +def parse(url: str) -> str: '''Requests a lyrics page and parses with libxml2, leaving only text and line breaks''' - resp = http.request('GET', url) + resp = shared.get_http_pool().request('GET', url) tree = html.document_fromstring(resp.data) divs = lyrics_xpath(tree) del resp, tree diff --git a/backend/id3pp.py b/backend/id3pp.py index 24b076e..e407dbd 100644 --- a/backend/id3pp.py +++ b/backend/id3pp.py @@ -1,6 +1,8 @@ from mutagen import mp3, id3 from yt_dlp.postprocessor import PostProcessor +import genius + ENC_UTF8 = 3 @@ -44,8 +46,11 @@ class ID3TagsPP(PostProcessor): file = mp3.MP3(information['filepath']) - file['TIT2'] = id3.TIT2(encoding=ENC_UTF8, text=information['track']) - file['TPE1'] = id3.TPE1(encoding=ENC_UTF8, text=information['artist']) + title = information['track'] + artist = information['artist'] + + file['TIT2'] = id3.TIT2(encoding=ENC_UTF8, text=title) + file['TPE1'] = id3.TPE1(encoding=ENC_UTF8, text=artist) if 'album' in information: file['TALB'] = id3.TALB(encoding=ENC_UTF8, text=information['album']) if 'release_year' in information: @@ -53,6 +58,7 @@ class ID3TagsPP(PostProcessor): if 'track_number' in information: file['TRCK'] = id3.TRCK(encoding=ENC_UTF8, text=str(information['track_number'])) - # TODO: lyrics file["USLT"] + lyr_url = genius.search(title, artist) + file['USLT'] = id3.USLT(encoding=ENC_UTF8, text=genius.parse(lyr_url)) return [], information diff --git a/backend/shared.py b/backend/shared.py new file mode 100644 index 0000000..ccc0447 --- /dev/null +++ b/backend/shared.py @@ -0,0 +1,10 @@ +from urllib3 import PoolManager + + +_http: PoolManager | None = None + +def get_http_pool() -> PoolManager: + global _http + if _http is None: + _http = PoolManager() + return _http