From 630b289dbc2e7ac485f454b164903920c47e7a0c Mon Sep 17 00:00:00 2001 From: tehkillerbee Date: Tue, 1 Oct 2024 22:29:57 +0200 Subject: [PATCH] Add track to user playlist from ISRC (Fixes #96) Formatting, updated changelog Add track to user playlist, user tracks from ISRC (#96) --- HISTORY.rst | 1 + tidalapi/playlist.py | 33 +++++++++++++++++++++++++++++++-- tidalapi/user.py | 20 ++++++++++++++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 2afd26a..241401c 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -4,6 +4,7 @@ History ======= v0.7.7 ------ +* Feature: Add track to user playlist, user tracks from ISRC (#96) - tehkillerbee_ * Feature: Add support for moving playlist items (#116) - tehkillerbee_ * Feature: Allow adding items multiple times to the same playlist - tehkillerbee_ * Feature: Add support for adding items to a playlists at a specific position (#116) - tehkillerbee_ diff --git a/tidalapi/playlist.py b/tidalapi/playlist.py index d18ad7a..2e6deee 100644 --- a/tidalapi/playlist.py +++ b/tidalapi/playlist.py @@ -276,8 +276,8 @@ def add( :param media_ids: List of Media IDs to add. :param allow_duplicates: Allow adding duplicate items - :param position: Insert items at a specific position. Default: insert at the end - of the playlist + :param position: Insert items at a specific position. + Default: insert at the end of the playlist :return: True, if successful. """ # Insert items at a specific index @@ -302,6 +302,35 @@ def add( self._reparse() return res.ok + def add_by_isrc( + self, + isrc: str, + allow_duplicates: bool = False, + position: int = -1, + ) -> bool: + """Add an item to a playlist, using the track ISRC. + + :param isrc: The ISRC of the track to be added + :param allow_duplicates: Allow adding duplicate items + :param position: Insert items at a specific position. + Default: insert at the end of the playlist + :return: True, if successful. + """ + try: + track = self.session.get_tracks_by_isrc(isrc) + if track: + # Add the first track in the list + track_id = str(track[0].id) + return self.add( + [track_id], + allow_duplicates=allow_duplicates, + position=position, + ) + else: + return False + except ObjectNotFound: + return False + def move_by_id(self, media_id: str, position: int) -> bool: """Move an item to a new position, by media ID. diff --git a/tidalapi/user.py b/tidalapi/user.py index 46165ea..f3d2506 100644 --- a/tidalapi/user.py +++ b/tidalapi/user.py @@ -28,6 +28,7 @@ from typing import TYPE_CHECKING, List, Optional, Union, cast from urllib.parse import urljoin +from tidalapi.exceptions import ObjectNotFound from tidalapi.types import JsonObj if TYPE_CHECKING: @@ -263,6 +264,25 @@ def add_track(self, track_id: str) -> bool: "POST", f"{self.base_url}/tracks", data={"trackId": track_id} ).ok + def add_track_by_isrc(self, isrc: str) -> bool: + """Adds a track to the users favorites, using isrc. + + :param isrc: The ISRC of the track to be added + :return: True, if successful. + """ + try: + track = self.session.get_tracks_by_isrc(isrc) + if track: + # Add the first track in the list + track_id = str(track[0].id) + return self.requests.request( + "POST", f"{self.base_url}/tracks", data={"trackId": track_id} + ).ok + else: + return False + except ObjectNotFound: + return False + def add_video(self, video_id: str) -> bool: """Adds a video to the users favorites.