Skip to content

Commit 7b70974

Browse files
Merge pull request #54 from hudsonbrendon/fix/matcher-subtitle-get
fix: refactor method matcher_subtitle_get
2 parents 56fc5fb + 0c561eb commit 7b70974

File tree

3 files changed

+54
-23
lines changed

3 files changed

+54
-23
lines changed

pymusixmatch/musixmatch.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def _apikey(self) -> str:
3737
"""
3838
return self.__api_key
3939

40-
def _request(self, url: str) -> dict:
40+
def _request(self, url: str, format: Format = Format.JSON) -> dict:
4141
"""Get the request.
4242
4343
Args:
@@ -47,6 +47,8 @@ def _request(self, url: str) -> dict:
4747
dict: The request.
4848
"""
4949
request = requests.get(url)
50+
if format == Format.XML:
51+
return request.text
5052
return request.json()
5153

5254
def _set_page_size(self, page_size: int) -> int:
@@ -366,7 +368,7 @@ def matcher_lyrics_get(
366368
return data
367369

368370
def matcher_track_get(
369-
self, q_track: str, q_artist: str, q_album: Optional[str] = None
371+
self, q_track: str, q_artist: str, q_album: Optional[str] = ""
370372
) -> dict:
371373
"""Match your song against our database.
372374
@@ -401,12 +403,11 @@ def matcher_track_get(
401403

402404
def matcher_subtitle_get(
403405
self,
404-
q_track,
405-
q_artist,
406-
f_subtitle_length,
407-
f_subtitle_length_max_deviation,
408-
track_isrc=None,
409-
_format="json",
406+
q_track: str,
407+
q_artist: str,
408+
f_subtitle_length: int,
409+
f_subtitle_length_max_deviation: int,
410+
track_isrc: Optional[str] = "",
410411
):
411412
"""Get the subtitles for a song given his title,artist and duration.
412413
@@ -415,14 +416,13 @@ def matcher_subtitle_get(
415416
416417
Parameters:
417418
418-
q_track - The song title.
419-
q_artist - The song artist.
420-
f_subtitle_length - Filter by subtitle length in seconds.
421-
f_subtitle_length_max_deviation - Max deviation for a subtitle
419+
q_track (str): The song title.
420+
q_artist (str): The song artist.
421+
f_subtitle_length (int): Filter by subtitle length in seconds.
422+
f_subtitle_length_max_deviation (int): Max deviation for a subtitle
422423
length in seconds.
423-
track_isrc - If you have an available isrc id in your catalogue
424+
track_isrc (str): If you have an available isrc id in your catalogue
424425
you can query using this id only (optional).
425-
format - Decide the output type json or xml (default json).
426426
427427
Note: This method requires a commercial plan.
428428
"""
@@ -431,15 +431,15 @@ def matcher_subtitle_get(
431431
"matcher.subtitle.get?q_track={}"
432432
"&q_artist={}&f_subtitle_length={}"
433433
"&f_subtitle_length_max_deviation={}"
434-
"&track_isrc={}&format={}".format(
434+
"&track_isrc={}".format(
435435
q_track,
436436
q_artist,
437437
f_subtitle_length,
438438
f_subtitle_length_max_deviation,
439439
track_isrc,
440-
_format,
441440
),
442441
),
442+
format=Format.XML,
443443
)
444444
return data
445445

tests/conftest.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,33 @@ def matcher_track_get() -> dict:
188188
"body": {"track": "track'"},
189189
}
190190
}
191+
192+
193+
@pytest.fixture
194+
def matcher_subtitle_get() -> str:
195+
return """
196+
<?xml version="1.0" encoding="utf-8"?>
197+
<message>
198+
<header>
199+
<status_code>200</status_code>
200+
<execute_time>0.064241886138916</execute_time>
201+
</header>
202+
<body>
203+
<subtitle>
204+
<subtitle_id>28141</subtitle_id>
205+
<restricted>0</restricted>
206+
<subtitle_body>[0:16.12] When I walk on by, girls be looking like damn he fly
207+
[0:19.69] I pay to the beat, walking on the street with in my new lafreak, yeah
208+
[0:23.48] This is how I roll, animal print, pants out control,
209+
... </subtitle_body>
210+
<subtitle_length>199</subtitle_length>
211+
<subtitle_language>en</subtitle_language>
212+
<script_tracking_url>http://tracking.musixmatch.com/t1.0/5RIyfxxrQ==/</script_tracking_url>
213+
<pixel_tracking_url>http://tracking.musixmatch.com/t1.0/5RIyxx=/</pixel_tracking_url>
214+
215+
<lyrics_copyright/>
216+
<updated_time>2012-04-01T21:53:11Z</updated_time>
217+
</subtitle>
218+
</body>
219+
</message>
220+
"""

tests/test_musixmatch.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -181,14 +181,15 @@ def test_matcher_track_get(self, requests_mock, matcher_track_get: dict) -> None
181181
request = self.musixmatch.matcher_track_get("Let Me Love You", "justinbieber")
182182
assert matcher_track_get == request
183183

184-
@pytest.mark.skip("Refactor test")
185-
def test_matcher_subtitle_get(self):
186-
self.assertEqual(
187-
self.musixmatch.matcher_subtitle_get("Sexy and I know it", "LMFAO", 200, 3)[
188-
"message"
189-
]["body"],
190-
"",
184+
def test_matcher_subtitle_get(
185+
self, requests_mock, matcher_subtitle_get: str
186+
) -> None:
187+
url = "https://api.musixmatch.com/ws/1.1/matcher.subtitle.get?q_artist=justinbieber&q_track=Let%20Me%20Love%20You&"
188+
requests_mock.get(url=url, text=matcher_subtitle_get)
189+
request = self.musixmatch.matcher_subtitle_get(
190+
"Let Me Love You", "justinbieber", 10, 10
191191
)
192+
assert matcher_subtitle_get == request
192193

193194
@pytest.mark.skip("Refactor test")
194195
def test_artist_get(self):

0 commit comments

Comments
 (0)