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,7 +81,6 @@ 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
|
||||||
|
|
@ -103,27 +101,20 @@ class BrowsingMixin:
|
||||||
if 'musicShelfRenderer' in res:
|
if 'musicShelfRenderer' in res:
|
||||||
results = res['musicShelfRenderer']['contents']
|
results = res['musicShelfRenderer']['contents']
|
||||||
|
|
||||||
for result in results:
|
|
||||||
data = result['musicResponsiveListItemRenderer']
|
|
||||||
type = filter[:-1] if filter else None
|
type = filter[:-1] if filter else None
|
||||||
search_result = self.parser.parse_search_result(data, type)
|
search_results = self.parser.parse_search_results(results, 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,7 +8,10 @@ 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_results = []
|
||||||
|
for result in results:
|
||||||
|
data = result['musicResponsiveListItemRenderer']
|
||||||
search_result = {}
|
search_result = {}
|
||||||
default = not resultType
|
default = not resultType
|
||||||
if not resultType:
|
if not resultType:
|
||||||
|
|
@ -28,47 +31,8 @@ class Parser:
|
||||||
data, PLAY_BUTTON)['playNavigationEndpoint']['watchEndpoint']['videoId']
|
data, PLAY_BUTTON)['playNavigationEndpoint']['watchEndpoint']['videoId']
|
||||||
search_result['title'] = get_item_text(data, 0)
|
search_result['title'] = get_item_text(data, 0)
|
||||||
|
|
||||||
if resultType in ['artist', 'album', 'playlist']:
|
if resultType in ['``artist', 'album', 'playlist']:
|
||||||
browse_id = nav(data, NAVIGATION_BROWSE_ID, True)
|
search_result['browseId'] = nav(data, NAVIGATION_BROWSE_ID)
|
||||||
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']:
|
if resultType in ['artist']:
|
||||||
search_result['artist'] = get_item_text(data, 0)
|
search_result['artist'] = get_item_text(data, 0)
|
||||||
|
|
@ -76,9 +40,7 @@ class Parser:
|
||||||
elif resultType in ['album']:
|
elif resultType in ['album']:
|
||||||
search_result['title'] = get_item_text(data, 0)
|
search_result['title'] = get_item_text(data, 0)
|
||||||
search_result['type'] = get_item_text(data, 1)
|
search_result['type'] = get_item_text(data, 1)
|
||||||
if 'artist' not in search_result:
|
|
||||||
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']:
|
elif resultType in ['playlist']:
|
||||||
|
|
@ -104,7 +66,8 @@ class Parser:
|
||||||
browse_id = nav(data, NAVIGATION_BROWSE_ID, True)
|
browse_id = nav(data, NAVIGATION_BROWSE_ID, True)
|
||||||
if not browse_id: # song result
|
if not browse_id: # song result
|
||||||
flex_items = [
|
flex_items = [
|
||||||
nav(get_flex_column_item(data, i), ['text', 'runs'], True) for i in range(2)
|
nav(get_flex_column_item(data, i), ['text', 'runs'], True)
|
||||||
|
for i in range(2)
|
||||||
]
|
]
|
||||||
if flex_items[0]:
|
if flex_items[0]:
|
||||||
search_result['videoId'] = nav(flex_items[0][0], NAVIGATION_VIDEO_ID)
|
search_result['videoId'] = nav(flex_items[0][0], NAVIGATION_VIDEO_ID)
|
||||||
|
|
@ -137,7 +100,9 @@ class Parser:
|
||||||
search_result['resultType'] = 'album'
|
search_result['resultType'] = 'album'
|
||||||
|
|
||||||
search_result['thumbnails'] = nav(data, THUMBNAILS)
|
search_result['thumbnails'] = nav(data, THUMBNAILS)
|
||||||
return search_result
|
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