respond to review of PR
This commit is contained in:
parent
a0506ebf75
commit
5654928ed2
2 changed files with 70 additions and 8 deletions
|
|
@ -4,19 +4,22 @@ 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 *
|
||||
|
||||
|
||||
class BrowsingMixin:
|
||||
def search(self, query: str, filter: str = None) -> List[Dict]:
|
||||
def search(self, query: str, filter: str = None, limit: int = 20) -> List[Dict]:
|
||||
"""
|
||||
Search YouTube music
|
||||
Returns up to 20 results within the provided category.
|
||||
Returns results within the provided category.
|
||||
|
||||
:param query: Query string, i.e. 'Oasis Wonderwall'
|
||||
:param filter: Filter for item types. Allowed values:
|
||||
'songs', 'videos', 'albums', 'artists', 'playlists', 'uploads'.
|
||||
Default: Default search, including all types of items.
|
||||
:param limit: Number of search results to return
|
||||
Default: 20
|
||||
: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
|
||||
|
|
@ -106,10 +109,21 @@ class BrowsingMixin:
|
|||
search_result = self.parser.parse_search_result(data, type)
|
||||
search_results.append(search_result)
|
||||
|
||||
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)
|
||||
|
||||
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
|
||||
return search_results[:limit]
|
||||
|
||||
def get_artist(self, channelId: str) -> Dict:
|
||||
"""
|
||||
|
|
@ -436,7 +450,9 @@ class BrowsingMixin:
|
|||
# in case the song is unavailable, there is no videoId
|
||||
track['videoId'] = item['payload']['musicTrack']['videoId'] if 'videoId' in item[
|
||||
'payload']['musicTrack'] else None
|
||||
track['lengthMs'] = item['payload']['musicTrack']['lengthMs']
|
||||
# very occasionally lengthMs is not returned
|
||||
track['lengthMs'] = item['payload']['musicTrack'][
|
||||
'lengthMs'] if 'lengthMs' in item['payload']['musicTrack'] else None
|
||||
track['likeStatus'] = likes[item['entityKey']]
|
||||
album['tracks'].append(track)
|
||||
|
||||
|
|
|
|||
|
|
@ -29,7 +29,46 @@ class Parser:
|
|||
search_result['title'] = get_item_text(data, 0)
|
||||
|
||||
if resultType in ['artist', 'album', 'playlist']:
|
||||
search_result['browseId'] = nav(data, NAVIGATION_BROWSE_ID)
|
||||
browse_id = nav(data, NAVIGATION_BROWSE_ID, True)
|
||||
if not browse_id: # song result
|
||||
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'
|
||||
|
||||
if resultType in ['artist']:
|
||||
search_result['artist'] = get_item_text(data, 0)
|
||||
|
|
@ -37,7 +76,9 @@ class Parser:
|
|||
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)
|
||||
if 'artist' not in search_result:
|
||||
search_result['artist'] = get_item_text(data, 2)
|
||||
|
||||
search_result['year'] = get_item_text(data, 3)
|
||||
|
||||
elif resultType in ['playlist']:
|
||||
|
|
@ -61,7 +102,7 @@ class Parser:
|
|||
search_result['title'] = get_item_text(data, 0)
|
||||
|
||||
browse_id = nav(data, NAVIGATION_BROWSE_ID, True)
|
||||
if not browse_id: # song result
|
||||
if not browse_id: # song result
|
||||
flex_items = [
|
||||
nav(get_flex_column_item(data, i), ['text', 'runs'], True) for i in range(2)
|
||||
]
|
||||
|
|
@ -96,7 +137,6 @@ class Parser:
|
|||
search_result['resultType'] = 'album'
|
||||
|
||||
search_result['thumbnails'] = nav(data, THUMBNAILS)
|
||||
|
||||
return search_result
|
||||
|
||||
@i18n
|
||||
|
|
@ -128,6 +168,12 @@ 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