Improve search error handling, remove unneeded print()
This commit is contained in:
parent
ecdda96b7f
commit
1fc42b05e9
2 changed files with 20 additions and 14 deletions
|
|
@ -13,6 +13,10 @@ def get_owned_playlist():
|
||||||
songs = youtube_auth.get_playlist_items('PLQwVIlKxHM6oCeKLTsUx_w9CmWjHKdg68')
|
songs = youtube_auth.get_playlist_items('PLQwVIlKxHM6oCeKLTsUx_w9CmWjHKdg68')
|
||||||
assert (len(songs) == 64)
|
assert (len(songs) == 64)
|
||||||
|
|
||||||
|
def search():
|
||||||
|
results = youtube_auth.search("Oasis Wonderwall")
|
||||||
|
print(results)
|
||||||
|
|
||||||
#end to end test adding playlist, adding item, deleting item, deleting playlist
|
#end to end test adding playlist, adding item, deleting item, deleting playlist
|
||||||
def end2end():
|
def end2end():
|
||||||
playlist = youtube_auth.add_playlist("test", "test description", False)
|
playlist = youtube_auth.add_playlist("test", "test description", False)
|
||||||
|
|
@ -38,4 +42,5 @@ def end2end():
|
||||||
# YTMusic.search('lione revive')
|
# YTMusic.search('lione revive')
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
end2end()
|
#end2end()
|
||||||
|
search()
|
||||||
|
|
|
||||||
|
|
@ -24,12 +24,16 @@ class YTMusic:
|
||||||
self.body['query'] = query
|
self.body['query'] = query
|
||||||
self.body['params'] = 'Eg-KAQwIARAAGAAgACgAMABqChAEEAMQCRAFEAo%3D'
|
self.body['params'] = 'Eg-KAQwIARAAGAAgACgAMABqChAEEAMQCRAFEAo%3D'
|
||||||
endpoint = 'search'
|
endpoint = 'search'
|
||||||
|
|
||||||
response = self.__send_request(endpoint)
|
|
||||||
results = response['contents']['sectionListRenderer']['contents'][0]['musicShelfRenderer']['contents']
|
|
||||||
songs = []
|
songs = []
|
||||||
for result in results:
|
try:
|
||||||
try:
|
response = self.__send_request(endpoint)
|
||||||
|
results = response['contents']['sectionListRenderer']['contents']
|
||||||
|
for res in results:
|
||||||
|
if 'musicShelfRenderer' in res:
|
||||||
|
results = res['musicShelfRenderer']['contents']
|
||||||
|
break
|
||||||
|
|
||||||
|
for result in results:
|
||||||
data = result['musicResponsiveListItemRenderer']
|
data = result['musicResponsiveListItemRenderer']
|
||||||
# videoId
|
# videoId
|
||||||
videoId = data['overlay']['musicItemThumbnailOverlayRenderer']['content']['musicPlayButtonRenderer']['playNavigationEndpoint']['watchEndpoint']['videoId']
|
videoId = data['overlay']['musicItemThumbnailOverlayRenderer']['content']['musicPlayButtonRenderer']['playNavigationEndpoint']['watchEndpoint']['videoId']
|
||||||
|
|
@ -37,10 +41,8 @@ class YTMusic:
|
||||||
title = data['flexColumns'][0]['musicResponsiveListItemFlexColumnRenderer']['text']['runs'][0]['text']
|
title = data['flexColumns'][0]['musicResponsiveListItemFlexColumnRenderer']['text']['runs'][0]['text']
|
||||||
song = {'videoId': videoId, 'artist': artist, 'title': title}
|
song = {'videoId': videoId, 'artist': artist, 'title': title}
|
||||||
songs.append(song)
|
songs.append(song)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(str(e))
|
print(str(e))
|
||||||
|
|
||||||
print(str(len(songs)) + " search results")
|
|
||||||
|
|
||||||
return songs
|
return songs
|
||||||
|
|
||||||
|
|
@ -70,7 +72,6 @@ class YTMusic:
|
||||||
songs.extend(parse_songs(results))
|
songs.extend(parse_songs(results))
|
||||||
request_count += 1
|
request_count += 1
|
||||||
|
|
||||||
print(len(songs))
|
|
||||||
return songs
|
return songs
|
||||||
|
|
||||||
def add_playlist(self, title, description, public=False):
|
def add_playlist(self, title, description, public=False):
|
||||||
|
|
@ -85,14 +86,14 @@ class YTMusic:
|
||||||
self.body['playlistId'] = playlistId
|
self.body['playlistId'] = playlistId
|
||||||
endpoint = 'playlist/delete'
|
endpoint = 'playlist/delete'
|
||||||
response = self.__send_request(endpoint)
|
response = self.__send_request(endpoint)
|
||||||
print(response)
|
return response['status'] if 'status' in response else response
|
||||||
|
|
||||||
def add_playlist_item(self, playlistId, videoId):
|
def add_playlist_item(self, playlistId, videoId):
|
||||||
self.body['playlistId'] = playlistId
|
self.body['playlistId'] = playlistId
|
||||||
self.body['actions'] = {'action': 'ACTION_ADD_VIDEO', 'addedVideoId': videoId}
|
self.body['actions'] = {'action': 'ACTION_ADD_VIDEO', 'addedVideoId': videoId}
|
||||||
endpoint = 'browse/edit_playlist'
|
endpoint = 'browse/edit_playlist'
|
||||||
response = self.__send_request(endpoint)
|
response = self.__send_request(endpoint)
|
||||||
print(response)
|
return response['status'] if 'status' in response else response
|
||||||
|
|
||||||
def remove_playlist_item(self, playlistId, song):
|
def remove_playlist_item(self, playlistId, song):
|
||||||
if not song['removeAction']:
|
if not song['removeAction']:
|
||||||
|
|
@ -103,4 +104,4 @@ class YTMusic:
|
||||||
self.body['actions'] = song['removeAction']
|
self.body['actions'] = song['removeAction']
|
||||||
endpoint = 'browse/edit_playlist'
|
endpoint = 'browse/edit_playlist'
|
||||||
response = self.__send_request(endpoint)
|
response = self.__send_request(endpoint)
|
||||||
print(response)
|
return response['status'] if 'status' in response else response
|
||||||
Loading…
Reference in a new issue