Add support for ignoring YTM's spelling suggestions (#91)

This commit is contained in:
Forrest Jones 2020-10-20 17:50:48 -06:00
commit 5c13accd96
2 changed files with 19 additions and 2 deletions

View file

@ -39,6 +39,13 @@ class TestYTMusic(unittest.TestCase):
results = youtube_auth.search(query, 'playlists') results = youtube_auth.search(query, 'playlists')
self.assertGreater(len(results), 0) self.assertGreater(len(results), 0)
def test_search_ignore_spelling(self):
query = "Martin Stig Andersen - Deteriation"
results = youtube_auth.search(query, ignore_spelling=True)
self.assertGreater(len(results), 0)
results = youtube_auth.search(query, 'songs', True)
self.assertGreater(len(results), 0)
def test_search_uploads(self): def test_search_uploads(self):
results = youtube_auth.search('audiomachine', 'uploads') results = youtube_auth.search('audiomachine', 'uploads')
self.assertGreater(len(results), 0) self.assertGreater(len(results), 0)

View file

@ -8,7 +8,7 @@ from ytmusicapi.parsers.playlists import *
class BrowsingMixin: class BrowsingMixin:
def search(self, query: str, filter: str = None) -> List[Dict]: def search(self, query: str, filter: str = None, ignore_spelling: bool = False) -> List[Dict]:
""" """
Search YouTube music Search YouTube music
Returns up to 20 results within the provided category. Returns up to 20 results within the provided category.
@ -17,6 +17,10 @@ class BrowsingMixin:
:param filter: Filter for item types. Allowed values: :param filter: Filter for item types. Allowed values:
'songs', 'videos', 'albums', 'artists', 'playlists', 'uploads'. 'songs', 'videos', 'albums', 'artists', 'playlists', 'uploads'.
Default: Default search, including all types of items. Default: Default search, including all types of items.
:param ignore_spelling: Whether to ignore YTM spelling suggestions.
If True, the exact search term will be searched for, and will not be corrected.
This does not have any effect when the filter is set to 'uploads'.
Default: False, will use YTM's default behavior of autocorrecting the search.
:return: List of results depending on filter. :return: List of results depending on filter.
resultType specifies the type of item (important for default search). resultType specifies the type of item (important for default search).
albums, artists and playlists additionally contain a browseId, corresponding to albums, artists and playlists additionally contain a browseId, corresponding to
@ -55,7 +59,11 @@ class BrowsingMixin:
if filter: if filter:
param1 = 'Eg-KAQwIA' param1 = 'Eg-KAQwIA'
param3 = 'MABqChAEEAMQCRAFEAo%3D'
if not ignore_spelling:
param3 = 'MABqChAEEAMQCRAFEAo%3D'
else:
param3 = 'MABCAggBagoQBBADEAkQBRAK'
if filter == 'uploads': if filter == 'uploads':
params = 'agIYAw%3D%3D' params = 'agIYAw%3D%3D'
@ -76,6 +84,8 @@ class BrowsingMixin:
params = param1 + param2 + param3 params = param1 + param2 + param3
body['params'] = params body['params'] = params
elif ignore_spelling:
body['params'] = "QgIIAQ%3D%3D"
response = self._send_request(endpoint, body) response = self._send_request(endpoint, body)