Skip to content

Commit 3148581

Browse files
committed
Google: test the entire fetch method
1 parent fc59070 commit 3148581

File tree

2 files changed

+83
-32
lines changed

2 files changed

+83
-32
lines changed

beetsplug/lyrics.py

+9-13
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ def __init__(self, config, log):
231231
self._log = log
232232
self.config = config
233233

234-
def fetch_url(self, url):
234+
def fetch_url(self, url, **kwargs):
235235
"""Retrieve the content at a given URL, or return None if the source
236236
is unreachable.
237237
"""
@@ -249,6 +249,7 @@ def fetch_url(self, url):
249249
"User-Agent": USER_AGENT,
250250
},
251251
timeout=10,
252+
**kwargs,
252253
)
253254
except requests.RequestException as exc:
254255
self._log.debug("lyrics request failed: {0}", exc)
@@ -604,11 +605,7 @@ class Google(Backend):
604605
"""Fetch lyrics from Google search results."""
605606

606607
REQUIRES_BS = True
607-
608-
def __init__(self, config, log):
609-
super().__init__(config, log)
610-
self.api_key = config["google_API_key"].as_str()
611-
self.engine_id = config["google_engine_ID"].as_str()
608+
SEARCH_URL = "https://www.googleapis.com/customsearch/v1"
612609

613610
def is_lyrics(self, text, artist=None):
614611
"""Determine whether the text seems to be valid lyrics."""
@@ -686,14 +683,13 @@ def is_page_candidate(self, url_link, url_title, title, artist):
686683
return ratio >= typo_ratio
687684

688685
def fetch(self, artist: str, title: str, *_) -> str | None:
689-
query = f"{artist} {title}"
690-
url = "https://www.googleapis.com/customsearch/v1?key=%s&cx=%s&q=%s" % (
691-
self.api_key,
692-
self.engine_id,
693-
quote(query.encode("utf-8")),
694-
)
686+
params = {
687+
"key": self.config["google_API_key"].as_str(),
688+
"cx": self.config["google_engine_ID"].as_str(),
689+
"q": f"{artist} {title}",
690+
}
695691

696-
data = self.fetch_url(url)
692+
data = self.fetch_url(self.SEARCH_URL, params=params)
697693
if not data:
698694
self._log.debug("google backend returned no data")
699695
return None

test/plugins/test_lyrics.py

+74-19
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import os
1818
from functools import partial
19+
from urllib.parse import urlparse
1920

2021
import pytest
2122

@@ -224,45 +225,99 @@ def plugin_config(self):
224225
def file_name(self):
225226
return "examplecom/beetssong"
226227

228+
@pytest.fixture
229+
def response_data(self, url_title, url):
230+
return {
231+
"items": [
232+
{
233+
"title": url_title,
234+
"link": url,
235+
"displayLink": urlparse(url).netloc,
236+
}
237+
]
238+
}
239+
240+
@pytest.fixture
241+
def fetch_lyrics(
242+
self, backend, requests_mock, response_data, artist, title
243+
):
244+
requests_mock.get(backend.SEARCH_URL, json=response_data)
245+
requests_mock.real_http = True
246+
247+
return partial(backend.fetch, artist, title)
248+
227249
@pytest.mark.on_lyrics_update
228250
@pytest.mark.parametrize(
229-
"title, url",
251+
"artist, title, url_title, url",
230252
[
231253
*(
232-
("Lady Madonna", url)
233-
for url in (
234-
"http://www.chartlyrics.com/_LsLsZ7P4EK-F-LD4dJgDQ/Lady+Madonna.aspx", # noqa: E501
235-
"http://www.absolutelyrics.com/lyrics/view/the_beatles/lady_madonna", # noqa: E501
236-
"https://www.letras.mus.br/the-beatles/275/",
237-
"https://www.lyricsmania.com/lady_madonna_lyrics_the_beatles.html",
238-
"https://www.lyricsmode.com/lyrics/b/beatles/lady_madonna.html",
239-
"https://www.paroles.net/the-beatles/paroles-lady-madonna",
240-
"https://www.songlyrics.com/the-beatles/lady-madonna-lyrics/",
241-
"https://sweetslyrics.com/the-beatles/lady-madonna-lyrics",
242-
"https://www.musica.com/letras.asp?letra=59862",
243-
"https://www.lacoccinelle.net/259956-the-beatles-lady-madonna.html",
254+
("The Beatles", "Lady Madonna", url_title, url)
255+
for url_title, url in (
256+
(
257+
"The Beatles Lady Madonna lyrics",
258+
"http://www.chartlyrics.com/_LsLsZ7P4EK-F-LD4dJgDQ/Lady+Madonna.aspx",
259+
),
260+
(
261+
"Lady Madonna Lyrics :: The Beatles - Absolute Lyrics",
262+
"http://www.absolutelyrics.com/lyrics/view/the_beatles/lady_madonna",
263+
),
264+
(
265+
"Lady Madonna - The Beatles - LETRAS.MUS.BR",
266+
"https://www.letras.mus.br/the-beatles/275/",
267+
),
268+
(
269+
"The Beatles - Lady Madonna Lyrics",
270+
"https://www.lyricsmania.com/lady_madonna_lyrics_the_beatles.html",
271+
),
272+
(
273+
"Lady Madonna lyrics by The Beatles - original song full text. Official Lady Madonna lyrics, 2024 version | LyricsMode.com", # noqa: E501
274+
"https://www.lyricsmode.com/lyrics/b/beatles/lady_madonna.html",
275+
),
276+
(
277+
"Paroles Lady Madonna par The Beatles - Lyrics - Paroles.net",
278+
"https://www.paroles.net/the-beatles/paroles-lady-madonna",
279+
),
280+
(
281+
"THE BEATLES - LADY MADONNA LYRICS",
282+
"https://www.songlyrics.com/the-beatles/lady-madonna-lyrics/",
283+
),
284+
(
285+
"The Beatles - Lady Madonna",
286+
"https://sweetslyrics.com/the-beatles/lady-madonna-lyrics",
287+
),
288+
(
289+
"Lady Madonna - Letra - The Beatles - Musica.com",
290+
"https://www.musica.com/letras.asp?letra=59862",
291+
),
292+
(
293+
"Paroles et traduction The Beatles : Lady Madonna - paroles de chanson", # noqa: E501
294+
"https://www.lacoccinelle.net/259956-the-beatles-lady-madonna.html",
295+
),
244296
)
245297
),
246298
pytest.param(
299+
"The Beatles",
247300
"Lady Madonna",
301+
"The Beatles - Lady Madonna Lyrics | AZLyrics.com",
248302
"https://www.azlyrics.com/lyrics/beatles/ladymadonna.html",
249303
marks=xfail_on_ci("AZLyrics is blocked by Cloudflare"),
250304
),
251305
(
306+
"Amy Winehouse",
252307
"Jazz'n'blues",
253-
"https://www.lyricsontop.com/amy-winehouse-songs/jazz-n-blues-lyrics.html", # noqa: E501
308+
"Amy Winehouse - Jazz N' Blues lyrics complete",
309+
"https://www.lyricsontop.com/amy-winehouse-songs/jazz-n-blues-lyrics.html",
254310
),
255311
],
256312
)
257-
def test_backend_source(self, backend, title, url):
313+
def test_backend_source(self, fetch_lyrics, title):
258314
"""Test if lyrics present on websites registered in beets google custom
259315
search engine are correctly scraped.
260316
"""
261-
response = backend.fetch_url(url)
262-
result = lyrics.scrape_lyrics_from_html(response).lower()
317+
lyrics = fetch_lyrics()
263318

264-
assert backend.is_lyrics(result)
265-
assert PHRASE_BY_TITLE[title] in result
319+
assert lyrics
320+
assert PHRASE_BY_TITLE[title].lower() in lyrics.lower()
266321

267322
def test_mocked_source_ok(self, backend, lyrics_html):
268323
"""Test that lyrics of the mocked page are correctly scraped"""

0 commit comments

Comments
 (0)