Compare commits
No commits in common. "c354aeb183f5489f9e3d16fd8c5a90d5e97b755a" and "80a7d2bde5af7d9185483f930078eec6eff42d39" have entirely different histories.
c354aeb183
...
80a7d2bde5
3 changed files with 8 additions and 10 deletions
|
@ -2,7 +2,7 @@ import re
|
||||||
|
|
||||||
from lxml import html, etree # type: ignore
|
from lxml import html, etree # type: ignore
|
||||||
|
|
||||||
import http_pool
|
import shared
|
||||||
|
|
||||||
|
|
||||||
# Matches not only latin symbols,
|
# Matches not only latin symbols,
|
||||||
|
@ -20,7 +20,7 @@ def search(title: str, artist: str) -> str:
|
||||||
'''Searches for Genius lyrics using SearXNG + Yahoo and returns the first URL.
|
'''Searches for Genius lyrics using SearXNG + Yahoo and returns the first URL.
|
||||||
Irrelevant texts should be picked manually'''
|
Irrelevant texts should be picked manually'''
|
||||||
|
|
||||||
resp = http_pool.get().request(
|
resp = shared.get_http_pool().request(
|
||||||
'GET',
|
'GET',
|
||||||
'https://searx.dc09.ru/search',
|
'https://searx.dc09.ru/search',
|
||||||
fields={
|
fields={
|
||||||
|
@ -41,7 +41,7 @@ def parse(url: str) -> str:
|
||||||
'''Requests a lyrics page and parses with libxml2,
|
'''Requests a lyrics page and parses with libxml2,
|
||||||
leaving only text and line breaks'''
|
leaving only text and line breaks'''
|
||||||
|
|
||||||
resp = http_pool.get().request('GET', url)
|
resp = shared.get_http_pool().request('GET', url)
|
||||||
tree = html.document_fromstring(resp.data)
|
tree = html.document_fromstring(resp.data)
|
||||||
divs = lyrics_xpath(tree)
|
divs = lyrics_xpath(tree)
|
||||||
del resp, tree
|
del resp, tree
|
||||||
|
|
|
@ -3,8 +3,7 @@ from urllib3 import PoolManager
|
||||||
|
|
||||||
_http: PoolManager | None = None
|
_http: PoolManager | None = None
|
||||||
|
|
||||||
|
def get_http_pool() -> PoolManager:
|
||||||
def get() -> PoolManager:
|
|
||||||
global _http
|
global _http
|
||||||
if _http is None:
|
if _http is None:
|
||||||
_http = PoolManager()
|
_http = PoolManager()
|
|
@ -2,7 +2,6 @@ from unittest import TestCase
|
||||||
import urllib3
|
import urllib3
|
||||||
|
|
||||||
import genius
|
import genius
|
||||||
import http_pool
|
|
||||||
|
|
||||||
|
|
||||||
TITLE = 'A Line In The Sand'
|
TITLE = 'A Line In The Sand'
|
||||||
|
@ -27,17 +26,17 @@ Another day'''
|
||||||
class TestGenius(TestCase):
|
class TestGenius(TestCase):
|
||||||
|
|
||||||
def setUp(self) -> None:
|
def setUp(self) -> None:
|
||||||
http_pool.get()
|
self.http = urllib3.PoolManager()
|
||||||
|
|
||||||
def test_search_success(self) -> None:
|
def test_search_success(self) -> None:
|
||||||
url = genius.search(TITLE, ARTIST)
|
url = genius.search(self.http, TITLE, ARTIST)
|
||||||
self.assertEqual(url, URL)
|
self.assertEqual(url, URL)
|
||||||
|
|
||||||
def test_lyrics_parsing(self) -> None:
|
def test_lyrics_parsing(self) -> None:
|
||||||
lyrics = genius.parse(URL)
|
lyrics = genius.parse(self.http, URL)
|
||||||
self.assertTrue(lyrics.startswith(LYR1))
|
self.assertTrue(lyrics.startswith(LYR1))
|
||||||
self.assertTrue(LYR2 in lyrics)
|
self.assertTrue(LYR2 in lyrics)
|
||||||
self.assertTrue(LYR3 in lyrics)
|
self.assertTrue(LYR3 in lyrics)
|
||||||
|
|
||||||
def tearDown(self) -> None:
|
def tearDown(self) -> None:
|
||||||
http_pool.get().clear()
|
self.http.clear()
|
||||||
|
|
Loading…
Add table
Reference in a new issue