sigma67/ytmusicapi

Edge case in edit_playlist: moving a title to the end of the playlist

AltoRetrato opened this issue · 3 comments

Hello, and thanks for the ytmusicapi! :D

The edit_playlist() method currently includes ACTION_MOVE_VIDEO_BEFORE, setVideoId, and movedSetVideoIdSuccessor in body["actions"] whenever the moveItem argument is defined. This mirrors the behavior of the YouTube Music client when a title is moved to a position above another one in a playlist.

However, when moving a title to the end of a playlist, the YouTube Music client performs a similar action but omits the movedSetVideoIdSuccessor field.

In order to allow ytmusicapi to do this as well, I suggest changing the edit_playlist() function from:

        moveItem: Optional[Tuple[str, str]] = None,
[...]
        if moveItem:
            actions.append(
                {
                    "action": "ACTION_MOVE_VIDEO_BEFORE",
                    "setVideoId": moveItem[0],
                    "movedSetVideoIdSuccessor": moveItem[1],
                }
            )

to:

        moveItem: Optional[Union[str, Tuple[str, str]]] = None,
[...]
        if moveItem:
            action = {
                        "action": "ACTION_MOVE_VIDEO_BEFORE",
                        "setVideoId": moveItem if isinstance(moveItem, str) else moveItem[0],
                     }
            if isinstance(moveItem, tuple) and len(moveItem) > 1:
                action["movedSetVideoIdSuccessor"] = moveItem[1]
            actions.append(action)

This way, moveItem can be a Tuple[str,str] (as it is today), as well as a single str or a Tuple[str,], allowing to move a title to the end of the playlist.

Interesting that you're the first one to stumble upon this, I haven't looked at this function in ages. Will have a closer look the next few days.

You should definitely be able to move an item to the end, if not I'd consider that a bug.

Contribution welcome.

You should definitely be able to move an item to the end, if not I'd consider that a bug.

If I understand this correctly, without this change, you can only move an item A above another item, so it can never be moved directly to the bottom of a playlist. You can do it with two steps, though - by first moving item A to the position above the last item in the list (L), and then moving L above A.

You can create a playlist with a few items and move one to the bottom to see the "action" parameters in the Javascript Console.

Anyway, I did pull request #602.