Fix continuations for some library functions
Update tests, reduce code duplication
This commit is contained in:
parent
abbc13f7af
commit
d97d88d4ef
5 changed files with 50 additions and 88 deletions
|
|
@ -111,7 +111,7 @@ class TestYTMusic(unittest.TestCase):
|
||||||
|
|
||||||
def test_get_watch_playlist(self):
|
def test_get_watch_playlist(self):
|
||||||
playlist = youtube_auth.get_watch_playlist("9mWr4c_ig54", limit=50)
|
playlist = youtube_auth.get_watch_playlist("9mWr4c_ig54", limit=50)
|
||||||
self.assertGreater(len(playlist['tracks']), 50)
|
self.assertGreater(len(playlist['tracks']), 45)
|
||||||
playlist = youtube_auth.get_watch_playlist("UoAf_y9Ok4k") # private track
|
playlist = youtube_auth.get_watch_playlist("UoAf_y9Ok4k") # private track
|
||||||
self.assertGreaterEqual(len(playlist['tracks']), 25)
|
self.assertGreaterEqual(len(playlist['tracks']), 25)
|
||||||
|
|
||||||
|
|
@ -131,7 +131,7 @@ class TestYTMusic(unittest.TestCase):
|
||||||
|
|
||||||
def test_get_library_playlists(self):
|
def test_get_library_playlists(self):
|
||||||
playlists = youtube_brand.get_library_playlists(50)
|
playlists = youtube_brand.get_library_playlists(50)
|
||||||
self.assertGreater(len(playlists), 0)
|
self.assertGreater(len(playlists), 25)
|
||||||
|
|
||||||
def test_get_library_songs(self):
|
def test_get_library_songs(self):
|
||||||
songs = youtube_brand.get_library_songs(100)
|
songs = youtube_brand.get_library_songs(100)
|
||||||
|
|
@ -146,34 +146,34 @@ class TestYTMusic(unittest.TestCase):
|
||||||
self.assertGreaterEqual(len(songs), 25)
|
self.assertGreaterEqual(len(songs), 25)
|
||||||
|
|
||||||
def test_get_library_albums(self):
|
def test_get_library_albums(self):
|
||||||
albums = youtube_brand.get_library_albums(100)
|
albums = youtube_auth.get_library_albums(100)
|
||||||
self.assertGreater(len(albums), 0)
|
self.assertGreater(len(albums), 50)
|
||||||
albums = youtube_brand.get_library_albums(100, order='a_to_z')
|
albums = youtube_brand.get_library_albums(100, order='a_to_z')
|
||||||
self.assertGreater(len(albums), 0)
|
self.assertGreater(len(albums), 50)
|
||||||
albums = youtube_brand.get_library_albums(100, order='z_to_a')
|
albums = youtube_brand.get_library_albums(100, order='z_to_a')
|
||||||
self.assertGreater(len(albums), 0)
|
self.assertGreater(len(albums), 50)
|
||||||
albums = youtube_brand.get_library_albums(100, order='recently_added')
|
albums = youtube_brand.get_library_albums(100, order='recently_added')
|
||||||
self.assertGreater(len(albums), 0)
|
self.assertGreater(len(albums), 50)
|
||||||
|
|
||||||
def test_get_library_artists(self):
|
def test_get_library_artists(self):
|
||||||
artists = youtube_brand.get_library_artists(100)
|
artists = youtube_brand.get_library_artists(50)
|
||||||
self.assertGreater(len(artists), 0)
|
self.assertGreater(len(artists), 40)
|
||||||
artists = youtube_brand.get_library_artists(order='a_to_z')
|
artists = youtube_brand.get_library_artists(order='a_to_z', limit=50)
|
||||||
self.assertGreater(len(artists), 0)
|
self.assertGreater(len(artists), 40)
|
||||||
artists = youtube_brand.get_library_artists(order='z_to_a')
|
artists = youtube_brand.get_library_artists(order='z_to_a')
|
||||||
self.assertGreater(len(artists), 0)
|
self.assertGreater(len(artists), 20)
|
||||||
artists = youtube_brand.get_library_artists(order='recently_added')
|
artists = youtube_brand.get_library_artists(order='recently_added')
|
||||||
self.assertGreater(len(artists), 0)
|
self.assertGreater(len(artists), 20)
|
||||||
|
|
||||||
def test_get_library_subscriptions(self):
|
def test_get_library_subscriptions(self):
|
||||||
artists = youtube_brand.get_library_subscriptions(50)
|
artists = youtube_brand.get_library_subscriptions(50)
|
||||||
self.assertGreater(len(artists), 0)
|
self.assertGreater(len(artists), 40)
|
||||||
artists = youtube_brand.get_library_subscriptions(order='a_to_z')
|
artists = youtube_brand.get_library_subscriptions(order='a_to_z')
|
||||||
self.assertGreater(len(artists), 0)
|
self.assertGreater(len(artists), 20)
|
||||||
artists = youtube_brand.get_library_subscriptions(order='z_to_a')
|
artists = youtube_brand.get_library_subscriptions(order='z_to_a')
|
||||||
self.assertGreater(len(artists), 0)
|
self.assertGreater(len(artists), 20)
|
||||||
artists = youtube_brand.get_library_subscriptions(order='recently_added')
|
artists = youtube_brand.get_library_subscriptions(order='recently_added')
|
||||||
self.assertGreater(len(artists), 0)
|
self.assertGreater(len(artists), 20)
|
||||||
|
|
||||||
def test_get_liked_songs(self):
|
def test_get_liked_songs(self):
|
||||||
songs = youtube_brand.get_liked_songs(200)
|
songs = youtube_brand.get_liked_songs(200)
|
||||||
|
|
@ -270,15 +270,8 @@ class TestYTMusic(unittest.TestCase):
|
||||||
###############
|
###############
|
||||||
|
|
||||||
def test_get_library_upload_songs(self):
|
def test_get_library_upload_songs(self):
|
||||||
results = youtube_auth.get_library_upload_songs(100)
|
results = youtube_auth.get_library_upload_songs(50)
|
||||||
self.assertGreater(len(results), 25)
|
self.assertGreater(len(results), 25)
|
||||||
results = youtube_auth.get_library_upload_songs(100, 'a_to_z')
|
|
||||||
self.assertGreater(len(results), 25)
|
|
||||||
results = youtube_auth.get_library_upload_songs(100, 'z_to_a')
|
|
||||||
self.assertGreater(len(results), 25)
|
|
||||||
results = youtube_auth.get_library_upload_songs(100, 'recently_added')
|
|
||||||
self.assertGreater(len(results), 25)
|
|
||||||
# There is no way to check if the order is correct.
|
|
||||||
|
|
||||||
@unittest.skip("Must not have any uploaded songs to pass")
|
@unittest.skip("Must not have any uploaded songs to pass")
|
||||||
def test_get_library_upload_songs_empty(self):
|
def test_get_library_upload_songs_empty(self):
|
||||||
|
|
@ -287,13 +280,7 @@ class TestYTMusic(unittest.TestCase):
|
||||||
|
|
||||||
def test_get_library_upload_albums(self):
|
def test_get_library_upload_albums(self):
|
||||||
results = youtube_auth.get_library_upload_albums(50)
|
results = youtube_auth.get_library_upload_albums(50)
|
||||||
self.assertGreater(len(results), 25)
|
self.assertGreater(len(results), 40)
|
||||||
results = youtube_auth.get_library_upload_albums(50, order='a_to_z')
|
|
||||||
self.assertGreater(len(results), 25)
|
|
||||||
results = youtube_auth.get_library_upload_albums(50, order='z_to_a')
|
|
||||||
self.assertGreater(len(results), 25)
|
|
||||||
results = youtube_auth.get_library_upload_albums(50, order='recently_added')
|
|
||||||
self.assertGreater(len(results), 25)
|
|
||||||
|
|
||||||
@unittest.skip("Must not have any uploaded albums to pass")
|
@unittest.skip("Must not have any uploaded albums to pass")
|
||||||
def test_get_library_upload_albums_empty(self):
|
def test_get_library_upload_albums_empty(self):
|
||||||
|
|
|
||||||
|
|
@ -28,8 +28,8 @@ class LibraryMixin:
|
||||||
|
|
||||||
results = find_object_by_key(nav(response, SINGLE_COLUMN_TAB + SECTION_LIST),
|
results = find_object_by_key(nav(response, SINGLE_COLUMN_TAB + SECTION_LIST),
|
||||||
'itemSectionRenderer')
|
'itemSectionRenderer')
|
||||||
results = nav(results, ITEM_SECTION + GRID_ITEMS)
|
results = nav(results, ITEM_SECTION + GRID)
|
||||||
playlists = parse_content_list(results[1:], parse_playlist)
|
playlists = parse_content_list(results['items'][1:], parse_playlist)
|
||||||
|
|
||||||
if 'continuations' in results:
|
if 'continuations' in results:
|
||||||
request_func = lambda additionalParams: self._send_request(
|
request_func = lambda additionalParams: self._send_request(
|
||||||
|
|
@ -124,23 +124,9 @@ class LibraryMixin:
|
||||||
|
|
||||||
endpoint = 'browse'
|
endpoint = 'browse'
|
||||||
response = self._send_request(endpoint, body)
|
response = self._send_request(endpoint, body)
|
||||||
results = find_object_by_key(nav(response, SINGLE_COLUMN_TAB + SECTION_LIST),
|
return parse_library_albums(
|
||||||
'itemSectionRenderer')
|
response,
|
||||||
results = nav(results, ITEM_SECTION)
|
lambda additionalParams: self._send_request(endpoint, body, additionalParams), limit)
|
||||||
if 'gridRenderer' not in results:
|
|
||||||
return []
|
|
||||||
results = nav(results, GRID_ITEMS)
|
|
||||||
albums = parse_albums(results)
|
|
||||||
|
|
||||||
if 'continuations' in results:
|
|
||||||
request_func = lambda additionalParams: self._send_request(
|
|
||||||
endpoint, body, additionalParams)
|
|
||||||
parse_func = lambda contents: parse_albums(contents)
|
|
||||||
albums.extend(
|
|
||||||
get_continuations(results, 'gridContinuation', limit - len(albums), request_func,
|
|
||||||
parse_func))
|
|
||||||
|
|
||||||
return albums
|
|
||||||
|
|
||||||
def get_library_artists(self, limit: int = 25, order: str = None) -> List[Dict]:
|
def get_library_artists(self, limit: int = 25, order: str = None) -> List[Dict]:
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -71,24 +71,9 @@ class UploadsMixin:
|
||||||
body["params"] = prepare_order_params(order)
|
body["params"] = prepare_order_params(order)
|
||||||
endpoint = 'browse'
|
endpoint = 'browse'
|
||||||
response = self._send_request(endpoint, body)
|
response = self._send_request(endpoint, body)
|
||||||
results = find_object_by_key(nav(response, SINGLE_COLUMN_TAB + SECTION_LIST),
|
return parse_library_albums(
|
||||||
'itemSectionRenderer')
|
response,
|
||||||
results = nav(results, ITEM_SECTION)
|
lambda additionalParams: self._send_request(endpoint, body, additionalParams), limit)
|
||||||
if 'gridRenderer' not in results:
|
|
||||||
return []
|
|
||||||
else:
|
|
||||||
results = results['gridRenderer']
|
|
||||||
albums = parse_albums(results['items'])
|
|
||||||
|
|
||||||
if 'continuations' in results:
|
|
||||||
request_func = lambda additionalParams: self._send_request(
|
|
||||||
endpoint, body, additionalParams)
|
|
||||||
parse_func = lambda contents: parse_albums(contents)
|
|
||||||
albums.extend(
|
|
||||||
get_continuations(results, 'gridContinuation', limit - len(albums), request_func,
|
|
||||||
parse_func))
|
|
||||||
|
|
||||||
return albums
|
|
||||||
|
|
||||||
def get_library_upload_artists(self, limit: int = 25, order: str = None) -> List[Dict]:
|
def get_library_upload_artists(self, limit: int = 25, order: str = None) -> List[Dict]:
|
||||||
"""
|
"""
|
||||||
|
|
@ -105,24 +90,9 @@ class UploadsMixin:
|
||||||
body["params"] = prepare_order_params(order)
|
body["params"] = prepare_order_params(order)
|
||||||
endpoint = 'browse'
|
endpoint = 'browse'
|
||||||
response = self._send_request(endpoint, body)
|
response = self._send_request(endpoint, body)
|
||||||
results = find_object_by_key(nav(response, SINGLE_COLUMN_TAB + SECTION_LIST),
|
return parse_library_artists(
|
||||||
'itemSectionRenderer')
|
response,
|
||||||
results = nav(results, ITEM_SECTION)
|
lambda additionalParams: self._send_request(endpoint, body, additionalParams), limit)
|
||||||
if 'musicShelfRenderer' not in results:
|
|
||||||
return []
|
|
||||||
else:
|
|
||||||
results = results['musicShelfRenderer']
|
|
||||||
artists = parse_artists(results['contents'], True)
|
|
||||||
|
|
||||||
if 'continuations' in results:
|
|
||||||
request_func = lambda additionalParams: self._send_request(
|
|
||||||
endpoint, body, additionalParams)
|
|
||||||
parse_func = lambda contents: parse_artists(contents, True)
|
|
||||||
artists.extend(
|
|
||||||
get_continuations(results, 'musicShelfContinuation', limit - len(artists),
|
|
||||||
request_func, parse_func))
|
|
||||||
|
|
||||||
return artists
|
|
||||||
|
|
||||||
def get_library_upload_artist(self, browseId: str, limit: int = 25) -> List[Dict]:
|
def get_library_upload_artist(self, browseId: str, limit: int = 25) -> List[Dict]:
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,8 @@ SECTION_LIST = ['sectionListRenderer', 'contents']
|
||||||
SECTION_LIST_ITEM = ['sectionListRenderer', 'contents', 0]
|
SECTION_LIST_ITEM = ['sectionListRenderer', 'contents', 0]
|
||||||
ITEM_SECTION = ['itemSectionRenderer', 'contents', 0]
|
ITEM_SECTION = ['itemSectionRenderer', 'contents', 0]
|
||||||
MUSIC_SHELF = ['musicShelfRenderer']
|
MUSIC_SHELF = ['musicShelfRenderer']
|
||||||
GRID_ITEMS = ['gridRenderer', 'items']
|
GRID = ['gridRenderer']
|
||||||
|
GRID_ITEMS = GRID + ['items']
|
||||||
MENU = ['menu', 'menuRenderer']
|
MENU = ['menu', 'menuRenderer']
|
||||||
MENU_ITEMS = MENU + ['items']
|
MENU_ITEMS = MENU + ['items']
|
||||||
MENU_LIKE_STATUS = MENU + ['topLevelButtons', 0, 'likeButtonRenderer', 'likeStatus']
|
MENU_LIKE_STATUS = MENU + ['topLevelButtons', 0, 'likeButtonRenderer', 'likeStatus']
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,24 @@ def parse_artists(results, uploaded=False):
|
||||||
return artists
|
return artists
|
||||||
|
|
||||||
|
|
||||||
|
def parse_library_albums(response, request_func, limit):
|
||||||
|
results = find_object_by_key(nav(response, SINGLE_COLUMN_TAB + SECTION_LIST),
|
||||||
|
'itemSectionRenderer')
|
||||||
|
results = nav(results, ITEM_SECTION)
|
||||||
|
if 'gridRenderer' not in results:
|
||||||
|
return []
|
||||||
|
results = nav(results, GRID)
|
||||||
|
albums = parse_albums(results['items'])
|
||||||
|
|
||||||
|
if 'continuations' in results:
|
||||||
|
parse_func = lambda contents: parse_albums(contents)
|
||||||
|
albums.extend(
|
||||||
|
get_continuations(results, 'gridContinuation', limit - len(albums), request_func,
|
||||||
|
parse_func))
|
||||||
|
|
||||||
|
return albums
|
||||||
|
|
||||||
|
|
||||||
def parse_albums(results):
|
def parse_albums(results):
|
||||||
albums = []
|
albums = []
|
||||||
for result in results:
|
for result in results:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue