From 5c13accd9621e863956613d213f318089ca6ff95 Mon Sep 17 00:00:00 2001 From: Forrest Jones Date: Tue, 20 Oct 2020 17:50:48 -0600 Subject: [PATCH] Add support for ignoring YTM's spelling suggestions (#91) --- tests/test.py | 7 +++++++ ytmusicapi/mixins/browsing.py | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/tests/test.py b/tests/test.py index 96adff6..2117b69 100644 --- a/tests/test.py +++ b/tests/test.py @@ -39,6 +39,13 @@ class TestYTMusic(unittest.TestCase): results = youtube_auth.search(query, 'playlists') 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): results = youtube_auth.search('audiomachine', 'uploads') self.assertGreater(len(results), 0) diff --git a/ytmusicapi/mixins/browsing.py b/ytmusicapi/mixins/browsing.py index 8d49cd5..899e35c 100644 --- a/ytmusicapi/mixins/browsing.py +++ b/ytmusicapi/mixins/browsing.py @@ -8,7 +8,7 @@ from ytmusicapi.parsers.playlists import * 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 Returns up to 20 results within the provided category. @@ -17,6 +17,10 @@ class BrowsingMixin: :param filter: Filter for item types. Allowed values: 'songs', 'videos', 'albums', 'artists', 'playlists', 'uploads'. 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. resultType specifies the type of item (important for default search). albums, artists and playlists additionally contain a browseId, corresponding to @@ -55,7 +59,11 @@ class BrowsingMixin: if filter: param1 = 'Eg-KAQwIA' - param3 = 'MABqChAEEAMQCRAFEAo%3D' + + if not ignore_spelling: + param3 = 'MABqChAEEAMQCRAFEAo%3D' + else: + param3 = 'MABCAggBagoQBBADEAkQBRAK' if filter == 'uploads': params = 'agIYAw%3D%3D' @@ -76,6 +84,8 @@ class BrowsingMixin: params = param1 + param2 + param3 body['params'] = params + elif ignore_spelling: + body['params'] = "QgIIAQ%3D%3D" response = self._send_request(endpoint, body)