feature: add library search
This commit is contained in:
parent
86790c07e2
commit
312147232a
3 changed files with 105 additions and 55 deletions
|
|
@ -37,32 +37,45 @@ class TestYTMusic(unittest.TestCase):
|
|||
|
||||
def test_search(self):
|
||||
query = "edm playlist"
|
||||
self.assertRaises(Exception, self.yt_auth.search, query, "song")
|
||||
self.assertRaises(Exception, self.yt_auth.search, query, filter="song")
|
||||
self.assertRaises(Exception, self.yt_auth.search, query, scope="upload")
|
||||
results = self.yt.search(query)
|
||||
self.assertGreater(len(results), 10)
|
||||
results = self.yt_auth.search('Martin Stig Andersen - Deteriation', ignore_spelling=True)
|
||||
self.assertGreater(len(results), 0)
|
||||
results = self.yt_auth.search(query, 'songs')
|
||||
results = self.yt_auth.search(query, filter='songs')
|
||||
self.assertGreater(len(results), 10)
|
||||
results = self.yt_auth.search(query, 'videos')
|
||||
results = self.yt_auth.search(query, filter='videos')
|
||||
self.assertGreater(len(results), 10)
|
||||
results = self.yt_auth.search(query, 'albums', limit=40)
|
||||
results = self.yt_auth.search(query, filter='albums', limit=40)
|
||||
self.assertGreater(len(results), 20)
|
||||
results = self.yt_auth.search('project-2', 'artists', ignore_spelling=True)
|
||||
results = self.yt_auth.search('project-2', filter='artists', ignore_spelling=True)
|
||||
self.assertGreater(len(results), 0)
|
||||
results = self.yt_auth.search("classical music", 'playlists')
|
||||
results = self.yt_auth.search("classical music", filter='playlists')
|
||||
self.assertGreater(len(results), 5)
|
||||
results = self.yt_auth.search("clasical music", 'playlists', ignore_spelling=True)
|
||||
results = self.yt_auth.search("clasical music", filter='playlists', ignore_spelling=True)
|
||||
self.assertGreater(len(results), 5)
|
||||
results = self.yt_auth.search("clasic rock", 'community_playlists', ignore_spelling=True)
|
||||
results = self.yt_auth.search("clasic rock", filter='community_playlists', ignore_spelling=True)
|
||||
self.assertGreater(len(results), 5)
|
||||
results = self.yt_auth.search("hip hop", 'featured_playlists')
|
||||
results = self.yt_auth.search("hip hop", filter='featured_playlists')
|
||||
self.assertGreater(len(results), 5)
|
||||
|
||||
def test_search_uploads(self):
|
||||
results = self.yt_auth.search('audiomachine', 'uploads', limit=40)
|
||||
results = self.yt_auth.search('audiomachine', scope='uploads', limit=40)
|
||||
self.assertGreater(len(results), 20)
|
||||
|
||||
def test_search_library(self):
|
||||
results = self.yt_auth.search('garrix', scope='library')
|
||||
self.assertGreater(len(results), 5)
|
||||
results = self.yt_auth.search('bergersen', filter='songs', scope='library', limit=40)
|
||||
self.assertGreater(len(results), 10)
|
||||
results = self.yt_auth.search('garrix', filter='albums', scope='library', limit=40)
|
||||
self.assertGreaterEqual(len(results), 4)
|
||||
results = self.yt_auth.search('garrix', filter='artists', scope='library', limit=40)
|
||||
self.assertGreaterEqual(len(results), 1)
|
||||
results = self.yt_auth.search('garrix', filter='playlists', scope='library')
|
||||
self.assertGreaterEqual(len(results), 1)
|
||||
|
||||
def test_get_artist(self):
|
||||
results = self.yt.get_artist("MPLAUCmMUZbaYdNH0bEd1PAlAqsA")
|
||||
self.assertEqual(len(results), 14)
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
from ytmusicapi.helpers import *
|
||||
from ytmusicapi.parsers.browsing import *
|
||||
from ytmusicapi.parsers.search_params import *
|
||||
from ytmusicapi.parsers.albums import *
|
||||
from ytmusicapi.parsers.playlists import *
|
||||
from ytmusicapi.parsers.library import parse_albums
|
||||
|
|
@ -9,6 +10,7 @@ class BrowsingMixin:
|
|||
def search(self,
|
||||
query: str,
|
||||
filter: str = None,
|
||||
scope: str = None,
|
||||
limit: int = 20,
|
||||
ignore_spelling: bool = False) -> List[Dict]:
|
||||
"""
|
||||
|
|
@ -18,6 +20,8 @@ class BrowsingMixin:
|
|||
:param query: Query string, i.e. 'Oasis Wonderwall'
|
||||
:param filter: Filter for item types. Allowed values: ``songs``, ``videos``, ``albums``, ``artists``, ``playlists``, ``community_playlists``, ``featured_playlists``, ``uploads``.
|
||||
Default: Default search, including all types of items.
|
||||
:param scope: Search scope. Allowed values: ``library``, ``uploads``.
|
||||
Default: Search the public YouTube Music catalogue.
|
||||
:param limit: Number of search results to return
|
||||
Default: 20
|
||||
:param ignore_spelling: Whether to ignore YTM spelling suggestions.
|
||||
|
|
@ -111,56 +115,23 @@ class BrowsingMixin:
|
|||
endpoint = 'search'
|
||||
search_results = []
|
||||
filters = [
|
||||
'albums', 'artists', 'playlists', 'community_playlists', 'featured_playlists', 'songs',
|
||||
'videos', 'uploads'
|
||||
'albums', 'artists', 'playlists', 'community_playlists', 'featured_playlists', 'songs', 'videos'
|
||||
]
|
||||
if filter and filter not in filters:
|
||||
raise Exception(
|
||||
"Invalid filter provided. Please use one of the following filters or leave out the parameter: "
|
||||
+ ', '.join(filters))
|
||||
|
||||
params = None
|
||||
if filter:
|
||||
if filter == 'uploads':
|
||||
params = 'agIYAw%3D%3D'
|
||||
|
||||
elif filter == 'playlists':
|
||||
params = 'Eg-KAQwIABAAGAAgACgB'
|
||||
if not ignore_spelling:
|
||||
params += 'MABqChAEEAMQCRAFEAo%3D'
|
||||
else:
|
||||
params += 'MABCAggBagoQBBADEAkQBRAK'
|
||||
|
||||
elif 'playlists' in filter:
|
||||
param1 = 'EgeKAQQoA'
|
||||
if filter == 'featured_playlists':
|
||||
param2 = 'Dg'
|
||||
else: # community_playlists
|
||||
param2 = 'EA'
|
||||
|
||||
if not ignore_spelling:
|
||||
param3 = 'BagwQDhAKEAMQBBAJEAU%3D'
|
||||
else:
|
||||
param3 = 'BQgIIAWoMEA4QChADEAQQCRAF'
|
||||
|
||||
filter = 'playlists' # reset to playlists for parser
|
||||
|
||||
else:
|
||||
param1 = 'EgWKAQI'
|
||||
filter_params = {'songs': 'I', 'videos': 'Q', 'albums': 'Y', 'artists': 'g'}
|
||||
param2 = filter_params[filter]
|
||||
if not ignore_spelling:
|
||||
param3 = 'AWoMEA4QChADEAQQCRAF'
|
||||
else:
|
||||
param3 = 'AUICCAFqDBAOEAoQAxAEEAkQBQ%3D%3D'
|
||||
|
||||
params = params if params else param1 + param2 + param3
|
||||
|
||||
elif ignore_spelling:
|
||||
params = 'EhGKAQ4IARABGAEgASgAOAFAAUICCAE%3D'
|
||||
scopes = ['library', 'uploads']
|
||||
if scope and scope not in scopes:
|
||||
raise Exception(
|
||||
"Invalid scope provided. Please use one of the following scopes or leave out the parameter: "
|
||||
+ ', '.join(scopes))
|
||||
|
||||
params = get_search_params(filter, scope, ignore_spelling)
|
||||
if params:
|
||||
body['params'] = params
|
||||
|
||||
response = self._send_request(endpoint, body)
|
||||
|
||||
# no results
|
||||
|
|
@ -168,8 +139,8 @@ class BrowsingMixin:
|
|||
return search_results
|
||||
|
||||
if 'tabbedSearchResultsRenderer' in response['contents']:
|
||||
results = response['contents']['tabbedSearchResultsRenderer']['tabs'][int(
|
||||
filter == "uploads")]['tabRenderer']['content']
|
||||
tab_index = 0 if not scope or filter else scopes.index(scope) + 1
|
||||
results = response['contents']['tabbedSearchResultsRenderer']['tabs'][tab_index]['tabRenderer']['content']
|
||||
else:
|
||||
results = response['contents']
|
||||
|
||||
|
|
@ -179,12 +150,22 @@ class BrowsingMixin:
|
|||
if len(results) == 1 and 'itemSectionRenderer' in results:
|
||||
return search_results
|
||||
|
||||
# set filter for parser
|
||||
if filter and 'playlists' in filter:
|
||||
filter = 'playlists'
|
||||
elif scope == scopes[1]:
|
||||
filter = scopes[1]
|
||||
|
||||
for res in results:
|
||||
if 'musicShelfRenderer' in res:
|
||||
results = res['musicShelfRenderer']['contents']
|
||||
original_filter = filter
|
||||
if not filter:
|
||||
filter = nav(res, MUSIC_SHELF + TITLE_TEXT, True)
|
||||
|
||||
type = filter[:-1] if filter else None
|
||||
type = filter[:-1].lower() if filter else None
|
||||
search_results.extend(self.parser.parse_search_results(results, type))
|
||||
filter = original_filter
|
||||
|
||||
if 'continuations' in res['musicShelfRenderer']:
|
||||
request_func = lambda additionalParams: self._send_request(
|
||||
|
|
@ -465,7 +446,7 @@ class BrowsingMixin:
|
|||
{
|
||||
"index": "1",
|
||||
"title": "WIEE (feat. Mesto)",
|
||||
"artists": "Martin Garrix",
|
||||
"artist": "Martin Garrix",
|
||||
"videoId": "8xMNeXI9wxI",
|
||||
"lengthMs": "203406",
|
||||
"likeStatus": "INDIFFERENT"
|
||||
|
|
|
|||
56
ytmusicapi/parsers/search_params.py
Normal file
56
ytmusicapi/parsers/search_params.py
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
FILTERED_PARAM1 = 'EgWKAQI'
|
||||
|
||||
|
||||
def get_search_params(filter, scope, ignore_spelling):
|
||||
params = None
|
||||
if filter is None and scope is None and not ignore_spelling:
|
||||
return params
|
||||
|
||||
if scope == 'uploads':
|
||||
params = 'agIYAw%3D%3D'
|
||||
|
||||
if scope == 'library':
|
||||
if filter:
|
||||
param1 = FILTERED_PARAM1
|
||||
param2 = _get_param2(filter)
|
||||
param3 = 'AWoKEAUQCRADEAoYBA%3D%3D'
|
||||
else:
|
||||
params = 'agIYBA%3D%3D'
|
||||
|
||||
if scope is None and filter:
|
||||
if filter == 'playlists':
|
||||
params = 'Eg-KAQwIABAAGAAgACgB'
|
||||
if not ignore_spelling:
|
||||
params += 'MABqChAEEAMQCRAFEAo%3D'
|
||||
else:
|
||||
params += 'MABCAggBagoQBBADEAkQBRAK'
|
||||
|
||||
elif 'playlists' in filter:
|
||||
param1 = 'EgeKAQQoA'
|
||||
if filter == 'featured_playlists':
|
||||
param2 = 'Dg'
|
||||
else: # community_playlists
|
||||
param2 = 'EA'
|
||||
|
||||
if not ignore_spelling:
|
||||
param3 = 'BagwQDhAKEAMQBBAJEAU%3D'
|
||||
else:
|
||||
param3 = 'BQgIIAWoMEA4QChADEAQQCRAF'
|
||||
|
||||
else:
|
||||
param1 = FILTERED_PARAM1
|
||||
param2 = _get_param2(filter)
|
||||
if not ignore_spelling:
|
||||
param3 = 'AWoMEA4QChADEAQQCRAF'
|
||||
else:
|
||||
param3 = 'AUICCAFqDBAOEAoQAxAEEAkQBQ%3D%3D'
|
||||
|
||||
if not scope and not filter and ignore_spelling:
|
||||
params = 'EhGKAQ4IARABGAEgASgAOAFAAUICCAE%3D'
|
||||
|
||||
return params if params else param1 + param2 + param3
|
||||
|
||||
|
||||
def _get_param2(filter):
|
||||
filter_params = {'songs': 'I', 'videos': 'Q', 'albums': 'Y', 'artists': 'g', 'playlists': 'o'}
|
||||
return filter_params[filter]
|
||||
Loading…
Reference in a new issue