Feature: add get_watch_playlist, get_watch_playlist_shuffle
This commit is contained in:
parent
d5287e5812
commit
3b46fda393
8 changed files with 154 additions and 24 deletions
|
|
@ -28,6 +28,11 @@ Browsing
|
||||||
.. automethod:: YTMusic.get_album
|
.. automethod:: YTMusic.get_album
|
||||||
.. automethod:: YTMusic.get_song
|
.. automethod:: YTMusic.get_song
|
||||||
|
|
||||||
|
Watch
|
||||||
|
--------
|
||||||
|
.. automethod:: YTMusic.get_watch_playlist
|
||||||
|
.. automethod:: YTMusic.get_watch_playlist_shuffle
|
||||||
|
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
.. automethod:: YTMusic.get_library_playlists
|
.. automethod:: YTMusic.get_library_playlists
|
||||||
|
|
|
||||||
|
|
@ -79,6 +79,24 @@ class TestYTMusic(unittest.TestCase):
|
||||||
song = youtube.get_song("ZrOKjDZOtkA")
|
song = youtube.get_song("ZrOKjDZOtkA")
|
||||||
self.assertGreaterEqual(len(song), 17)
|
self.assertGreaterEqual(len(song), 17)
|
||||||
|
|
||||||
|
###############
|
||||||
|
# WATCH
|
||||||
|
###############
|
||||||
|
|
||||||
|
def test_get_watch_playlist(self):
|
||||||
|
playlist = youtube.get_watch_playlist("4y33h81phKU", limit=50)
|
||||||
|
self.assertGreater(len(playlist), 50)
|
||||||
|
|
||||||
|
def test_get_watch_playlist_shuffle(self):
|
||||||
|
playlist = youtube.get_watch_playlist_shuffle(
|
||||||
|
playlistId="OLAK5uy_lKgoGvlrWhX0EIPavQUXxyPed8Cj38AWc")
|
||||||
|
self.assertEqual(len(playlist), 12)
|
||||||
|
|
||||||
|
def test_get_watch_playlist_shuffle_playlist(self):
|
||||||
|
playlist = youtube.get_watch_playlist_shuffle(
|
||||||
|
playlistId="PL528pVfw3ao0x8jlW3kwdIx1FEMMeeghb", limit=99)
|
||||||
|
self.assertGreaterEqual(len(playlist), 99)
|
||||||
|
|
||||||
###############
|
###############
|
||||||
# LIBRARY
|
# LIBRARY
|
||||||
###############
|
###############
|
||||||
|
|
|
||||||
|
|
@ -35,8 +35,8 @@ class LibraryMixin:
|
||||||
endpoint, body, additionalParams)
|
endpoint, body, additionalParams)
|
||||||
parse_func = lambda contents: parse_content_list(contents, parse_playlist)
|
parse_func = lambda contents: parse_content_list(contents, parse_playlist)
|
||||||
playlists.extend(
|
playlists.extend(
|
||||||
get_continuations(results, 'gridContinuation', 25, limit, request_func,
|
get_continuations(results, 'gridContinuation', limit - len(playlists),
|
||||||
parse_func))
|
request_func, parse_func))
|
||||||
|
|
||||||
return playlists
|
return playlists
|
||||||
|
|
||||||
|
|
@ -65,8 +65,8 @@ class LibraryMixin:
|
||||||
endpoint, body, additionalParams)
|
endpoint, body, additionalParams)
|
||||||
parse_func = lambda contents: parse_playlist_items(contents)
|
parse_func = lambda contents: parse_playlist_items(contents)
|
||||||
songs.extend(
|
songs.extend(
|
||||||
get_continuations(results, 'musicShelfContinuation', 25, limit, request_func,
|
get_continuations(results, 'musicShelfContinuation', limit - len(songs),
|
||||||
parse_func))
|
request_func, parse_func))
|
||||||
|
|
||||||
return songs
|
return songs
|
||||||
|
|
||||||
|
|
@ -95,7 +95,7 @@ class LibraryMixin:
|
||||||
endpoint, body, additionalParams)
|
endpoint, body, additionalParams)
|
||||||
parse_func = lambda contents: parse_albums(contents, False)
|
parse_func = lambda contents: parse_albums(contents, False)
|
||||||
albums.extend(
|
albums.extend(
|
||||||
get_continuations(results, 'gridContinuation', 25, limit, request_func,
|
get_continuations(results, 'gridContinuation', limit - len(albums), request_func,
|
||||||
parse_func))
|
parse_func))
|
||||||
|
|
||||||
return albums
|
return albums
|
||||||
|
|
|
||||||
|
|
@ -103,8 +103,9 @@ class PlaylistsMixin:
|
||||||
endpoint, body, additionalParams)
|
endpoint, body, additionalParams)
|
||||||
parse_func = lambda contents: parse_playlist_items(contents)
|
parse_func = lambda contents: parse_playlist_items(contents)
|
||||||
playlist['tracks'].extend(
|
playlist['tracks'].extend(
|
||||||
get_continuations(results, 'musicPlaylistShelfContinuation', 100, songs_to_get,
|
get_continuations(results, 'musicPlaylistShelfContinuation',
|
||||||
request_func, parse_func))
|
songs_to_get - len(playlist['tracks']), request_func,
|
||||||
|
parse_func))
|
||||||
|
|
||||||
return playlist
|
return playlist
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -41,8 +41,8 @@ class UploadsMixin:
|
||||||
request_func = lambda additionalParams: self._send_request(
|
request_func = lambda additionalParams: self._send_request(
|
||||||
endpoint, body, additionalParams)
|
endpoint, body, additionalParams)
|
||||||
songs.extend(
|
songs.extend(
|
||||||
get_continuations(results, 'musicShelfContinuation', 25, limit, request_func,
|
get_continuations(results, 'musicShelfContinuation', limit - len(songs),
|
||||||
parse_uploaded_items))
|
request_func, parse_uploaded_items))
|
||||||
|
|
||||||
return songs
|
return songs
|
||||||
|
|
||||||
|
|
@ -71,7 +71,7 @@ class UploadsMixin:
|
||||||
endpoint, body, additionalParams)
|
endpoint, body, additionalParams)
|
||||||
parse_func = lambda contents: parse_albums(contents)
|
parse_func = lambda contents: parse_albums(contents)
|
||||||
albums.extend(
|
albums.extend(
|
||||||
get_continuations(results, 'gridContinuation', 25, limit, request_func,
|
get_continuations(results, 'gridContinuation', limit - len(albums), request_func,
|
||||||
parse_func))
|
parse_func))
|
||||||
|
|
||||||
return albums
|
return albums
|
||||||
|
|
@ -97,8 +97,8 @@ class UploadsMixin:
|
||||||
endpoint, body, additionalParams)
|
endpoint, body, additionalParams)
|
||||||
parse_func = lambda contents: parse_artists(contents, True)
|
parse_func = lambda contents: parse_artists(contents, True)
|
||||||
artists.extend(
|
artists.extend(
|
||||||
get_continuations(results, 'musicShelfContinuation', 25, limit, request_func,
|
get_continuations(results, 'musicShelfContinuation', limit - len(artists),
|
||||||
parse_func))
|
request_func, parse_func))
|
||||||
|
|
||||||
return artists
|
return artists
|
||||||
|
|
||||||
|
|
|
||||||
79
ytmusicapi/mixins/watch.py
Normal file
79
ytmusicapi/mixins/watch.py
Normal file
|
|
@ -0,0 +1,79 @@
|
||||||
|
from ytmusicapi.parsers import *
|
||||||
|
|
||||||
|
|
||||||
|
class WatchMixin:
|
||||||
|
def get_watch_playlist(self,
|
||||||
|
videoId: str = None,
|
||||||
|
playlistId: str = None,
|
||||||
|
limit=25,
|
||||||
|
params: str = None) -> List[Dict]:
|
||||||
|
"""
|
||||||
|
Get a watch list of tracks. This watch playlist appears when you press
|
||||||
|
play on a track in YouTube Music.
|
||||||
|
|
||||||
|
:param videoId: videoId of the played video
|
||||||
|
:param playlistId: playlistId of the played playlist or album
|
||||||
|
:param limit: minimum number of watch playlist items to return
|
||||||
|
:param params: only used internally by :py:func:`get_watch_playlist_shuffle`
|
||||||
|
:return: List of watch playlist items.
|
||||||
|
|
||||||
|
Example::
|
||||||
|
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"title": "Interstellar (Main Theme) - Piano Version",
|
||||||
|
"byline": "Patrik Pietschmann • 47M views",
|
||||||
|
"length": "4:47",
|
||||||
|
"videoId": "4y33h81phKU",
|
||||||
|
"playlistId": "RDAMVM4y33h81phKU",
|
||||||
|
"thumbnail": [
|
||||||
|
{
|
||||||
|
"url": "https://i.ytimg.com/vi/4y...",
|
||||||
|
"width": 400,
|
||||||
|
"height": 225
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},...
|
||||||
|
]
|
||||||
|
|
||||||
|
"""
|
||||||
|
body = {'enablePersistentPlaylistPanel': True, 'isAudioOnly': True}
|
||||||
|
if videoId:
|
||||||
|
body['videoId'] = videoId
|
||||||
|
body['watchEndpointMusicSupportedConfigs'] = {
|
||||||
|
'watchEndpointMusicConfig': {
|
||||||
|
'hasPersistentPlaylistPanel': True,
|
||||||
|
'musicVideoType': "MUSIC_VIDEO_TYPE_OMV",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
is_playlist = False
|
||||||
|
if playlistId:
|
||||||
|
body['playlistId'] = playlistId
|
||||||
|
is_playlist = playlistId.startswith('PL')
|
||||||
|
if params:
|
||||||
|
body['params'] = params
|
||||||
|
endpoint = 'next'
|
||||||
|
response = self._send_request(endpoint, body)
|
||||||
|
results = response['contents']['singleColumnMusicWatchNextResultsRenderer']['playlist'][
|
||||||
|
'playlistPanelRenderer']
|
||||||
|
tracks = parse_watch_playlist(results['contents'])
|
||||||
|
|
||||||
|
if 'continuations' in results:
|
||||||
|
request_func = lambda additionalParams: self._send_request(
|
||||||
|
endpoint, body, additionalParams)
|
||||||
|
parse_func = lambda contents: parse_watch_playlist(contents)
|
||||||
|
tracks.extend(
|
||||||
|
get_continuations(results, 'playlistPanelContinuation', limit - len(tracks),
|
||||||
|
request_func, parse_func, '' if is_playlist else 'Radio'))
|
||||||
|
|
||||||
|
return tracks
|
||||||
|
|
||||||
|
def get_watch_playlist_shuffle(self, playlistId: str = None, limit=50) -> List[Dict]:
|
||||||
|
"""
|
||||||
|
Shuffle any playlist
|
||||||
|
|
||||||
|
:param playlistId: Playlist id
|
||||||
|
:param limit: The number of watch playlist items to return
|
||||||
|
:return: A list of watch playlist items (see :py:func:`get_watch_playlist`)
|
||||||
|
"""
|
||||||
|
return self.get_watch_playlist(playlistId=playlistId, limit=limit, params='wAEB8gECGAE%3D')
|
||||||
|
|
@ -7,7 +7,6 @@ SINGLE_COLUMN_TAB = [
|
||||||
SECTION_LIST = ['sectionListRenderer', 'contents']
|
SECTION_LIST = ['sectionListRenderer', 'contents']
|
||||||
ITEM_SECTION = ['itemSectionRenderer', 'contents', 0]
|
ITEM_SECTION = ['itemSectionRenderer', 'contents', 0]
|
||||||
MUSIC_SHELF = [0, 'musicShelfRenderer']
|
MUSIC_SHELF = [0, 'musicShelfRenderer']
|
||||||
CONTINUATION = ['continuations', 0, 'nextContinuationData', 'continuation']
|
|
||||||
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']
|
||||||
|
|
@ -25,9 +24,10 @@ TITLE_TEXT = ['title', 'runs', 0, 'text']
|
||||||
SUBTITLE = ['subtitle', 'runs', 0, 'text']
|
SUBTITLE = ['subtitle', 'runs', 0, 'text']
|
||||||
SUBTITLE2 = ['subtitle', 'runs', 2, 'text']
|
SUBTITLE2 = ['subtitle', 'runs', 2, 'text']
|
||||||
SUBTITLE3 = ['subtitle', 'runs', 4, 'text']
|
SUBTITLE3 = ['subtitle', 'runs', 4, 'text']
|
||||||
THUMBNAILS = ['thumbnail', 'musicThumbnailRenderer', 'thumbnail', 'thumbnails']
|
THUMBNAIL = ['thumbnail', 'thumbnails']
|
||||||
THUMBNAIL_RENDERER = ['thumbnailRenderer', 'musicThumbnailRenderer', 'thumbnail', 'thumbnails']
|
THUMBNAILS = ['thumbnail', 'musicThumbnailRenderer'] + THUMBNAIL
|
||||||
THUMBNAIL_CROPPED = ['thumbnail', 'croppedSquareThumbnailRenderer', 'thumbnail', 'thumbnails']
|
THUMBNAIL_RENDERER = ['thumbnailRenderer', 'musicThumbnailRenderer'] + THUMBNAIL
|
||||||
|
THUMBNAIL_CROPPED = ['thumbnail', 'croppedSquareThumbnailRenderer'] + THUMBNAIL
|
||||||
|
|
||||||
|
|
||||||
class Parser:
|
class Parser:
|
||||||
|
|
@ -110,7 +110,8 @@ class Parser:
|
||||||
data[0],
|
data[0],
|
||||||
CAROUSEL_TITLE)['navigationEndpoint']['browseEndpoint']['params']
|
CAROUSEL_TITLE)['navigationEndpoint']['browseEndpoint']['params']
|
||||||
|
|
||||||
artist[category]['results'] = parse_content_list(data[0]['contents'], categories_parser[i])
|
artist[category]['results'] = parse_content_list(data[0]['contents'],
|
||||||
|
categories_parser[i])
|
||||||
|
|
||||||
return artist
|
return artist
|
||||||
|
|
||||||
|
|
@ -222,6 +223,29 @@ def parse_albums(results, upload=True):
|
||||||
return albums
|
return albums
|
||||||
|
|
||||||
|
|
||||||
|
def parse_watch_playlist(results):
|
||||||
|
tracks = []
|
||||||
|
for result in results:
|
||||||
|
try:
|
||||||
|
if 'playlistPanelVideoRenderer' not in result:
|
||||||
|
continue
|
||||||
|
data = result['playlistPanelVideoRenderer']
|
||||||
|
if 'unplayableText' in data:
|
||||||
|
continue
|
||||||
|
track = {
|
||||||
|
'title': nav(data, TITLE_TEXT),
|
||||||
|
'byline': nav(data, ['longBylineText', 'runs', 0, 'text']),
|
||||||
|
'length': nav(data, ['lengthText', 'runs', 0, 'text']),
|
||||||
|
'videoId': data['videoId'],
|
||||||
|
'playlistId': nav(data, NAVIGATION_PLAYLIST_ID),
|
||||||
|
'thumbnail': nav(data, THUMBNAIL)
|
||||||
|
}
|
||||||
|
tracks.append(track)
|
||||||
|
except Exception:
|
||||||
|
print()
|
||||||
|
return tracks
|
||||||
|
|
||||||
|
|
||||||
def parse_playlist_items(results):
|
def parse_playlist_items(results):
|
||||||
songs = []
|
songs = []
|
||||||
count = 1
|
count = 1
|
||||||
|
|
@ -340,8 +364,8 @@ def parse_library_artists(response, request_func, limit):
|
||||||
if 'continuations' in results:
|
if 'continuations' in results:
|
||||||
parse_func = lambda contents: parse_artists(contents)
|
parse_func = lambda contents: parse_artists(contents)
|
||||||
artists.extend(
|
artists.extend(
|
||||||
get_continuations(results, 'musicShelfContinuation', 25, limit, request_func,
|
get_continuations(results, 'musicShelfContinuation', limit - len(artists),
|
||||||
parse_func))
|
request_func, parse_func))
|
||||||
|
|
||||||
return artists
|
return artists
|
||||||
|
|
||||||
|
|
@ -392,14 +416,16 @@ def get_browse_id(item, index):
|
||||||
return nav(item['text']['runs'][index], NAVIGATION_BROWSE_ID)
|
return nav(item['text']['runs'][index], NAVIGATION_BROWSE_ID)
|
||||||
|
|
||||||
|
|
||||||
def get_continuations(results, continuation_type, per_page, limit, request_func, parse_func):
|
def get_continuations(results, continuation_type, limit, request_func, parse_func, ctoken_path=""):
|
||||||
items = []
|
items = []
|
||||||
while 'continuations' in results and len(items) < limit - per_page:
|
while 'continuations' in results and len(items) < limit:
|
||||||
ctoken = nav(results, CONTINUATION)
|
ctoken = nav(
|
||||||
|
results,
|
||||||
|
['continuations', 0, 'next' + ctoken_path + 'ContinuationData', 'continuation'])
|
||||||
additionalParams = "&ctoken=" + ctoken + "&continuation=" + ctoken
|
additionalParams = "&ctoken=" + ctoken + "&continuation=" + ctoken
|
||||||
response = request_func(additionalParams)
|
response = request_func(additionalParams)
|
||||||
results = response['continuationContents'][continuation_type]
|
results = response['continuationContents'][continuation_type]
|
||||||
continuation_contents = 'contents' if 'Shelf' in continuation_type else 'items'
|
continuation_contents = 'contents' if 'contents' in results else 'items'
|
||||||
items.extend(parse_func(results[continuation_contents]))
|
items.extend(parse_func(results[continuation_contents]))
|
||||||
|
|
||||||
return items
|
return items
|
||||||
|
|
|
||||||
|
|
@ -8,6 +8,7 @@ from ytmusicapi.helpers import *
|
||||||
from ytmusicapi.parsers import *
|
from ytmusicapi.parsers import *
|
||||||
from ytmusicapi.setup import setup
|
from ytmusicapi.setup import setup
|
||||||
from ytmusicapi.mixins.browsing import BrowsingMixin
|
from ytmusicapi.mixins.browsing import BrowsingMixin
|
||||||
|
from ytmusicapi.mixins.watch import WatchMixin
|
||||||
from ytmusicapi.mixins.library import LibraryMixin
|
from ytmusicapi.mixins.library import LibraryMixin
|
||||||
from ytmusicapi.mixins.playlists import PlaylistsMixin
|
from ytmusicapi.mixins.playlists import PlaylistsMixin
|
||||||
from ytmusicapi.mixins.uploads import UploadsMixin
|
from ytmusicapi.mixins.uploads import UploadsMixin
|
||||||
|
|
@ -16,7 +17,7 @@ params = '?alt=json&key=AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30'
|
||||||
base_url = 'https://music.youtube.com/youtubei/v1/'
|
base_url = 'https://music.youtube.com/youtubei/v1/'
|
||||||
|
|
||||||
|
|
||||||
class YTMusic(BrowsingMixin, LibraryMixin, PlaylistsMixin, UploadsMixin):
|
class YTMusic(BrowsingMixin, WatchMixin, LibraryMixin, PlaylistsMixin, UploadsMixin):
|
||||||
"""
|
"""
|
||||||
Allows automated interactions with YouTube Music by emulating the YouTube web client's requests.
|
Allows automated interactions with YouTube Music by emulating the YouTube web client's requests.
|
||||||
Permits both authenticated and non-authenticated requests.
|
Permits both authenticated and non-authenticated requests.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue