Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
SuppliedOrange opened this issue Aug 19, 2024 · 4 comments
Labels
documentation Improvements or additions to documentation

Comments

@SuppliedOrange
Copy link

SuppliedOrange commented Aug 19, 2024

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 linsomniac/spotify_to_ytmusic#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.

@sigma67
Copy link
Owner

sigma67 commented Aug 19, 2024

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 sigma67 added the documentation Improvements or additions to documentation label Aug 19, 2024
@sigma67
Copy link
Owner

sigma67 commented Aug 19, 2024

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

PR welcome

@SuppliedOrange
Copy link
Author

SuppliedOrange commented Aug 20, 2024

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
Copy link
Author

Silly me, I closed the issue.

sigma67 added a commit that referenced this issue Oct 5, 2024
@sigma67 sigma67 closed this as completed in 09ca398 Oct 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

2 participants