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):
|
def test_search(self):
|
||||||
query = "edm playlist"
|
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)
|
results = self.yt.search(query)
|
||||||
self.assertGreater(len(results), 10)
|
self.assertGreater(len(results), 10)
|
||||||
results = self.yt_auth.search('Martin Stig Andersen - Deteriation', ignore_spelling=True)
|
results = self.yt_auth.search('Martin Stig Andersen - Deteriation', ignore_spelling=True)
|
||||||
self.assertGreater(len(results), 0)
|
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)
|
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)
|
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)
|
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)
|
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)
|
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)
|
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)
|
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)
|
self.assertGreater(len(results), 5)
|
||||||
|
|
||||||
def test_search_uploads(self):
|
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)
|
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):
|
def test_get_artist(self):
|
||||||
results = self.yt.get_artist("MPLAUCmMUZbaYdNH0bEd1PAlAqsA")
|
results = self.yt.get_artist("MPLAUCmMUZbaYdNH0bEd1PAlAqsA")
|
||||||
self.assertEqual(len(results), 14)
|
self.assertEqual(len(results), 14)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
from ytmusicapi.helpers import *
|
from ytmusicapi.helpers import *
|
||||||
from ytmusicapi.parsers.browsing import *
|
from ytmusicapi.parsers.browsing import *
|
||||||
|
from ytmusicapi.parsers.search_params import *
|
||||||
from ytmusicapi.parsers.albums import *
|
from ytmusicapi.parsers.albums import *
|
||||||
from ytmusicapi.parsers.playlists import *
|
from ytmusicapi.parsers.playlists import *
|
||||||
from ytmusicapi.parsers.library import parse_albums
|
from ytmusicapi.parsers.library import parse_albums
|
||||||
|
|
@ -9,6 +10,7 @@ class BrowsingMixin:
|
||||||
def search(self,
|
def search(self,
|
||||||
query: str,
|
query: str,
|
||||||
filter: str = None,
|
filter: str = None,
|
||||||
|
scope: str = None,
|
||||||
limit: int = 20,
|
limit: int = 20,
|
||||||
ignore_spelling: bool = False) -> List[Dict]:
|
ignore_spelling: bool = False) -> List[Dict]:
|
||||||
"""
|
"""
|
||||||
|
|
@ -18,6 +20,8 @@ class BrowsingMixin:
|
||||||
:param query: Query string, i.e. 'Oasis Wonderwall'
|
: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``.
|
: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.
|
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
|
:param limit: Number of search results to return
|
||||||
Default: 20
|
Default: 20
|
||||||
:param ignore_spelling: Whether to ignore YTM spelling suggestions.
|
:param ignore_spelling: Whether to ignore YTM spelling suggestions.
|
||||||
|
|
@ -111,56 +115,23 @@ class BrowsingMixin:
|
||||||
endpoint = 'search'
|
endpoint = 'search'
|
||||||
search_results = []
|
search_results = []
|
||||||
filters = [
|
filters = [
|
||||||
'albums', 'artists', 'playlists', 'community_playlists', 'featured_playlists', 'songs',
|
'albums', 'artists', 'playlists', 'community_playlists', 'featured_playlists', 'songs', 'videos'
|
||||||
'videos', 'uploads'
|
|
||||||
]
|
]
|
||||||
if filter and filter not in filters:
|
if filter and filter not in filters:
|
||||||
raise Exception(
|
raise Exception(
|
||||||
"Invalid filter provided. Please use one of the following filters or leave out the parameter: "
|
"Invalid filter provided. Please use one of the following filters or leave out the parameter: "
|
||||||
+ ', '.join(filters))
|
+ ', '.join(filters))
|
||||||
|
|
||||||
params = None
|
scopes = ['library', 'uploads']
|
||||||
if filter:
|
if scope and scope not in scopes:
|
||||||
if filter == 'uploads':
|
raise Exception(
|
||||||
params = 'agIYAw%3D%3D'
|
"Invalid scope provided. Please use one of the following scopes or leave out the parameter: "
|
||||||
|
+ ', '.join(scopes))
|
||||||
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'
|
|
||||||
|
|
||||||
|
params = get_search_params(filter, scope, ignore_spelling)
|
||||||
if params:
|
if params:
|
||||||
body['params'] = params
|
body['params'] = params
|
||||||
|
|
||||||
response = self._send_request(endpoint, body)
|
response = self._send_request(endpoint, body)
|
||||||
|
|
||||||
# no results
|
# no results
|
||||||
|
|
@ -168,8 +139,8 @@ class BrowsingMixin:
|
||||||
return search_results
|
return search_results
|
||||||
|
|
||||||
if 'tabbedSearchResultsRenderer' in response['contents']:
|
if 'tabbedSearchResultsRenderer' in response['contents']:
|
||||||
results = response['contents']['tabbedSearchResultsRenderer']['tabs'][int(
|
tab_index = 0 if not scope or filter else scopes.index(scope) + 1
|
||||||
filter == "uploads")]['tabRenderer']['content']
|
results = response['contents']['tabbedSearchResultsRenderer']['tabs'][tab_index]['tabRenderer']['content']
|
||||||
else:
|
else:
|
||||||
results = response['contents']
|
results = response['contents']
|
||||||
|
|
||||||
|
|
@ -179,12 +150,22 @@ class BrowsingMixin:
|
||||||
if len(results) == 1 and 'itemSectionRenderer' in results:
|
if len(results) == 1 and 'itemSectionRenderer' in results:
|
||||||
return search_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:
|
for res in results:
|
||||||
if 'musicShelfRenderer' in res:
|
if 'musicShelfRenderer' in res:
|
||||||
results = res['musicShelfRenderer']['contents']
|
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))
|
search_results.extend(self.parser.parse_search_results(results, type))
|
||||||
|
filter = original_filter
|
||||||
|
|
||||||
if 'continuations' in res['musicShelfRenderer']:
|
if 'continuations' in res['musicShelfRenderer']:
|
||||||
request_func = lambda additionalParams: self._send_request(
|
request_func = lambda additionalParams: self._send_request(
|
||||||
|
|
@ -465,7 +446,7 @@ class BrowsingMixin:
|
||||||
{
|
{
|
||||||
"index": "1",
|
"index": "1",
|
||||||
"title": "WIEE (feat. Mesto)",
|
"title": "WIEE (feat. Mesto)",
|
||||||
"artists": "Martin Garrix",
|
"artist": "Martin Garrix",
|
||||||
"videoId": "8xMNeXI9wxI",
|
"videoId": "8xMNeXI9wxI",
|
||||||
"lengthMs": "203406",
|
"lengthMs": "203406",
|
||||||
"likeStatus": "INDIFFERENT"
|
"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