Update playlist item parser to work with unavailable songs
This commit is contained in:
parent
d666022447
commit
c9dd0192c7
3 changed files with 23 additions and 12 deletions
|
|
@ -3,23 +3,28 @@ import re
|
||||||
# Static helper methods
|
# Static helper methods
|
||||||
|
|
||||||
|
|
||||||
def parse_songs(results):
|
def parse_playlist_items(results, owned=False):
|
||||||
songs = []
|
songs = []
|
||||||
count = 1
|
count = 1
|
||||||
for result in results:
|
for result in results:
|
||||||
count += 1
|
count += 1
|
||||||
if not 'musicResponsiveListItemRenderer' in result:
|
if 'musicResponsiveListItemRenderer' not in result:
|
||||||
continue
|
continue
|
||||||
data = result['musicResponsiveListItemRenderer']
|
data = result['musicResponsiveListItemRenderer']
|
||||||
try:
|
|
||||||
# if item is not playable, there is no videoId
|
|
||||||
if 'playNavigationEndpoint' in data['overlay']['musicItemThumbnailOverlayRenderer']['content']['musicPlayButtonRenderer']:
|
|
||||||
videoId = data['overlay']['musicItemThumbnailOverlayRenderer']['content']['musicPlayButtonRenderer']['playNavigationEndpoint']['watchEndpoint']['videoId']
|
|
||||||
else:
|
|
||||||
videoId = None
|
videoId = None
|
||||||
|
|
||||||
|
try:
|
||||||
# if playlist is not owned, the playlist item can't be interacted with
|
# if playlist is not owned, the playlist item can't be interacted with
|
||||||
setVideoId = data['playlistItemData']['playlistSetVideoId'] if 'playlistItemData' in data else None
|
if owned:
|
||||||
|
for item in data['menu']['menuRenderer']['items']:
|
||||||
|
if 'menuServiceItemRenderer' in item and 'playlistEditEndpoint' in item['menuServiceItemRenderer']['serviceEndpoint']:
|
||||||
|
setVideoId = item['menuServiceItemRenderer']['serviceEndpoint']['playlistEditEndpoint']['actions'][0]['setVideoId']
|
||||||
|
videoId = item['menuServiceItemRenderer']['serviceEndpoint']['playlistEditEndpoint']['actions'][0]['removedVideoId']
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
videoId = data['overlay']['musicItemThumbnailOverlayRenderer']['content']['musicPlayButtonRenderer']['playNavigationEndpoint']['watchEndpoint']['videoId']
|
||||||
|
setVideoId = None
|
||||||
|
|
||||||
|
|
||||||
artist = data['flexColumns'][1]['musicResponsiveListItemFlexColumnRenderer']['text']['runs'][0]['text']
|
artist = data['flexColumns'][1]['musicResponsiveListItemFlexColumnRenderer']['text']['runs'][0]['text']
|
||||||
title = data['flexColumns'][0]['musicResponsiveListItemFlexColumnRenderer']['text']['runs'][0]['text']
|
title = data['flexColumns'][0]['musicResponsiveListItemFlexColumnRenderer']['text']['runs'][0]['text']
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,10 @@ def get_owned_playlist():
|
||||||
assert (len(songs) == 64)
|
assert (len(songs) == 64)
|
||||||
|
|
||||||
|
|
||||||
|
def get_history():
|
||||||
|
songs = youtube_auth.get_history()
|
||||||
|
assert (len(songs) > 0)
|
||||||
|
|
||||||
def search():
|
def search():
|
||||||
results = youtube_auth.search("Oasis Wonderwall")
|
results = youtube_auth.search("Oasis Wonderwall")
|
||||||
print(results)
|
print(results)
|
||||||
|
|
@ -40,5 +44,6 @@ def end2end():
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
end2end()
|
end2end()
|
||||||
|
#get_history()
|
||||||
#search()
|
#search()
|
||||||
#get_owned_playlist()
|
#get_owned_playlist()
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ import requests
|
||||||
import json
|
import json
|
||||||
import pkg_resources
|
import pkg_resources
|
||||||
from ytmusicapi.helpers import \
|
from ytmusicapi.helpers import \
|
||||||
parse_songs, parse_search_result, html_to_txt
|
parse_playlist_items, parse_search_result, html_to_txt
|
||||||
|
|
||||||
params = '?alt=json&key=AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30'
|
params = '?alt=json&key=AIzaSyC9XL3ZjWddXya6X74dJoCTL-WEYFDNX30'
|
||||||
base_url = 'https://music.youtube.com/youtubei/v1/'
|
base_url = 'https://music.youtube.com/youtubei/v1/'
|
||||||
|
|
@ -176,7 +176,7 @@ class YTMusic:
|
||||||
songs = []
|
songs = []
|
||||||
for content in results:
|
for content in results:
|
||||||
data = content['musicShelfRenderer']['contents']
|
data = content['musicShelfRenderer']['contents']
|
||||||
songlist = parse_songs(data)
|
songlist = parse_playlist_items(data)
|
||||||
for song in songlist:
|
for song in songlist:
|
||||||
song['played'] = content['musicShelfRenderer']['title']['runs'][0]['text']
|
song['played'] = content['musicShelfRenderer']['title']['runs'][0]['text']
|
||||||
songs.extend(songlist)
|
songs.extend(songlist)
|
||||||
|
|
@ -248,8 +248,9 @@ class YTMusic:
|
||||||
if song_count == 0:
|
if song_count == 0:
|
||||||
return songs
|
return songs
|
||||||
|
|
||||||
|
own_playlist = 'musicEditablePlaylistDetailHeaderRenderer' in response['header']
|
||||||
|
songs.extend(parse_playlist_items(results['contents'], own_playlist))
|
||||||
songs_to_get = min(limit, song_count)
|
songs_to_get = min(limit, song_count)
|
||||||
songs.extend(parse_songs(results['contents']))
|
|
||||||
request_count = 1
|
request_count = 1
|
||||||
|
|
||||||
while request_count * 100 < songs_to_get:
|
while request_count * 100 < songs_to_get:
|
||||||
|
|
@ -259,7 +260,7 @@ class YTMusic:
|
||||||
|
|
||||||
response = self.__send_request(endpoint, body, additionalParams)
|
response = self.__send_request(endpoint, body, additionalParams)
|
||||||
results = response['continuationContents']['musicPlaylistShelfContinuation']
|
results = response['continuationContents']['musicPlaylistShelfContinuation']
|
||||||
songs.extend(parse_songs(results['contents']))
|
songs.extend(parse_playlist_items(results['contents'], own_playlist))
|
||||||
request_count += 1
|
request_count += 1
|
||||||
|
|
||||||
return songs
|
return songs
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue