Add limit parameter to search and search_uploads (closes #95)
This commit is contained in:
parent
5654928ed2
commit
0d419fb68f
3 changed files with 119 additions and 169 deletions
|
|
@ -32,7 +32,7 @@ class TestYTMusic(unittest.TestCase):
|
|||
self.assertGreater(len(results), 0)
|
||||
results = youtube_auth.search(query, 'videos')
|
||||
self.assertGreater(len(results), 0)
|
||||
results = youtube_auth.search(query, 'albums')
|
||||
results = youtube_auth.search(query, 'albums', limit=40)
|
||||
self.assertGreater(len(results), 0)
|
||||
results = youtube_auth.search(query, 'artists')
|
||||
self.assertGreater(len(results), 0)
|
||||
|
|
@ -40,8 +40,8 @@ class TestYTMusic(unittest.TestCase):
|
|||
self.assertGreater(len(results), 0)
|
||||
|
||||
def test_search_uploads(self):
|
||||
results = youtube_auth.search('audiomachine', 'uploads')
|
||||
self.assertGreater(len(results), 10)
|
||||
results = youtube_auth.search('audiomachine', 'uploads', limit=40)
|
||||
self.assertGreater(len(results), 20)
|
||||
|
||||
def test_get_artist(self):
|
||||
results = youtube.get_artist("UCmMUZbaYdNH0bEd1PAlAqsA")
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@ import codecs
|
|||
from urllib.parse import parse_qs
|
||||
from typing import List, Dict
|
||||
from ytmusicapi.helpers import *
|
||||
from ytmusicapi.parsers.browsing import parse_continuation_content_list
|
||||
from ytmusicapi.parsers.playlists import *
|
||||
|
||||
|
||||
|
|
@ -82,48 +81,40 @@ class BrowsingMixin:
|
|||
|
||||
response = self._send_request(endpoint, body)
|
||||
|
||||
try:
|
||||
# no results
|
||||
if 'contents' not in response:
|
||||
return search_results
|
||||
# no results
|
||||
if 'contents' not in response:
|
||||
return search_results
|
||||
|
||||
if 'tabbedSearchResultsRenderer' in response['contents']:
|
||||
results = response['contents']['tabbedSearchResultsRenderer']['tabs'][int(
|
||||
filter == "uploads")]['tabRenderer']['content']
|
||||
else:
|
||||
results = response['contents']
|
||||
if 'tabbedSearchResultsRenderer' in response['contents']:
|
||||
results = response['contents']['tabbedSearchResultsRenderer']['tabs'][int(
|
||||
filter == "uploads")]['tabRenderer']['content']
|
||||
else:
|
||||
results = response['contents']
|
||||
|
||||
results = nav(results, SECTION_LIST)
|
||||
results = nav(results, SECTION_LIST)
|
||||
|
||||
# no results
|
||||
if len(results) == 1 and 'itemSectionRenderer' in results:
|
||||
return search_results
|
||||
# no results
|
||||
if len(results) == 1 and 'itemSectionRenderer' in results:
|
||||
return search_results
|
||||
|
||||
for res in results:
|
||||
if 'musicShelfRenderer' in res:
|
||||
results = res['musicShelfRenderer']['contents']
|
||||
for res in results:
|
||||
if 'musicShelfRenderer' in res:
|
||||
results = res['musicShelfRenderer']['contents']
|
||||
|
||||
for result in results:
|
||||
data = result['musicResponsiveListItemRenderer']
|
||||
type = filter[:-1] if filter else None
|
||||
search_result = self.parser.parse_search_result(data, type)
|
||||
search_results.append(search_result)
|
||||
type = filter[:-1] if filter else None
|
||||
search_results = self.parser.parse_search_results(results, type)
|
||||
|
||||
if 'continuations' in res['musicShelfRenderer']:
|
||||
request_func = lambda additionalParams: self._send_request(
|
||||
endpoint, body, additionalParams)
|
||||
if 'continuations' in res['musicShelfRenderer']:
|
||||
request_func = lambda additionalParams: self._send_request(
|
||||
endpoint, body, additionalParams)
|
||||
|
||||
parse_func = lambda contents: parse_continuation_content_list(
|
||||
contents, self.parser.parse_search_result)
|
||||
parse_func = lambda contents: self.parser.parse_search_results(contents, type)
|
||||
|
||||
search_results.extend(
|
||||
get_continuations(res['musicShelfRenderer'], 'musicShelfContinuation',
|
||||
limit - len(search_results), request_func, parse_func))
|
||||
search_results.extend(
|
||||
get_continuations(res['musicShelfRenderer'], 'musicShelfContinuation',
|
||||
limit - len(search_results), request_func, parse_func))
|
||||
|
||||
except Exception as e:
|
||||
print(str(e))
|
||||
|
||||
return search_results[:limit]
|
||||
return search_results
|
||||
|
||||
def get_artist(self, channelId: str) -> Dict:
|
||||
"""
|
||||
|
|
|
|||
|
|
@ -8,136 +8,101 @@ class Parser:
|
|||
self.lang = language
|
||||
|
||||
@i18n
|
||||
def parse_search_result(self, data, resultType=None):
|
||||
search_result = {}
|
||||
default = not resultType
|
||||
if not resultType:
|
||||
resultType = get_item_text(data, 1).lower()
|
||||
result_types = ['artist', 'playlist', 'song', 'video']
|
||||
result_types_local = [_('artist'), _('playlist'), _('song'), _('video')]
|
||||
# default to album since it's labeled with multiple values ('Single', 'EP', etc.)
|
||||
if resultType not in result_types_local:
|
||||
resultType = 'album'
|
||||
else:
|
||||
resultType = result_types[result_types_local.index(resultType)]
|
||||
def parse_search_results(self, results, resultType=None):
|
||||
search_results = []
|
||||
for result in results:
|
||||
data = result['musicResponsiveListItemRenderer']
|
||||
search_result = {}
|
||||
default = not resultType
|
||||
if not resultType:
|
||||
resultType = get_item_text(data, 1).lower()
|
||||
result_types = ['artist', 'playlist', 'song', 'video']
|
||||
result_types_local = [_('artist'), _('playlist'), _('song'), _('video')]
|
||||
# default to album since it's labeled with multiple values ('Single', 'EP', etc.)
|
||||
if resultType not in result_types_local:
|
||||
resultType = 'album'
|
||||
else:
|
||||
resultType = result_types[result_types_local.index(resultType)]
|
||||
|
||||
search_result['resultType'] = resultType
|
||||
search_result['resultType'] = resultType
|
||||
|
||||
if resultType in ['song', 'video']:
|
||||
search_result['videoId'] = nav(
|
||||
data, PLAY_BUTTON)['playNavigationEndpoint']['watchEndpoint']['videoId']
|
||||
search_result['title'] = get_item_text(data, 0)
|
||||
|
||||
if resultType in ['artist', 'album', 'playlist']:
|
||||
browse_id = nav(data, NAVIGATION_BROWSE_ID, True)
|
||||
if not browse_id: # song result
|
||||
if resultType in ['song', 'video']:
|
||||
search_result['videoId'] = nav(
|
||||
data, PLAY_BUTTON)['playNavigationEndpoint']['watchEndpoint']['videoId']
|
||||
search_result['playlistId'] = nav(
|
||||
data, PLAY_BUTTON)['playNavigationEndpoint']['watchEndpoint']['playlistId']
|
||||
flex_items = [
|
||||
nav(get_flex_column_item(data, i), ['text', 'runs'], True) for i in range(4)
|
||||
]
|
||||
if flex_items[0]:
|
||||
search_result['title'] = {flex_items[0][0]['text']}
|
||||
if flex_items[1]:
|
||||
search_result['artists'] = [{
|
||||
'name': flex_items[1][0]['text'],
|
||||
'id': nav(flex_items[1][0], NAVIGATION_BROWSE_ID)
|
||||
}]
|
||||
if flex_items[2]:
|
||||
search_result['album'] = {
|
||||
'name': flex_items[2][0]['text'],
|
||||
'id': nav(flex_items[2][0], NAVIGATION_BROWSE_ID)
|
||||
}
|
||||
if flex_items[3]:
|
||||
search_result['duration'] = flex_items[3][0]['text']
|
||||
search_result['resultType'] = 'song'
|
||||
else: # artist or album result
|
||||
search_result['browseId'] = browse_id
|
||||
if 'artist' in search_result['browseId']:
|
||||
search_result['resultType'] = 'artist'
|
||||
else:
|
||||
flex_items2 = [
|
||||
nav(get_flex_column_item(data, i), ['text', 'runs'], True)
|
||||
for i in range(4)
|
||||
]
|
||||
if flex_items2[0]:
|
||||
search_result['title'] = flex_items2[0][0]['text']
|
||||
if flex_items2[2]:
|
||||
search_result['artist'] = {'name': flex_items2[2][0]['text'], 'id': None}
|
||||
if flex_items2[3]:
|
||||
search_result['releaseDate'] = flex_items2[3][0]['text']
|
||||
search_result['resultType'] = 'album'
|
||||
search_result['title'] = get_item_text(data, 0)
|
||||
|
||||
if resultType in ['artist']:
|
||||
search_result['artist'] = get_item_text(data, 0)
|
||||
if resultType in ['``artist', 'album', 'playlist']:
|
||||
search_result['browseId'] = nav(data, NAVIGATION_BROWSE_ID)
|
||||
|
||||
elif resultType in ['album']:
|
||||
search_result['title'] = get_item_text(data, 0)
|
||||
search_result['type'] = get_item_text(data, 1)
|
||||
if 'artist' not in search_result:
|
||||
if resultType in ['artist']:
|
||||
search_result['artist'] = get_item_text(data, 0)
|
||||
|
||||
elif resultType in ['album']:
|
||||
search_result['title'] = get_item_text(data, 0)
|
||||
search_result['type'] = get_item_text(data, 1)
|
||||
search_result['artist'] = get_item_text(data, 2)
|
||||
search_result['year'] = get_item_text(data, 3)
|
||||
|
||||
search_result['year'] = get_item_text(data, 3)
|
||||
elif resultType in ['playlist']:
|
||||
search_result['title'] = get_item_text(data, 0)
|
||||
search_result['author'] = get_item_text(data, 1 + default)
|
||||
search_result['itemCount'] = get_item_text(data, 2 + default).split(' ')[0]
|
||||
|
||||
elif resultType in ['playlist']:
|
||||
search_result['title'] = get_item_text(data, 0)
|
||||
search_result['author'] = get_item_text(data, 1 + default)
|
||||
search_result['itemCount'] = get_item_text(data, 2 + default).split(' ')[0]
|
||||
elif resultType in ['song']:
|
||||
search_result['artists'] = parse_song_artists(data, 1 + default)
|
||||
hasAlbum = len(data['flexColumns']) == 4 + default
|
||||
if hasAlbum:
|
||||
search_result['album'] = parse_song_album(data, 2 + default)
|
||||
search_result['duration'] = get_item_text(data, 2 + hasAlbum + default)
|
||||
|
||||
elif resultType in ['song']:
|
||||
search_result['artists'] = parse_song_artists(data, 1 + default)
|
||||
hasAlbum = len(data['flexColumns']) == 4 + default
|
||||
if hasAlbum:
|
||||
search_result['album'] = parse_song_album(data, 2 + default)
|
||||
search_result['duration'] = get_item_text(data, 2 + hasAlbum + default)
|
||||
elif resultType in ['video']:
|
||||
search_result['artist'] = get_item_text(data, 1 + default)
|
||||
search_result['views'] = get_item_text(data, 2 + default).split(' ')[0]
|
||||
search_result['duration'] = get_item_text(data, 3 + default)
|
||||
|
||||
elif resultType in ['video']:
|
||||
search_result['artist'] = get_item_text(data, 1 + default)
|
||||
search_result['views'] = get_item_text(data, 2 + default).split(' ')[0]
|
||||
search_result['duration'] = get_item_text(data, 3 + default)
|
||||
elif resultType in ['upload']:
|
||||
search_result['title'] = get_item_text(data, 0)
|
||||
|
||||
elif resultType in ['upload']:
|
||||
search_result['title'] = get_item_text(data, 0)
|
||||
|
||||
browse_id = nav(data, NAVIGATION_BROWSE_ID, True)
|
||||
if not browse_id: # song result
|
||||
flex_items = [
|
||||
nav(get_flex_column_item(data, i), ['text', 'runs'], True) for i in range(2)
|
||||
]
|
||||
if flex_items[0]:
|
||||
search_result['videoId'] = nav(flex_items[0][0], NAVIGATION_VIDEO_ID)
|
||||
search_result['playlistId'] = nav(flex_items[0][0], NAVIGATION_PLAYLIST_ID)
|
||||
if flex_items[1]:
|
||||
search_result['artist'] = {
|
||||
'name': flex_items[1][0]['text'],
|
||||
'id': nav(flex_items[1][0], NAVIGATION_BROWSE_ID)
|
||||
}
|
||||
search_result['album'] = {
|
||||
'name': flex_items[1][2]['text'],
|
||||
'id': nav(flex_items[1][2], NAVIGATION_BROWSE_ID)
|
||||
}
|
||||
search_result['duration'] = flex_items[1][4]['text']
|
||||
search_result['resultType'] = 'song'
|
||||
|
||||
else: # artist or album result
|
||||
search_result['browseId'] = browse_id
|
||||
if 'artist' in search_result['browseId']:
|
||||
search_result['resultType'] = 'artist'
|
||||
else:
|
||||
flex_item2 = get_flex_column_item(data, 1)
|
||||
runs = [
|
||||
run['text'] for i, run in enumerate(flex_item2['text']['runs'])
|
||||
if i % 2 == 0
|
||||
browse_id = nav(data, NAVIGATION_BROWSE_ID, True)
|
||||
if not browse_id: # song result
|
||||
flex_items = [
|
||||
nav(get_flex_column_item(data, i), ['text', 'runs'], True)
|
||||
for i in range(2)
|
||||
]
|
||||
search_result['artist'] = runs[1]
|
||||
if len(runs) > 2: # date may be missing
|
||||
search_result['releaseDate'] = runs[2]
|
||||
search_result['resultType'] = 'album'
|
||||
if flex_items[0]:
|
||||
search_result['videoId'] = nav(flex_items[0][0], NAVIGATION_VIDEO_ID)
|
||||
search_result['playlistId'] = nav(flex_items[0][0], NAVIGATION_PLAYLIST_ID)
|
||||
if flex_items[1]:
|
||||
search_result['artist'] = {
|
||||
'name': flex_items[1][0]['text'],
|
||||
'id': nav(flex_items[1][0], NAVIGATION_BROWSE_ID)
|
||||
}
|
||||
search_result['album'] = {
|
||||
'name': flex_items[1][2]['text'],
|
||||
'id': nav(flex_items[1][2], NAVIGATION_BROWSE_ID)
|
||||
}
|
||||
search_result['duration'] = flex_items[1][4]['text']
|
||||
search_result['resultType'] = 'song'
|
||||
|
||||
search_result['thumbnails'] = nav(data, THUMBNAILS)
|
||||
return search_result
|
||||
else: # artist or album result
|
||||
search_result['browseId'] = browse_id
|
||||
if 'artist' in search_result['browseId']:
|
||||
search_result['resultType'] = 'artist'
|
||||
else:
|
||||
flex_item2 = get_flex_column_item(data, 1)
|
||||
runs = [
|
||||
run['text'] for i, run in enumerate(flex_item2['text']['runs'])
|
||||
if i % 2 == 0
|
||||
]
|
||||
search_result['artist'] = runs[1]
|
||||
if len(runs) > 2: # date may be missing
|
||||
search_result['releaseDate'] = runs[2]
|
||||
search_result['resultType'] = 'album'
|
||||
|
||||
search_result['thumbnails'] = nav(data, THUMBNAILS)
|
||||
search_results.append(search_result)
|
||||
|
||||
return search_results
|
||||
|
||||
@i18n
|
||||
def parse_artist_contents(self, results: List) -> Dict:
|
||||
|
|
@ -168,12 +133,6 @@ class Parser:
|
|||
return artist
|
||||
|
||||
|
||||
def parse_continuation_content_list(results, parse_func):
|
||||
contents = [parse_func(result['musicResponsiveListItemRenderer']) for result in results]
|
||||
|
||||
return contents
|
||||
|
||||
|
||||
def parse_content_list(results, parse_func):
|
||||
contents = []
|
||||
for result in results:
|
||||
|
|
|
|||
Loading…
Reference in a new issue