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