Skip to content

Commit 7ee9fd0

Browse files
committed
Add track to user playlist, user tracks from ISRC (#96). Updated changelog.
1 parent 48459c8 commit 7ee9fd0

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

HISTORY.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ History
44
=======
55
v0.7.7
66
------
7+
* Feature: Add track to user playlist, user tracks from ISRC (#96) - tehkillerbee_
78
* Feature: Add support for moving playlist items (#116) - tehkillerbee_
89
* Feature: Allow adding items multiple times to the same playlist - tehkillerbee_
910
* Feature: Add support for adding items to a playlists at a specific position (#116) - tehkillerbee_

tidalapi/playlist.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,8 @@ def add(
276276
277277
:param media_ids: List of Media IDs to add.
278278
:param allow_duplicates: Allow adding duplicate items
279-
:param position: Insert items at a specific position. Default: insert at the end
280-
of the playlist
279+
:param position: Insert items at a specific position.
280+
Default: insert at the end of the playlist
281281
:return: True, if successful.
282282
"""
283283
# Insert items at a specific index
@@ -302,6 +302,35 @@ def add(
302302
self._reparse()
303303
return res.ok
304304

305+
def add_by_isrc(
306+
self,
307+
isrc: str,
308+
allow_duplicates: bool = False,
309+
position: int = -1,
310+
) -> bool:
311+
"""Add an item to a playlist, using the track ISRC.
312+
313+
:param isrc: The ISRC of the track to be added
314+
:param allow_duplicates: Allow adding duplicate items
315+
:param position: Insert items at a specific position.
316+
Default: insert at the end of the playlist
317+
:return: True, if successful.
318+
"""
319+
try:
320+
track = self.session.get_tracks_by_isrc(isrc)
321+
if track:
322+
# Add the first track in the list
323+
track_id = str(track[0].id)
324+
return self.add(
325+
[track_id],
326+
allow_duplicates=allow_duplicates,
327+
position=position,
328+
)
329+
else:
330+
return False
331+
except ObjectNotFound:
332+
return False
333+
305334
def move_by_id(self, media_id: str, position: int) -> bool:
306335
"""Move an item to a new position, by media ID.
307336

tidalapi/user.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from typing import TYPE_CHECKING, List, Optional, Union, cast
2929
from urllib.parse import urljoin
3030

31+
from tidalapi.exceptions import ObjectNotFound
3132
from tidalapi.types import JsonObj
3233

3334
if TYPE_CHECKING:
@@ -263,6 +264,25 @@ def add_track(self, track_id: str) -> bool:
263264
"POST", f"{self.base_url}/tracks", data={"trackId": track_id}
264265
).ok
265266

267+
def add_track_by_isrc(self, isrc: str) -> bool:
268+
"""Adds a track to the users favorites, using isrc.
269+
270+
:param isrc: The ISRC of the track to be added
271+
:return: True, if successful.
272+
"""
273+
try:
274+
track = self.session.get_tracks_by_isrc(isrc)
275+
if track:
276+
# Add the first track in the list
277+
track_id = str(track[0].id)
278+
return self.requests.request(
279+
"POST", f"{self.base_url}/tracks", data={"trackId": track_id}
280+
).ok
281+
else:
282+
return False
283+
except ObjectNotFound:
284+
return False
285+
266286
def add_video(self, video_id: str) -> bool:
267287
"""Adds a video to the users favorites.
268288

0 commit comments

Comments
 (0)