Skip to content

Commit

Permalink
Merge pull request #54 from hudsonbrendon/fix/matcher-subtitle-get
Browse files Browse the repository at this point in the history
fix: refactor method matcher_subtitle_get
  • Loading branch information
hudsonbrendon authored Jan 20, 2025
2 parents 56fc5fb + 0c561eb commit 7b70974
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
32 changes: 16 additions & 16 deletions pymusixmatch/musixmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def _apikey(self) -> str:
"""
return self.__api_key

def _request(self, url: str) -> dict:
def _request(self, url: str, format: Format = Format.JSON) -> dict:
"""Get the request.
Args:
Expand All @@ -47,6 +47,8 @@ def _request(self, url: str) -> dict:
dict: The request.
"""
request = requests.get(url)
if format == Format.XML:
return request.text
return request.json()

def _set_page_size(self, page_size: int) -> int:
Expand Down Expand Up @@ -366,7 +368,7 @@ def matcher_lyrics_get(
return data

def matcher_track_get(
self, q_track: str, q_artist: str, q_album: Optional[str] = None
self, q_track: str, q_artist: str, q_album: Optional[str] = ""
) -> dict:
"""Match your song against our database.
Expand Down Expand Up @@ -401,12 +403,11 @@ def matcher_track_get(

def matcher_subtitle_get(
self,
q_track,
q_artist,
f_subtitle_length,
f_subtitle_length_max_deviation,
track_isrc=None,
_format="json",
q_track: str,
q_artist: str,
f_subtitle_length: int,
f_subtitle_length_max_deviation: int,
track_isrc: Optional[str] = "",
):
"""Get the subtitles for a song given his title,artist and duration.
Expand All @@ -415,14 +416,13 @@ def matcher_subtitle_get(
Parameters:
q_track - The song title.
q_artist - The song artist.
f_subtitle_length - Filter by subtitle length in seconds.
f_subtitle_length_max_deviation - Max deviation for a subtitle
q_track (str): The song title.
q_artist (str): The song artist.
f_subtitle_length (int): Filter by subtitle length in seconds.
f_subtitle_length_max_deviation (int): Max deviation for a subtitle
length in seconds.
track_isrc - If you have an available isrc id in your catalogue
track_isrc (str): If you have an available isrc id in your catalogue
you can query using this id only (optional).
format - Decide the output type json or xml (default json).
Note: This method requires a commercial plan.
"""
Expand All @@ -431,15 +431,15 @@ def matcher_subtitle_get(
"matcher.subtitle.get?q_track={}"
"&q_artist={}&f_subtitle_length={}"
"&f_subtitle_length_max_deviation={}"
"&track_isrc={}&format={}".format(
"&track_isrc={}".format(
q_track,
q_artist,
f_subtitle_length,
f_subtitle_length_max_deviation,
track_isrc,
_format,
),
),
format=Format.XML,
)
return data

Expand Down
30 changes: 30 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,33 @@ def matcher_track_get() -> dict:
"body": {"track": "track'"},
}
}


@pytest.fixture
def matcher_subtitle_get() -> str:
return """
<?xml version="1.0" encoding="utf-8"?>
<message>
<header>
<status_code>200</status_code>
<execute_time>0.064241886138916</execute_time>
</header>
<body>
<subtitle>
<subtitle_id>28141</subtitle_id>
<restricted>0</restricted>
<subtitle_body>[0:16.12] When I walk on by, girls be looking like damn he fly
[0:19.69] I pay to the beat, walking on the street with in my new lafreak, yeah
[0:23.48] This is how I roll, animal print, pants out control,
... </subtitle_body>
<subtitle_length>199</subtitle_length>
<subtitle_language>en</subtitle_language>
<script_tracking_url>http://tracking.musixmatch.com/t1.0/5RIyfxxrQ==/</script_tracking_url>
<pixel_tracking_url>http://tracking.musixmatch.com/t1.0/5RIyxx=/</pixel_tracking_url>
<lyrics_copyright/>
<updated_time>2012-04-01T21:53:11Z</updated_time>
</subtitle>
</body>
</message>
"""
15 changes: 8 additions & 7 deletions tests/test_musixmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,15 @@ def test_matcher_track_get(self, requests_mock, matcher_track_get: dict) -> None
request = self.musixmatch.matcher_track_get("Let Me Love You", "justinbieber")
assert matcher_track_get == request

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

@pytest.mark.skip("Refactor test")
def test_artist_get(self):
Expand Down

0 comments on commit 7b70974

Please sign in to comment.