From d62e3f80791c952e71bb363b6f1a621f8650b919 Mon Sep 17 00:00:00 2001 From: alexmercerind Date: Fri, 4 Dec 2020 12:05:32 +0530 Subject: [PATCH 1/6] Added get_lyrics method to get lyrics of a video or song --- ytmusicapi/mixins/browsing.py | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/ytmusicapi/mixins/browsing.py b/ytmusicapi/mixins/browsing.py index b241bd0..9e9428d 100644 --- a/ytmusicapi/mixins/browsing.py +++ b/ytmusicapi/mixins/browsing.py @@ -636,3 +636,34 @@ class BrowsingMixin: raise Exception('This video is not playable.') return player_response['streamingData'] + + def get_lyrics(self, videoId: str) -> None: + """ + Returns lyrics of a song or video. + + :param videoId: Video id + :return: Dictionary with song lyrics. + + Example:: + + { + "videoId": "ZrOKjDZOtkA", + "browseId": "MPLYt_9nqEki4ZDpp-3", + "lyrics": "Today is gonna be the day\nThat they're gonna throw it back to you\nBy now you should've somehow\nRealized what you gotta do\nI don't believe that anybody\nFeels the way I do, about you now\n\nBackbeat, the word was on the street\nThat the fire in your heart is out\nI'm sure you've heard it all before\nBut you never really had a doubt\nI don't believe that anybody\nFeels the way I do about you now\n\nAnd all the roads we have to walk are winding\nAnd all the lights that lead us there are blinding\nThere are many things that I\nWould like to say to you but I don't know how\n\nBecause maybe, you're gonna be the one that saves me\nAnd after all, you're my wonderwall\n\nToday was gonna be the day\nBut they'll never throw it back to you\nBy now you should've somehow\nRealized what you're not to do\nI don't believe that anybody\nFeels the way I do, about you now\n\nAnd all the roads that lead you there are winding\nAnd all the lights that light the way are blinding\nThere are many things that I\nWould like to say to you but I don't know how\n\nI said maybe, you're gonna be the one that saves me\nAnd after all, you're my wonderwall\n\nI said maybe, you're gonna be the one that saves me\nAnd after all, you're my wonderwall\n\nI said maybe, you're gonna be the one that saves me\nyou're gonna be the one that saves me\nyou're gonna be the one that saves me" + } + + """ + body = { + 'videoId': videoId, + 'browseId': None, + 'lyrics': None, + } + browseIdResponse = self._send_request('next', body) + for tab in browseIdResponse['contents']['singleColumnMusicWatchNextResultsRenderer']['tabbedRenderer']['watchNextTabbedResultsRenderer']['tabs']: + if tab['tabRenderer']['title'] == 'Lyrics': + body['browseId'] = tab['tabRenderer']['endpoint']['browseEndpoint']['browseId'] + if body['browseId'] != None: + response = self._send_request('browse', body) + body['lyrics'] = (response['contents']['sectionListRenderer']['contents'][0]['musicDescriptionShelfRenderer']['description']['runs'][0]['text']) + body.pop('context', None) + return body From a1fee33e37980ef78a3ba7b79feeb2aae7ee24a5 Mon Sep 17 00:00:00 2001 From: alexmercerind Date: Fri, 4 Dec 2020 16:23:20 +0530 Subject: [PATCH 2/6] Added lyrics source & fixed method for songs which do not have lyrics --- ytmusicapi/mixins/browsing.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ytmusicapi/mixins/browsing.py b/ytmusicapi/mixins/browsing.py index 9e9428d..baf4e04 100644 --- a/ytmusicapi/mixins/browsing.py +++ b/ytmusicapi/mixins/browsing.py @@ -664,6 +664,11 @@ class BrowsingMixin: body['browseId'] = tab['tabRenderer']['endpoint']['browseEndpoint']['browseId'] if body['browseId'] != None: response = self._send_request('browse', body) - body['lyrics'] = (response['contents']['sectionListRenderer']['contents'][0]['musicDescriptionShelfRenderer']['description']['runs'][0]['text']) + if 'sectionListRenderer' in response['contents'].keys(): + body['lyrics'] = response['contents']['sectionListRenderer']['contents'][0]['musicDescriptionShelfRenderer']['description']['runs'][0]['text'] + body['source'] = response['contents']['sectionListRenderer']['contents'][0]['musicDescriptionShelfRenderer']['footer']['runs'][0]['text'] + else: + body['lyrics'] = response['contents']['messageRenderer']['subtext']['messageSubtextRenderer']['text']['runs'][0]['text'] + body['source'] = response['contents']['messageRenderer']['text']['runs'][0]['text'] body.pop('context', None) return body From aac93a3010e650b7c479f099566c4f9ebac7d960 Mon Sep 17 00:00:00 2001 From: alexmercerind Date: Fri, 4 Dec 2020 16:33:18 +0530 Subject: [PATCH 3/6] Updated docstring --- ytmusicapi/mixins/browsing.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ytmusicapi/mixins/browsing.py b/ytmusicapi/mixins/browsing.py index baf4e04..e63e377 100644 --- a/ytmusicapi/mixins/browsing.py +++ b/ytmusicapi/mixins/browsing.py @@ -650,6 +650,7 @@ class BrowsingMixin: "videoId": "ZrOKjDZOtkA", "browseId": "MPLYt_9nqEki4ZDpp-3", "lyrics": "Today is gonna be the day\nThat they're gonna throw it back to you\nBy now you should've somehow\nRealized what you gotta do\nI don't believe that anybody\nFeels the way I do, about you now\n\nBackbeat, the word was on the street\nThat the fire in your heart is out\nI'm sure you've heard it all before\nBut you never really had a doubt\nI don't believe that anybody\nFeels the way I do about you now\n\nAnd all the roads we have to walk are winding\nAnd all the lights that lead us there are blinding\nThere are many things that I\nWould like to say to you but I don't know how\n\nBecause maybe, you're gonna be the one that saves me\nAnd after all, you're my wonderwall\n\nToday was gonna be the day\nBut they'll never throw it back to you\nBy now you should've somehow\nRealized what you're not to do\nI don't believe that anybody\nFeels the way I do, about you now\n\nAnd all the roads that lead you there are winding\nAnd all the lights that light the way are blinding\nThere are many things that I\nWould like to say to you but I don't know how\n\nI said maybe, you're gonna be the one that saves me\nAnd after all, you're my wonderwall\n\nI said maybe, you're gonna be the one that saves me\nAnd after all, you're my wonderwall\n\nI said maybe, you're gonna be the one that saves me\nyou're gonna be the one that saves me\nyou're gonna be the one that saves me" + "source": "Source: LyricFind" } """ From 0d81ec9dcd6212801be896feab859b1c49f81fb8 Mon Sep 17 00:00:00 2001 From: alexmercerind Date: Fri, 4 Dec 2020 18:33:23 +0530 Subject: [PATCH 4/6] Added tests for get_lyrics --- tests/test.py | 11 +++++++++++ ytmusicapi/mixins/browsing.py | 1 + 2 files changed, 12 insertions(+) diff --git a/tests/test.py b/tests/test.py index 5b32e4b..a5689a7 100644 --- a/tests/test.py +++ b/tests/test.py @@ -92,6 +92,17 @@ class TestYTMusic(unittest.TestCase): streaming_data = youtube_auth.get_streaming_data("ZrOKjDZOtkA") self.assertEqual(len(streaming_data), 3) + def test_get_lyrics(self): + lyrics_song = youtube.get_lyrics("ZrOKjDZOtkA") + self.assertIsNotNone(lyrics_song["browseId"]) + self.assertIsNotNone(lyrics_song["lyrics"]) + self.assertIsNotNone(lyrics_song["source"]) + + no_lyrics_song = youtube.get_lyrics("9TnpB8WgW4s") + self.assertIsNotNone(no_lyrics_song["browseId"]) + self.assertIsNotNone(no_lyrics_song["lyrics"]) + self.assertIsNotNone(no_lyrics_song["source"]) + ############### # WATCH ############### diff --git a/ytmusicapi/mixins/browsing.py b/ytmusicapi/mixins/browsing.py index e63e377..549e8e6 100644 --- a/ytmusicapi/mixins/browsing.py +++ b/ytmusicapi/mixins/browsing.py @@ -658,6 +658,7 @@ class BrowsingMixin: 'videoId': videoId, 'browseId': None, 'lyrics': None, + 'source': None, } browseIdResponse = self._send_request('next', body) for tab in browseIdResponse['contents']['singleColumnMusicWatchNextResultsRenderer']['tabbedRenderer']['watchNextTabbedResultsRenderer']['tabs']: From 946ad8d07bf361cf4eb201e1e978d646d9a8ac4f Mon Sep 17 00:00:00 2001 From: alexmercerind Date: Fri, 4 Dec 2020 18:41:57 +0530 Subject: [PATCH 5/6] Fixed wrong return annotation in get_lyrics --- ytmusicapi/mixins/browsing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ytmusicapi/mixins/browsing.py b/ytmusicapi/mixins/browsing.py index 549e8e6..ecc36ae 100644 --- a/ytmusicapi/mixins/browsing.py +++ b/ytmusicapi/mixins/browsing.py @@ -637,7 +637,7 @@ class BrowsingMixin: return player_response['streamingData'] - def get_lyrics(self, videoId: str) -> None: + def get_lyrics(self, videoId: str) -> Dict: """ Returns lyrics of a song or video. From eec1ff1d2962d39e784b6d27b87c6cbcb3e40298 Mon Sep 17 00:00:00 2001 From: alexmercerind Date: Fri, 4 Dec 2020 21:20:22 +0530 Subject: [PATCH 6/6] Added lyricsFound key to get_lyrics result & updated tests --- tests/test.py | 2 ++ ytmusicapi/mixins/browsing.py | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/tests/test.py b/tests/test.py index a5689a7..78113ad 100644 --- a/tests/test.py +++ b/tests/test.py @@ -94,11 +94,13 @@ class TestYTMusic(unittest.TestCase): def test_get_lyrics(self): lyrics_song = youtube.get_lyrics("ZrOKjDZOtkA") + self.assertTrue(lyrics_song["lyricsFound"]) self.assertIsNotNone(lyrics_song["browseId"]) self.assertIsNotNone(lyrics_song["lyrics"]) self.assertIsNotNone(lyrics_song["source"]) no_lyrics_song = youtube.get_lyrics("9TnpB8WgW4s") + self.assertFalse(lyrics_song["lyricsFound"]) self.assertIsNotNone(no_lyrics_song["browseId"]) self.assertIsNotNone(no_lyrics_song["lyrics"]) self.assertIsNotNone(no_lyrics_song["source"]) diff --git a/ytmusicapi/mixins/browsing.py b/ytmusicapi/mixins/browsing.py index ecc36ae..4db838d 100644 --- a/ytmusicapi/mixins/browsing.py +++ b/ytmusicapi/mixins/browsing.py @@ -649,6 +649,7 @@ class BrowsingMixin: { "videoId": "ZrOKjDZOtkA", "browseId": "MPLYt_9nqEki4ZDpp-3", + "lyricsFound": true, "lyrics": "Today is gonna be the day\nThat they're gonna throw it back to you\nBy now you should've somehow\nRealized what you gotta do\nI don't believe that anybody\nFeels the way I do, about you now\n\nBackbeat, the word was on the street\nThat the fire in your heart is out\nI'm sure you've heard it all before\nBut you never really had a doubt\nI don't believe that anybody\nFeels the way I do about you now\n\nAnd all the roads we have to walk are winding\nAnd all the lights that lead us there are blinding\nThere are many things that I\nWould like to say to you but I don't know how\n\nBecause maybe, you're gonna be the one that saves me\nAnd after all, you're my wonderwall\n\nToday was gonna be the day\nBut they'll never throw it back to you\nBy now you should've somehow\nRealized what you're not to do\nI don't believe that anybody\nFeels the way I do, about you now\n\nAnd all the roads that lead you there are winding\nAnd all the lights that light the way are blinding\nThere are many things that I\nWould like to say to you but I don't know how\n\nI said maybe, you're gonna be the one that saves me\nAnd after all, you're my wonderwall\n\nI said maybe, you're gonna be the one that saves me\nAnd after all, you're my wonderwall\n\nI said maybe, you're gonna be the one that saves me\nyou're gonna be the one that saves me\nyou're gonna be the one that saves me" "source": "Source: LyricFind" } @@ -657,6 +658,7 @@ class BrowsingMixin: body = { 'videoId': videoId, 'browseId': None, + 'lyricsFound': None, 'lyrics': None, 'source': None, } @@ -664,12 +666,15 @@ class BrowsingMixin: for tab in browseIdResponse['contents']['singleColumnMusicWatchNextResultsRenderer']['tabbedRenderer']['watchNextTabbedResultsRenderer']['tabs']: if tab['tabRenderer']['title'] == 'Lyrics': body['browseId'] = tab['tabRenderer']['endpoint']['browseEndpoint']['browseId'] + break if body['browseId'] != None: response = self._send_request('browse', body) if 'sectionListRenderer' in response['contents'].keys(): + body['lyricsFound'] = True body['lyrics'] = response['contents']['sectionListRenderer']['contents'][0]['musicDescriptionShelfRenderer']['description']['runs'][0]['text'] body['source'] = response['contents']['sectionListRenderer']['contents'][0]['musicDescriptionShelfRenderer']['footer']['runs'][0]['text'] else: + body['lyricsFound'] = False body['lyrics'] = response['contents']['messageRenderer']['subtext']['messageSubtextRenderer']['text']['runs'][0]['text'] body['source'] = response['contents']['messageRenderer']['text']['runs'][0]['text'] body.pop('context', None)