From 436d1cd7f3504c863795d3ea35e177deb23b8f5f Mon Sep 17 00:00:00 2001 From: impliedchaos Date: Sat, 22 May 2021 11:01:23 -0400 Subject: [PATCH] Added get_signatureTimestamp() method. --- tests/test.py | 4 ++++ ytmusicapi/mixins/browsing.py | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/tests/test.py b/tests/test.py index 303c980..c3e5c9e 100644 --- a/tests/test.py +++ b/tests/test.py @@ -136,6 +136,10 @@ class TestYTMusic(unittest.TestCase): self.assertIsNone(playlist["lyrics"]) self.assertRaises(Exception, self.yt.get_lyrics, playlist["lyrics"]) + def test_get_signatureTimestamp(self): + signatureTimestamp = self.yt.get_signatureTimestamp() + self.assertIsNotNone(signatureTimestamp) + ############### # WATCH ############### diff --git a/ytmusicapi/mixins/browsing.py b/ytmusicapi/mixins/browsing.py index 5ede872..f267cc0 100644 --- a/ytmusicapi/mixins/browsing.py +++ b/ytmusicapi/mixins/browsing.py @@ -748,3 +748,32 @@ class BrowsingMixin: + ['musicDescriptionShelfRenderer', 'footer'] + RUN_TEXT, True) return lyrics + + def get_basejs_url(self): + """ + Extract the URL for the `base.js` script from YouTube Music. + + :return: URL to `base.js` + """ + response = self._send_get_request(url=YTM_DOMAIN) + match = re.search(r'jsUrl"\s*:\s*"([^"]+)"', response) + if match is None: + raise Exception("Could not identify the URL for base.js player.") + return YTM_DOMAIN + match.group(1) + + def get_signatureTimestamp(self, url: str = None): + """ + Fetch the `base.js` script from YouTube Music and parse out the + `signatureTimestamp` for use with :py:func:`get_song`. + + :param url: Optional. Provide the URL of the `base.js` script. If this + isn't specified a call will be made to :py:func:`get_basejs_url`. + :return: `signatureTimestamp` string + """ + if url is None: + url = self.get_basejs_url() + response = self._send_get_request(url=url) + match = re.search(r"signatureTimestamp[:=](\d+)", response) + if match is None: + raise Exception("Unable to identify the signatureTimestamp.") + return match.group(1)