Skip to content

Commit

Permalink
fix: refactor method track_subtitle_get
Browse files Browse the repository at this point in the history
  • Loading branch information
hudsonbrendon committed Jan 12, 2025
1 parent af29daa commit 11ba7a6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 22 deletions.
28 changes: 12 additions & 16 deletions pymusixmatch/musixmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -280,29 +279,26 @@ 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(
"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,
),
),
)
Expand Down
21 changes: 21 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}
},
}
}
11 changes: 5 additions & 6 deletions tests/test_musixmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 11ba7a6

Please sign in to comment.