Skip to content

Commit

Permalink
Merge pull request #36 from alistairmaclean/system-playlists
Browse files Browse the repository at this point in the history
Add support for system-playlist kind so that "discover" playlists are loaded
  • Loading branch information
3jackdaws authored Mar 13, 2023
2 parents b184c31 + 5df335d commit 813b931
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 38 deletions.
6 changes: 1 addition & 5 deletions sclib/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ async def resolve(self, url): # pylint: disable=invalid-overridden-method
if obj['kind'] == 'track':
return Track(obj=obj, client=self)

if obj['kind'] == 'playlist':
if obj['kind'] in ('playlist', 'system-playlist'):
playlist = Playlist(obj=obj, client=self)
await playlist.clean_attributes()
return playlist
Expand Down Expand Up @@ -184,10 +184,6 @@ async def clean_attributes(self): # pylint: disable=invalid-overridden-method
if track not in self.tracks:
self.tracks.append(track)


def __len__(self):
return int(self.track_count)

async def __aiter__(self):
await self.clean_attributes()
for track in self.tracks:
Expand Down
4 changes: 2 additions & 2 deletions sclib/sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def resolve(self, url):
obj = get_obj_from(url)
if obj['kind'] == 'track':
return Track(obj=obj, client=self)
if obj['kind'] == 'playlist':
if obj['kind'] in ('playlist', 'system-playlist'):
playlist = Playlist(obj=obj, client=self)
playlist.clean_attributes()
return playlist
Expand Down Expand Up @@ -356,7 +356,7 @@ def clean_attributes(self):
self.tracks = track_objects

def __len__(self):
return int(self.track_count)
return len(self.tracks)

def __iter__(self):
self.clean_attributes()
Expand Down
38 changes: 25 additions & 13 deletions tests/async/test_playlist.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
"""
Test async playlist
Test async playlists
"""
import pytest_asyncio
import pytest

from sclib.asyncio import SoundcloudAPI, Playlist
from sclib.asyncio import SoundcloudAPI

PLAYLIST_URL = 'https://soundcloud.com/soundcloud-circuits/sets/web-tempo-future-dance-and-electronic'
TEST_PLAYLIST = None
pytest_plugins = ('pytest_asyncio',)


@pytest_asyncio.fixture(name='test_playlist')
async def playlist_fixture():
@pytest.fixture(name='sclib')
def sclib_fixture():
""" Ex playlist """
return await SoundcloudAPI().resolve(PLAYLIST_URL)
return SoundcloudAPI()


@pytest.mark.asyncio
@pytest.mark.parametrize("playlist_url", [
"https://soundcloud.com/soundcloud-circuits/sets/web-tempo-future-dance-and-electronic",
"https://soundcloud.com/discover/sets/artist-stations:127466931",
])
async def test_playlist_is_resolved(sclib: SoundcloudAPI, playlist_url: str):
""" Test async playlist is resolved """
await sclib.resolve(playlist_url)

def test_playlist_size(test_playlist: Playlist):
""" Test async playlist size """
assert len(test_playlist) > 0
test_playlist.clean_attributes()
assert len(test_playlist) == len(test_playlist.tracks)

@pytest.mark.asyncio
@pytest.mark.parametrize("playlist_url,expected_playlist_kind", [
("https://soundcloud.com/soundcloud-circuits/sets/web-tempo-future-dance-and-electronic", "playlist"),
("https://soundcloud.com/discover/sets/artist-stations:127466931", "system-playlist"),
])
async def test_playlist_type(sclib: SoundcloudAPI, playlist_url: str, expected_playlist_kind: str):
""" Test async playlist type """
test_playlist = await sclib.resolve(playlist_url)
assert test_playlist.kind == expected_playlist_kind
41 changes: 23 additions & 18 deletions tests/sync/test_playlist.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,31 @@
""" Test sync playlists """

"""
Test async playlists
"""
import pytest

from sclib import SoundcloudAPI, Playlist

PLAYLIST_URL = 'https://soundcloud.com/soundcloud-circuits/sets/web-tempo-future-dance-and-electronic'
TEST_PLAYLIST = None


@pytest.fixture(name="playlist")
def test_playlist():
""" Example playlist fixture """
from sclib.sync import SoundcloudAPI

return SoundcloudAPI().resolve(PLAYLIST_URL)

@pytest.fixture(name='sclib')
def sclib_fixture():
""" Ex playlist """
return SoundcloudAPI()


def test_playlist_size(playlist: Playlist):
""" Test that playlist size is correct """
assert len(playlist) > 0 and type(len(playlist)) is int
@pytest.mark.parametrize("playlist_url", [
"https://soundcloud.com/soundcloud-circuits/sets/web-tempo-future-dance-and-electronic",
"https://soundcloud.com/discover/sets/artist-stations:127466931",
])
def test_playlist_is_resolved(sclib: SoundcloudAPI, playlist_url: str):
""" Test async playlist is resolved """
sclib.resolve(playlist_url)


def test_playlist_fetch_tracks(playlist: Playlist):
""" Test that number of tracks matches size of playlist """
assert len(playlist.tracks) is len(playlist)
@pytest.mark.parametrize("playlist_url,expected_playlist_kind", [
("https://soundcloud.com/soundcloud-circuits/sets/web-tempo-future-dance-and-electronic", "playlist"),
("https://soundcloud.com/discover/sets/artist-stations:127466931", "system-playlist"),
])
def test_playlist_type(sclib: SoundcloudAPI, playlist_url: str, expected_playlist_kind: str):
""" Test async playlist type """
test_playlist = sclib.resolve(playlist_url)
assert test_playlist.kind == expected_playlist_kind

0 comments on commit 813b931

Please sign in to comment.