ytmusicapi icon indicating copy to clipboard operation
ytmusicapi copied to clipboard

Implement clear errors or sanitize requests with `>` or `<` in the playlist title.

Open SuppliedOrange opened this issue 1 year ago • 4 comments

Is your feature request related to a problem? Please describe.

This is related to a problem. Using > and < in playlists will cause it to error with 400 Bad Request. Originally found in https://github.com/linsomniac/spotify_to_ytmusic/issues/93 I could not find other characters affected by this. The error it returns does not make perfect sense either.

To reproduce:

.create_playlist(title="this > won't work", description="")
# Server returned HTTP 400: Bad Request.

This is what is returned:

{
  "error": {
    "code": 400,
    "message": "Sorry, something went wrong.",
    "errors": [
      {
        "message": "Sorry, something went wrong.",
        "domain": "global",
        "reason": "badRequest"
      }
    ],
    "status": "INVALID_ARGUMENT"
  }
}

Describe the solution you'd like

Might help to sanitize the request or return a more concise error. It's difficult to determine that it's related to this.

SuppliedOrange avatar Aug 19 '24 11:08 SuppliedOrange

Funny enough, this doesn't work on the Web either. Neither do they have a good error message for it. Not sure it's the task of this project to figure something out that YouTube's not doing

sigma67 avatar Aug 19 '24 17:08 sigma67

I'd prefer to raise in that case. Not sure if this is just a temporary issue.

PR welcome

sigma67 avatar Aug 19 '24 17:08 sigma67

Cool, it would help to return the response object within your YTMusicServerError and other exception classes. I'm guessing the INVALID_ARGUMENT status is probably related to this, so it'd be better to check it against that.

Otherwise, would this be an appropriate change to make?

def create_playlist():
        #...
        try:
            response = self._send_request(endpoint, body)

        except YTMusicServerError as error:
            if ("400" in error): # Checks if it's 400 [Bad Request]
                """
                Implemented to counter an undocumented error with no proper response
                Refers to issue https://github.com/sigma67/ytmusicapi/issues/642
                """
                invalid_characters = self._check_has_invalid_playlist_title_characters( title )
                if (invalid_characters): raise YTMusicServerError(error + f"Invalid characters in playlist title: {", ".join(invalid_characters)}")
            
            raise error

def _check_has_invalid_playlist_title_characters(self, title: str) -> list:
        """
        Checks if the playlist title has characters that youtube does not like.

        :param title: Playlist title
        :return: Boolean if playlist title has valid characters or not
        """
        invalid_characters = [">", "<"]
        found_invalid_characters = [char for char in title if char in invalid_characters]
        return found_invalid_characters

SuppliedOrange avatar Aug 20 '24 14:08 SuppliedOrange

Silly me, I closed the issue.

SuppliedOrange avatar Aug 20 '24 14:08 SuppliedOrange