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

KeyError: 'searchSuggestionRenderer' when using get_search_suggestions with 'a' as query #494

Closed
Johna-123 opened this issue Dec 27, 2023 · 6 comments · Fixed by #495
Closed

Comments

@Johna-123
Copy link

Johna-123 commented Dec 27, 2023

Describe the bug
I get an error when using the get_search_suggestions function with these parameters:
query="a"
detailed_runs=True

Here's the error from Flask:

[2023-12-27 14:05:16,222] ERROR in app: Exception on /api/get_search_suggestions [GET]
Traceback (most recent call last):
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 1455, in wsgi_app
    response = self.full_dispatch_request()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 869, in full_dispatch_request
    rv = self.handle_user_exception(e)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask_cors\extension.py", line 176, in wrapped_function
    return cors_after_request(app.make_response(f(*args, **kwargs)))
                                                ^^^^^^^^^^^^^^^^^^
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 867, in full_dispatch_request
    rv = self.dispatch_request()
         ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python311\Lib\site-packages\flask\app.py", line 852, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Owner\Documents\react\ytmusic\python\app.py", line 24, in get_search_suggestions
    return ytmusic.get_search_suggestions(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python311\Lib\site-packages\ytmusicapi\mixins\search.py", line 307, in get_search_suggestions
    search_suggestions = parse_search_suggestions(response, detailed_runs)
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Owner\AppData\Local\Programs\Python\Python311\Lib\site-packages\ytmusicapi\parsers\search.py", line 224, in parse_search_suggestions
    suggestion_content = raw_suggestion['searchSuggestionRenderer']
                         ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
KeyError: 'searchSuggestionRenderer'
127.0.0.1 - - [27/Dec/2023 14:05:18] "GET /api/get_search_suggestions?query=a&detailed_runs=false HTTP/1.1" 500 -

And this is my code:

from flask import Flask, request
from flask_cors import CORS
from ytmusicapi import YTMusic

app = Flask(__name__)
CORS(app)
ytmusic = YTMusic("oauth.json")

@app.route("/api/get_search_suggestions", methods=["GET"])
def get_search_suggestions():
	query = request.args["query"]
	detailed_runs = True if request.args["detailed_runs"] == "true" else False
	return ytmusic.get_search_suggestions(
		query=query,
		detailed_runs=detailed_runs
	)

I sent a GET request from Firefox to the flask server: http://127.0.0.1:5000/api/get_search_suggestions?query=a&detailed_runs=true

To Reproduce
Steps to reproduce the behavior:

  1. Use the get_search_suggestions() function with the following parameters:
    • query="a"
    • detailed_runs=True

Additional context
At first I thought single characters may not be supported for the query, but when setting the query as c, I get a response:

[
  {
    "runs": [
      {
        "bold": true,
        "text": "c"
      },
      {
        "text": "alum scott"
      }
    ],
    "text": "calum scott"
  },
  {
    "runs": [
      {
        "bold": true,
        "text": "c"
      },
      {
        "text": "cm"
      }
    ],
    "text": "ccm"
  },
  {
    "runs": [
      {
        "bold": true,
        "text": "c"
      },
      {
        "text": "itizen tv live"
      }
    ],
    "text": "citizen tv live"
  },
  {
    "runs": [
      {
        "bold": true,
        "text": "c"
      },
      {
        "text": "hristmas music"
      }
    ],
    "text": "christmas music"
  }
]

The same error also occurs for some other single characters as the query.

The error occurs when detailed_runs is set to both True and False.

@sigma67
Copy link
Owner

sigma67 commented Dec 27, 2023

Cannot reproduce. Works fine for me. What does your preview look like?

@Johna-123
Copy link
Author

You mean what I get in the browser? Error 500.

@sigma67
Copy link
Owner

sigma67 commented Dec 27, 2023

So you can't even type this query in your YouTube Music search bar? Seems like something's wrong on your end

@Johna-123
Copy link
Author

No, I can type it in YouTube Music and it's works well.

@Johna-123
Copy link
Author

The problem seems to occur only when authenticated.

@jcbirdwell
Copy link
Contributor

jcbirdwell commented Dec 29, 2023

search suggestions is returning historySuggestionRenderer objects mixed in with the searchSuggestionRenderers when authenticated, based on past searches. replicate in test.py with

def test_auth_search_suggestions(self):
    # add search term to history
    first_pass = self.yt_oauth.search('b')
    self.assertGreater(len(first_pass), 0)
    # get results
    results = self.yt_oauth.get_search_suggestions('b', detailed_runs=True)
    self.assertGreater(len(results), 0)

Adding items to search history seems to be a bit finicky / sometimes takes a second to start getting returned by suggestions., so it may take an extra run or adding a sleep timeout to get the error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants