Skip to content

Commit

Permalink
Merge pull request #34 from hudsonbrendon/feat/chart-tracks-get
Browse files Browse the repository at this point in the history
Feat/chart tracks get
  • Loading branch information
hudsonbrendon authored Jan 2, 2025
2 parents 2fac045 + 0d8d3eb commit d82b6cf
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
32 changes: 20 additions & 12 deletions pymusixmatch/musixmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@


class Musixmatch(object):
BASE_URL = "http://api.musixmatch.com/ws"
BASE_URL = "https://api.musixmatch.com/ws"
VERSION = "1.1"

def __init__(self, api_key: str) -> None:
Expand Down Expand Up @@ -78,7 +78,7 @@ def chart_artists(
page (int): Define the page number for paginated results.
page_size (int): Define the page size for paginated results (range 1 - 100).
country (str): A valid country code (default US).
country (Country): A valid country code (default US).
format (Format): Decide the output type json or xml (default json).
"""
if country not in Country._value2member_map_:
Expand All @@ -98,23 +98,31 @@ def chart_artists(

def chart_tracks_get(
self,
page,
page_size,
f_has_lyrics,
country="us",
_format="json",
page: int,
page_size: int,
f_has_lyrics: bool,
country: Optional[Country] = Country.US.value,
_format: Optional[Format] = Format.JSON.value,
):
"""This api provides you the list
of the top songs of a given country.
Parameters:
page - Define the page number for paginated results.
page_size - Define the page size for paginated results (range 1 - 100).
f_has_lyrics - When set, filter only contents with lyrics.
country - A valid country code (default US).
format - Decide the output type json or xml (default json).
page (int): Define the page number for paginated results.
page_size (int): Define the page size for paginated results (range 1 - 100).
f_has_lyrics (bool): When set, filter only contents with lyrics.
country (Country): A valid country code (default US).
format (Format): Decide the output type json or xml (default json).
"""
if country not in Country._value2member_map_:
raise ValueError(
f"Invalid country code: {country}, please use a valid country code."
)

if _format not in Format._value2member_map_:
raise ValueError(f"Invalid format: {_format}, please use a valid format.")

request = self._request(
self._get_url(
f"chart.tracks.get?page={page}&page_size={self._set_page_size(page_size)}&country={country}&format={_format}&f_has_lyrics={f_has_lyrics}",
Expand Down
30 changes: 23 additions & 7 deletions tests/test_musixmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class TestMusixmatch:
@classmethod
def setup_class(cls) -> None:
cls.musixmatch = Musixmatch("test")
cls.url = "http://api.musixmatch.com/ws/1.1/"
cls.url = "https://api.musixmatch.com/ws/1.1/"

def test_get_url(self) -> None:
assert (
Expand Down Expand Up @@ -38,33 +38,49 @@ def test_set_page_size_with_invalid_page_size(
)

def test_chart_artists(self, requests_mock, chart_artists: dict) -> None:
url = "http://api.musixmatch.com/ws/1.1/chart.artists.get?page=1&page_size=1&country=us&format=json"
url = "https://api.musixmatch.com/ws/1.1/chart.artists.get?page=1&page_size=1&country=us&format=json"
requests_mock.get(url=url, json=chart_artists)
request = self.musixmatch.chart_artists(1, 1)
assert chart_artists == request

def test_chart_artists_with_invalid_country(
self, requests_mock, chart_artists: dict
) -> None:
url = "http://api.musixmatch.com/ws/1.1/chart.artists.get?page=1&page_size=1&country=invalid&format=json&apikey=test"
url = "https://api.musixmatch.com/ws/1.1/chart.artists.get?page=1&page_size=1&country=invalid&format=json&apikey=test"
requests_mock.get(url=url, json=chart_artists)
with pytest.raises(ValueError):
self.musixmatch.chart_artists(1, 1, country="invalid")

def test_chart_artists_with_invalid_format(
self, requests_mock, chart_artists: dict
) -> None:
url = "http://api.musixmatch.com/ws/1.1/chart.artists.get?page=1&page_size=1&country=us&format=invalid&apikey=test"
url = "https://api.musixmatch.com/ws/1.1/chart.artists.get?page=1&page_size=1&country=us&format=invalid&apikey=test"
requests_mock.get(url=url, json=chart_artists)
with pytest.raises(ValueError):
self.musixmatch.chart_artists(1, 1, _format="invalid")

def test_chart_tracks_get(self, requests_mock, tracks: dict) -> None:
url = "http://api.musixmatch.com/ws/1.1/chart.tracks.get?page=1&page_size=1&country=us&format=json&f_has_lyrics=1"
url = "https://api.musixmatch.com/ws/1.1/chart.tracks.get?page=1&page_size=1&country=us&format=json&f_has_lyrics=1"
requests_mock.get(url=url, json=tracks)
request = self.musixmatch.chart_tracks_get(1, 1, 1)
assert tracks == request

def test_chart_tracks_get_with_invalid_country(
self, requests_mock, tracks: dict
) -> None:
url = "https://api.musixmatch.com/ws/1.1/chart.tracks.get?page=1&page_size=1&country=invalid&format=json&f_has_lyrics=1&apikey=test"
requests_mock.get(url=url, json=tracks)
with pytest.raises(ValueError):
self.musixmatch.chart_tracks_get(1, 1, 1, country="invalid")

def test_chart_tracks_get_with_invalid_format(
self, requests_mock, tracks: dict
) -> None:
url = "https://api.musixmatch.com/ws/1.1/chart.tracks.get?page=1&page_size=1&country=us&format=invalid&f_has_lyrics=1&apikey=test"
requests_mock.get(url=url, json=tracks)
with pytest.raises(ValueError):
self.musixmatch.chart_tracks_get(1, 1, 1, _format="invalid")

@pytest.mark.skip("Refactor test")
def test_track_search(self):
self.assertEqual(
Expand Down Expand Up @@ -94,13 +110,13 @@ def test_track_get(self):
)

def test_track_lyrics_get(self, requests_mock, tracks: dict) -> None:
url = "http://api.musixmatch.com/ws/1.1/track.lyrics.get?track_id=12345"
url = "https://api.musixmatch.com/ws/1.1/track.lyrics.get?track_id=12345"
requests_mock.get(url=url, json=tracks)
request = self.musixmatch.track_lyrics_get(12345)
assert tracks == request

def test_track_snippet_get(self, requests_mock, track_snippet: dict) -> None:
url = "http://api.musixmatch.com/ws/1.1/track.snippet.get?track_id=12345"
url = "https://api.musixmatch.com/ws/1.1/track.snippet.get?track_id=12345"
requests_mock.get(url=url, json=track_snippet)
request = self.musixmatch.track_snippet_get(12345)
assert track_snippet == request
Expand Down

0 comments on commit d82b6cf

Please sign in to comment.