Add get_library_songs, get_library_albums, get_library_artists (draft)
This commit is contained in:
parent
e0d4f0af57
commit
f05b7b3be7
3 changed files with 89 additions and 4 deletions
|
|
@ -41,6 +41,22 @@ class TestYTMusic(unittest.TestCase):
|
||||||
# LIBRARY
|
# 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):
|
def test_get_liked_songs(self):
|
||||||
songs = youtube_auth.get_liked_songs(100)
|
songs = youtube_auth.get_liked_songs(100)
|
||||||
self.assertGreater(len(songs), 0)
|
self.assertGreater(len(songs), 0)
|
||||||
|
|
@ -69,10 +85,6 @@ class TestYTMusic(unittest.TestCase):
|
||||||
# PLAYLISTS
|
# PLAYLISTS
|
||||||
###############
|
###############
|
||||||
|
|
||||||
def test_get_playlists(self):
|
|
||||||
playlists = youtube_auth.get_playlists()
|
|
||||||
self.assertGreater(len(playlists), 0)
|
|
||||||
|
|
||||||
def test_get_foreign_playlist(self):
|
def test_get_foreign_playlist(self):
|
||||||
songs = youtube.get_playlist_items("PLw-VjHDlEOgs658kAHR_LAaILBXb-s6Q5")
|
songs = youtube.get_playlist_items("PLw-VjHDlEOgs658kAHR_LAaILBXb-s6Q5")
|
||||||
self.assertEqual(len(songs), 200)
|
self.assertEqual(len(songs), 200)
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ FRAMEWORK_MUTATIONS = ['frameworkUpdates', 'entityBatchUpdate', 'mutations']
|
||||||
TITLE = ['title', 'runs', 0, 'text']
|
TITLE = ['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']
|
||||||
|
|
||||||
def parse_playlist_items(results, owned=False):
|
def parse_playlist_items(results, owned=False):
|
||||||
songs = []
|
songs = []
|
||||||
|
|
|
||||||
|
|
@ -390,6 +390,66 @@ class YTMusic:
|
||||||
|
|
||||||
return playlists
|
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):
|
def get_liked_songs(self, limit=1000):
|
||||||
"""
|
"""
|
||||||
Gets playlist items for the 'Liked Songs' playlist
|
Gets playlist items for the 'Liked Songs' playlist
|
||||||
|
|
@ -683,6 +743,18 @@ class YTMusic:
|
||||||
|
|
||||||
return songs
|
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):
|
def upload_song(self, filepath):
|
||||||
"""
|
"""
|
||||||
Uploads a song to YouTube Music
|
Uploads a song to YouTube Music
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue