Add get_library_songs, get_library_albums, get_library_artists (draft)

This commit is contained in:
sigma67 2020-05-01 00:06:01 +02:00
commit f05b7b3be7
No known key found for this signature in database
GPG key ID: 1545A01D4474BAF0
3 changed files with 89 additions and 4 deletions

View file

@ -41,6 +41,22 @@ class TestYTMusic(unittest.TestCase):
# LIBRARY
###############
def test_get_playlists(self):
playlists = youtube_auth.get_playlists()
self.assertGreater(len(playlists), 0)
def test_get_library_songs(self):
songs = youtube_auth.get_library_songs();
self.assertGreater(len(songs), 0)
def test_get_library_albums(self):
albums = youtube_auth.get_library_albums();
self.assertGreater(len(albums), 0)
def test_get_library_artists(self):
artists = youtube_auth.get_library_artists();
self.assertGreater(len(artists), 0)
def test_get_liked_songs(self):
songs = youtube_auth.get_liked_songs(100)
self.assertGreater(len(songs), 0)
@ -69,10 +85,6 @@ class TestYTMusic(unittest.TestCase):
# PLAYLISTS
###############
def test_get_playlists(self):
playlists = youtube_auth.get_playlists()
self.assertGreater(len(playlists), 0)
def test_get_foreign_playlist(self):
songs = youtube.get_playlist_items("PLw-VjHDlEOgs658kAHR_LAaILBXb-s6Q5")
self.assertEqual(len(songs), 200)

View file

@ -11,6 +11,7 @@ FRAMEWORK_MUTATIONS = ['frameworkUpdates', 'entityBatchUpdate', 'mutations']
TITLE = ['title', 'runs', 0, 'text']
SUBTITLE = ['subtitle', 'runs', 0, 'text']
SUBTITLE2 = ['subtitle', 'runs', 2, 'text']
SUBTITLE3 = ['subtitle', 'runs', 4, 'text']
def parse_playlist_items(results, owned=False):
songs = []

View file

@ -390,6 +390,66 @@ class YTMusic:
return playlists
def get_library_songs(self, limit=100):
self.__check_auth()
body = {'browseId': 'FEmusic_liked_videos'}
endpoint = 'browse'
response = self.__send_request(endpoint, body)
results = find_object_by_key(nav(response, SINGLE_COLUMN_TAB + SECTION_LIST), 'itemSectionRenderer')
results = nav(results, ITEM_SECTION)['musicShelfRenderer']
songs = parse_playlist_items(results['contents'][1:])
if 'continuations' in results:
request_func = lambda additionalParams: self.__send_request(endpoint, body, additionalParams)
parse_func = lambda contents: parse_playlist_items(contents)
songs.extend(get_continuations(results, 'musicShelfContinuation', 100, limit, request_func, parse_func))
return songs
def get_library_albums(self, upload=False):
self.__check_auth()
if not upload:
body = {'browseId': 'FEmusic_liked_albums'}
endpoint = 'browse'
response = self.__send_request(endpoint, body)
results = find_object_by_key(nav(response, SINGLE_COLUMN_TAB + SECTION_LIST), 'itemSectionRenderer')
results = nav(results, ITEM_SECTION)['gridRenderer']['items']
albums = []
# skip first item ("New Playlist" button)
for result in results:
data = result['musicTwoRowItemRenderer']
album = {}
album['browseId'] = data['title']['runs'][0]['navigationEndpoint']['browseEndpoint']['browseId']
album['title'] = nav(data, TITLE)
album['type'] = nav(data, SUBTITLE)
album['artist'] = nav(data, SUBTITLE2)
album['year'] = nav(data, SUBTITLE3)
albums.append(album)
#todo continuations
return albums
def get_library_artists(self):
self.__check_auth()
body = {'browseId': 'FEmusic_library_corpus_artists'}
endpoint = 'browse'
response = self.__send_request(endpoint, body)
results = find_object_by_key(nav(response, SINGLE_COLUMN_TAB + SECTION_LIST), 'itemSectionRenderer')
results = nav(results, ITEM_SECTION)['musicShelfRenderer']
artists = []
for result in results['contents']:
data = result['musicResponsiveListItemRenderer']
browseId = data['navigationEndpoint']['browseEndpoint']['browseId']
artist = get_item_text(data, 0)
subscribers = get_item_text(data, 1).split(' ')[0]
artists.append({"browseId": browseId, "artist": artist, "subscribers": subscribers})
#todo continuations
return artists
def get_liked_songs(self, limit=1000):
"""
Gets playlist items for the 'Liked Songs' playlist
@ -683,6 +743,18 @@ class YTMusic:
return songs
def get_uploaded_albums(self):
self.__check_auth()
body = {'browseId': 'FEmusic_library_privately_owned_albums'}
endpoint = 'browse'
response = self.__send_request(endpoint, body)
def get_library_upload_artists(self):
self.__check_auth()
body = {'browseId': 'FEmusic_library_privately_owned_artists'}
endpoint = 'browse'
response = self.__send_request(endpoint, body)
def upload_song(self, filepath):
"""
Uploads a song to YouTube Music