From 11ba7a61bb0610c2563a09588ec49dfa6e39ffaf Mon Sep 17 00:00:00 2001 From: Hudson Brendon Date: Sun, 12 Jan 2025 04:01:36 -0300 Subject: [PATCH] fix: refactor method track_subtitle_get --- pymusixmatch/musixmatch.py | 28 ++++++++++++---------------- tests/conftest.py | 21 +++++++++++++++++++++ tests/test_musixmatch.py | 11 +++++------ 3 files changed, 38 insertions(+), 22 deletions(-) diff --git a/pymusixmatch/musixmatch.py b/pymusixmatch/musixmatch.py index 8d00727..dea2af8 100644 --- a/pymusixmatch/musixmatch.py +++ b/pymusixmatch/musixmatch.py @@ -265,13 +265,12 @@ def track_snippet_get(self, track_id: str) -> dict: def track_subtitle_get( self, - track_id, - track_mbid=None, - subtitle_format=None, - f_subtitle_length=None, - f_subtitle_length_max_deviation=None, - _format="json", - ): + track_id: str, + track_mbid: str = "", + subtitle_format: str = "", + f_subtitle_length: str = "", + f_subtitle_length_max_deviation: str = "", + ) -> dict: """Retreive the subtitle of a track. Return the subtitle of a track in LRC or DFXP format. @@ -280,14 +279,13 @@ def track_subtitle_get( Parameters: - track_id - The musiXmatch track id. - track_mbid - The musicbrainz track id. - subtitle_format - The format of the subtitle (lrc,dfxp,stledu). + track_id (str): The musiXmatch track id. + track_mbid (str): The musicbrainz track id. + subtitle_format (str): The format of the subtitle (lrc,dfxp,stledu). Default to lrc. - f_subtitle_length - The desired length of the subtitle (seconds). - f_subtitle_length_max_deviation - The maximum deviation allowed. + f_subtitle_length (str): The desired length of the subtitle (seconds). + f_subtitle_length_max_deviation (str): The maximum deviation allowed. from the f_subtitle_length (seconds). - format - Decide the output type json or xml (default json). """ data = self._request( self._get_url( @@ -295,14 +293,12 @@ def track_subtitle_get( "track_id={}&track_mbid={}" "&subtitle_format={}" "&f_subtitle_length={}" - "&f_subtitle_length_max_deviation={}" - "&format={}".format( + "&f_subtitle_length_max_deviation={}".format( track_id, track_mbid, subtitle_format, f_subtitle_length, f_subtitle_length_max_deviation, - _format, ), ), ) diff --git a/tests/conftest.py b/tests/conftest.py index 34f5a46..3f34d61 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -134,3 +134,24 @@ def track_richsync_get() -> dict: }, } } + + +@pytest.fixture +def track_subtitle_get() -> dict: + return { + "message": { + "header": {"status_code": 200, "execute_time": 0.0058}, + "body": { + "subtitle": { + "subtitle_id": 18116, + "restricted": 0, + "subtitle_body": "I'm tryna put you in the worst mood, ah\r\nP1 cleaner than your church shoes, ah\r\nMilli point two just to hurt you, ah\r\nAll red Lamb' just to tease you, ah\r\nNone of these toys on lease too, ah", + "subtitle_language": "en", + "script_tracking_url": "http:\/\/tracking.musixmatch.com\/t1.0\/gs2ObSlxR9vsuyJ9XYRITJw==\/", + "pixel_tracking_url": "http:\/\/tracking.musixmatch.com\/t1.0\/gs2ObSlxR9vsuGDsbZwOULTQ==\/", + "html_tracking_url": "http:\/\/tracking.musixmatch.com\/t1.0\/gs2ObSlxR9yR97gQ==\/", + "lyrics_copyright": "Lyrics powered by www.musiXmatch.com", + } + }, + } + } diff --git a/tests/test_musixmatch.py b/tests/test_musixmatch.py index 8f2a2ac..0f8d800 100644 --- a/tests/test_musixmatch.py +++ b/tests/test_musixmatch.py @@ -157,12 +157,11 @@ def test_track_snippet_get(self, requests_mock, track_snippet: dict) -> None: request = self.musixmatch.track_snippet_get(12345) assert track_snippet == request - @pytest.mark.skip("Refactor test") - def test_track_subtitle_get(self): - self.assertEqual( - self.musixmatch.track_subtitle_get(14201829)["message"]["body"], - "", - ) + def test_track_subtitle_get(self, requests_mock, track_subtitle_get: dict) -> None: + url = "https://api.musixmatch.com/ws/1.1/track.subtitle.get?track_id=14201829" + requests_mock.get(url=url, json=track_subtitle_get) + request = self.musixmatch.track_subtitle_get(14201829) + assert track_subtitle_get == request def test_track_richsync_get(self, requests_mock, track_richsync_get: dict) -> None: url = "https://api.musixmatch.com/ws/1.1/track.richsync.get?track_id=114837357"