Add get_library_upload_album, get_library_upload_artist (fixes #20)
get_uploaded_songs now returns ids for uploaded song artist/album delete_uploaded_song renamed to delete_upload_entity
This commit is contained in:
parent
b94f5f02e3
commit
c79f13ab92
4 changed files with 152 additions and 33 deletions
|
|
@ -52,5 +52,7 @@ Uploads
|
||||||
.. automethod:: YTMusic.get_library_upload_songs
|
.. automethod:: YTMusic.get_library_upload_songs
|
||||||
.. automethod:: YTMusic.get_library_upload_artists
|
.. automethod:: YTMusic.get_library_upload_artists
|
||||||
.. automethod:: YTMusic.get_library_upload_albums
|
.. automethod:: YTMusic.get_library_upload_albums
|
||||||
|
.. automethod:: YTMusic.get_library_upload_artist
|
||||||
|
.. automethod:: YTMusic.get_library_upload_album
|
||||||
.. automethod:: YTMusic.upload_song
|
.. automethod:: YTMusic.upload_song
|
||||||
.. automethod:: YTMusic.delete_uploaded_song
|
.. automethod:: YTMusic.delete_upload_entity
|
||||||
|
|
@ -177,11 +177,27 @@ class TestYTMusic(unittest.TestCase):
|
||||||
response = youtube_auth.upload_song(config['uploads']['file'])
|
response = youtube_auth.upload_song(config['uploads']['file'])
|
||||||
self.assertEqual(response.status_code, 409)
|
self.assertEqual(response.status_code, 409)
|
||||||
|
|
||||||
def test_delete_uploaded_song(self):
|
def test_delete_upload_entity(self):
|
||||||
results = youtube_auth.get_library_upload_songs()
|
results = youtube_auth.get_library_upload_songs()
|
||||||
response = youtube_auth.delete_uploaded_song(results[0])
|
response = youtube_auth.delete_upload_entity(results[0]['entityId'])
|
||||||
self.assertEqual(response, 'STATUS_SUCCEEDED')
|
self.assertEqual(response, 'STATUS_SUCCEEDED')
|
||||||
|
|
||||||
|
def test_get_library_upload_album(self):
|
||||||
|
songs = youtube_auth.get_library_upload_songs()
|
||||||
|
for song in songs:
|
||||||
|
if song['album']:
|
||||||
|
album = youtube_auth.get_library_upload_album(song['album']['id'])
|
||||||
|
self.assertGreater(len(album['tracks']), 0)
|
||||||
|
break
|
||||||
|
|
||||||
|
def test_get_library_upload_artist(self):
|
||||||
|
songs = youtube_auth.get_library_upload_songs()
|
||||||
|
for song in songs:
|
||||||
|
if song['artist']:
|
||||||
|
tracks = youtube_auth.get_library_upload_artist(song['artist'][0]['id'])
|
||||||
|
self.assertGreater(len(tracks), 0)
|
||||||
|
break
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ 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']
|
||||||
CONTINUATION = ['continuations', 0, 'nextContinuationData', 'continuation']
|
CONTINUATION = ['continuations', 0, 'nextContinuationData', 'continuation']
|
||||||
MENU_ITEMS = ['menu', 'menuRenderer', 'items']
|
MENU_ITEMS = ['menu', 'menuRenderer', 'items']
|
||||||
MENU_SERVICE = ['menuServiceItemRenderer', 'serviceEndpoint']
|
MENU_SERVICE = ['menuServiceItemRenderer', 'serviceEndpoint']
|
||||||
|
|
@ -104,8 +105,8 @@ def parse_playlist_items(results):
|
||||||
|
|
||||||
# if item is not playable, there is no videoId
|
# if item is not playable, there is no videoId
|
||||||
if 'playNavigationEndpoint' in nav(data, PLAY_BUTTON):
|
if 'playNavigationEndpoint' in nav(data, PLAY_BUTTON):
|
||||||
videoId = nav(
|
videoId = nav(data,
|
||||||
data, PLAY_BUTTON)['playNavigationEndpoint']['watchEndpoint']['videoId']
|
PLAY_BUTTON)['playNavigationEndpoint']['watchEndpoint']['videoId']
|
||||||
|
|
||||||
for item in nav(data, MENU_ITEMS):
|
for item in nav(data, MENU_ITEMS):
|
||||||
if 'menuServiceItemRenderer' in item and 'playlistEditEndpoint' in nav(
|
if 'menuServiceItemRenderer' in item and 'playlistEditEndpoint' in nav(
|
||||||
|
|
@ -160,16 +161,12 @@ def parse_uploaded_items(results):
|
||||||
+ MENU_SERVICE)['queueAddEndpoint']['queueTarget']['videoId']
|
+ MENU_SERVICE)['queueAddEndpoint']['queueTarget']['videoId']
|
||||||
|
|
||||||
title = get_item_text(data, 0)
|
title = get_item_text(data, 0)
|
||||||
artist = get_item_text(data, 1)
|
|
||||||
album = get_item_text(data, 2)
|
|
||||||
|
|
||||||
song = {
|
song = {'entityId': entityId, 'videoId': videoId, 'title': title}
|
||||||
'entityId': entityId,
|
if get_flex_column_item(data, 1):
|
||||||
'videoId': videoId,
|
song['artist'] = parse_song_artists(data, 1)
|
||||||
'artist': artist,
|
song['album'] = parse_song_album(data, 2)
|
||||||
'title': title,
|
|
||||||
'album': album
|
|
||||||
}
|
|
||||||
songs.append(song)
|
songs.append(song)
|
||||||
|
|
||||||
return songs
|
return songs
|
||||||
|
|
|
||||||
|
|
@ -268,10 +268,9 @@ class YTMusic:
|
||||||
'text'].split(' ')[0]
|
'text'].split(' ')[0]
|
||||||
artist['songs'] = {}
|
artist['songs'] = {}
|
||||||
if 'musicShelfRenderer' in results[0]: # API sometimes does not return songs
|
if 'musicShelfRenderer' in results[0]: # API sometimes does not return songs
|
||||||
artist['songs']['browseId'] = nav(results[0]['musicShelfRenderer'],
|
musicShelf = nav(results, MUSIC_SHELF)
|
||||||
TITLE + NAVIGATION_BROWSE_ID)
|
artist['songs']['browseId'] = nav(musicShelf, TITLE + NAVIGATION_BROWSE_ID)
|
||||||
artist['songs']['results'] = parse_playlist_items(
|
artist['songs']['results'] = parse_playlist_items(musicShelf['contents'])
|
||||||
results[0]['musicShelfRenderer']['contents'])
|
|
||||||
|
|
||||||
categories = ['albums', 'singles', 'videos']
|
categories = ['albums', 'singles', 'videos']
|
||||||
for category in categories:
|
for category in categories:
|
||||||
|
|
@ -330,11 +329,10 @@ class YTMusic:
|
||||||
endpoint = 'browse'
|
endpoint = 'browse'
|
||||||
response = self.__send_request(endpoint, body)
|
response = self.__send_request(endpoint, body)
|
||||||
artist = nav(response['header']['musicHeaderRenderer'], TITLE_TEXT)
|
artist = nav(response['header']['musicHeaderRenderer'], TITLE_TEXT)
|
||||||
results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST)
|
results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST + MUSIC_SHELF)
|
||||||
albums = []
|
albums = []
|
||||||
musicShelf = results[0]['musicShelfRenderer']
|
release_type = nav(results, TITLE_TEXT).lower()
|
||||||
release_type = nav(musicShelf, TITLE_TEXT).lower()
|
for result in results['contents']:
|
||||||
for result in musicShelf['contents']:
|
|
||||||
data = result['musicResponsiveListItemRenderer']
|
data = result['musicResponsiveListItemRenderer']
|
||||||
browseId = nav(data, NAVIGATION_BROWSE_ID)
|
browseId = nav(data, NAVIGATION_BROWSE_ID)
|
||||||
title = get_item_text(data, 0)
|
title = get_item_text(data, 0)
|
||||||
|
|
@ -362,12 +360,31 @@ class YTMusic:
|
||||||
Each track is in the following format::
|
Each track is in the following format::
|
||||||
|
|
||||||
{
|
{
|
||||||
"index": "1",
|
"title": "Seven",
|
||||||
"title": "WIEE (feat. Mesto)",
|
"trackCount": "7",
|
||||||
"artists": "Martin Garrix",
|
"durationMs": "1439579",
|
||||||
"videoId": "8xMNeXI9wxI",
|
"playlistId": "OLAK5uy_kGnhwT08mQMGw8fArBowdtlew3DpgUt9c",
|
||||||
"lengthMs": "203406"
|
"releaseDate": {
|
||||||
}
|
"year": 2016,
|
||||||
|
"month": 10,
|
||||||
|
"day": 28
|
||||||
|
},
|
||||||
|
"description": "Seven is ...",
|
||||||
|
"artist": [
|
||||||
|
{
|
||||||
|
"name": "Martin Garrix",
|
||||||
|
"id": "UCqJnSdHjKtfsrHi9aI-9d3g"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"tracks": [
|
||||||
|
{
|
||||||
|
"index": "1",
|
||||||
|
"title": "WIEE (feat. Mesto)",
|
||||||
|
"artists": "Martin Garrix",
|
||||||
|
"videoId": "8xMNeXI9wxI",
|
||||||
|
"lengthMs": "203406"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
"""
|
"""
|
||||||
body = prepare_browse_endpoint("ALBUM", browseId)
|
body = prepare_browse_endpoint("ALBUM", browseId)
|
||||||
|
|
@ -992,6 +1009,90 @@ class YTMusic:
|
||||||
|
|
||||||
return artists
|
return artists
|
||||||
|
|
||||||
|
def get_library_upload_artist(self, browseId: str) -> List[Dict]:
|
||||||
|
"""
|
||||||
|
Returns a list of uploaded tracks for the artist.
|
||||||
|
|
||||||
|
:param browseId:
|
||||||
|
:return: List of uploaded songs
|
||||||
|
|
||||||
|
Example List:
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"entityId": "t_po_CICr2crg7OWpchDKwoakAQ",
|
||||||
|
"videoId": "Dtffhy8WJgw",
|
||||||
|
"title": "Hold Me (Original Mix)",
|
||||||
|
"artist": [
|
||||||
|
{
|
||||||
|
"name": "Jakko",
|
||||||
|
"id": "FEmusic_library_privately_owned_artist_detaila_po_CICr2crg7OWpchIFamFra28"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"album": null
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
"""
|
||||||
|
self.__check_auth()
|
||||||
|
body = prepare_browse_endpoint("ARTIST", browseId)
|
||||||
|
endpoint = 'browse'
|
||||||
|
response = self.__send_request(endpoint, body)
|
||||||
|
results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST + MUSIC_SHELF)
|
||||||
|
if len(results['contents']) > 1:
|
||||||
|
results['contents'].pop(0)
|
||||||
|
|
||||||
|
return parse_uploaded_items(results['contents'])
|
||||||
|
|
||||||
|
def get_library_upload_album(self, browseId: str) -> Dict:
|
||||||
|
"""
|
||||||
|
Get information and tracks of an album associated with uploaded tracks
|
||||||
|
|
||||||
|
:param browseId:
|
||||||
|
:returns Dictionary with title, description, artist and tracks.
|
||||||
|
|
||||||
|
{
|
||||||
|
"title": "Hard To Stop - Single",
|
||||||
|
"year": "2013",
|
||||||
|
"trackCount": 1,
|
||||||
|
"duration": "4 minutes, 2 seconds",
|
||||||
|
"tracks": [
|
||||||
|
{
|
||||||
|
"entityId": "t_po_CICr2crg7OWpchDN6tnYBw",
|
||||||
|
"videoId": "VBQVcjJM7ak",
|
||||||
|
"title": "Hard To Stop (Vicetone x Ne-Yo x Daft Punk)"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
"""
|
||||||
|
self.__check_auth()
|
||||||
|
body = prepare_browse_endpoint("ALBUM", browseId)
|
||||||
|
endpoint = 'browse'
|
||||||
|
response = self.__send_request(endpoint, body)
|
||||||
|
header = response['header']['musicDetailHeaderRenderer']
|
||||||
|
album = {'title': nav(header, TITLE_TEXT)}
|
||||||
|
if "description" in header:
|
||||||
|
album["description"] = header["description"]["runs"][0]["text"]
|
||||||
|
run_count = len(header['subtitle']['runs'])
|
||||||
|
if run_count == 3:
|
||||||
|
album['year'] = nav(header, SUBTITLE2)
|
||||||
|
|
||||||
|
if run_count == 5:
|
||||||
|
album['artist'] = {
|
||||||
|
'name': nav(header, SUBTITLE2),
|
||||||
|
'id': nav(header, SUBTITLE2 + NAVIGATION_BROWSE_ID)
|
||||||
|
}
|
||||||
|
album['year'] = nav(header, SUBTITLE3)
|
||||||
|
|
||||||
|
if len(header['secondSubtitle']['runs']) > 1:
|
||||||
|
album['trackCount'] = int(header['secondSubtitle']['runs'][0]['text'].split(' ')[0])
|
||||||
|
album['duration'] = header['secondSubtitle']['runs'][2]['text']
|
||||||
|
else:
|
||||||
|
album['duration'] = header['secondSubtitle']['runs'][0]['text']
|
||||||
|
|
||||||
|
results = nav(response, SINGLE_COLUMN_TAB + SECTION_LIST + MUSIC_SHELF)
|
||||||
|
album['tracks'] = parse_uploaded_items(results['contents'])
|
||||||
|
return album
|
||||||
|
|
||||||
def upload_song(self, filepath: str) -> Union[str, requests.Response]:
|
def upload_song(self, filepath: str) -> Union[str, requests.Response]:
|
||||||
"""
|
"""
|
||||||
Uploads a song to YouTube Music
|
Uploads a song to YouTube Music
|
||||||
|
|
@ -1029,17 +1130,20 @@ class YTMusic:
|
||||||
else:
|
else:
|
||||||
return response
|
return response
|
||||||
|
|
||||||
def delete_uploaded_song(self, uploaded_song: Dict) -> Union[str, Dict]:
|
def delete_upload_entity(self, entityId: str) -> Union[str, Dict]:
|
||||||
"""
|
"""
|
||||||
Deletes a previously uploaded song
|
Deletes a previously uploaded song or album
|
||||||
|
|
||||||
:param uploaded_song: The uploaded song to delete,
|
:param entityId: The entity id of the uploaded song to delete,
|
||||||
e.g. retrieved from :py:func:`get_library_upload_songs`
|
e.g. retrieved from :py:func:`get_library_upload_songs`
|
||||||
:return: Status String or error
|
:return: Status String or error
|
||||||
"""
|
"""
|
||||||
self.__check_auth()
|
self.__check_auth()
|
||||||
endpoint = 'music/delete_privately_owned_entity'
|
endpoint = 'music/delete_privately_owned_entity'
|
||||||
body = {"entityId": uploaded_song['entityId']}
|
if 'FEmusic_library_privately_owned_release_detail' in entityId:
|
||||||
|
entityId = entityId.replace('FEmusic_library_privately_owned_release_detail', '')
|
||||||
|
|
||||||
|
body = {"entityId": entityId}
|
||||||
response = self.__send_request(endpoint, body)
|
response = self.__send_request(endpoint, body)
|
||||||
|
|
||||||
if 'error' not in response:
|
if 'error' not in response:
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue