From e96c4f1ca89131eaab4d6e445a304590fb630f7d Mon Sep 17 00:00:00 2001 From: sigma67 Date: Wed, 15 Apr 2020 00:44:08 +0200 Subject: [PATCH] Simplify setup -> only cookie required SAPISID Authorization now implemented correctly --- docs/source/setup.rst | 2 +- headers_auth.json.example | 1 - ytmusicapi/helpers.py | 20 +++++++++++++++++++- ytmusicapi/setup.py | 2 +- ytmusicapi/ytmusic.py | 3 +++ 5 files changed, 24 insertions(+), 4 deletions(-) diff --git a/docs/source/setup.rst b/docs/source/setup.rst index 558479f..f1fc3e0 100644 --- a/docs/source/setup.rst +++ b/docs/source/setup.rst @@ -23,7 +23,7 @@ in the correct format in the current directory. Manual file creation -------------------- -Alternatively, you can paste the three missing items to `headers_auth.json` below and create your own file: +Alternatively, you can paste the cookie to `headers_auth.json` below and create your own file: .. literalinclude:: ../../headers_auth.json.example :language: JSON \ No newline at end of file diff --git a/headers_auth.json.example b/headers_auth.json.example index 6f08eaf..21d0314 100644 --- a/headers_auth.json.example +++ b/headers_auth.json.example @@ -5,6 +5,5 @@ "Content-Type": "application/json", "X-Goog-AuthUser": "0", "x-origin": "https://music.youtube.com", - "Authorization": "PASTE_AUTHORIZATION", "Cookie" : "PASTE_COOKIE" } \ No newline at end of file diff --git a/ytmusicapi/helpers.py b/ytmusicapi/helpers.py index 7140c0c..26f899d 100644 --- a/ytmusicapi/helpers.py +++ b/ytmusicapi/helpers.py @@ -1,4 +1,7 @@ import re +from http.cookies import SimpleCookie +from hashlib import sha1 +import time #commonly used navigation paths SINGLE_COLUMN_TAB = ['contents', 'singleColumnBrowseResultsRenderer', 'tabs', 0, 'tabRenderer', 'content'] @@ -152,4 +155,19 @@ def html_to_txt(html_text): tags = re.findall("<[^>]+>",html_text) for tag in tags: html_text = html_text.replace(tag,'') - return html_text \ No newline at end of file + return html_text + + +def sapisid_from_cookie(raw_cookie): + cookie = SimpleCookie() + cookie.load(raw_cookie) + return cookie['SAPISID'].value + + +# SAPISID Hash reverse engineered by +# https://stackoverflow.com/a/32065323/5726546 +def get_authorization(auth): + sha_1 = sha1() + unix_timestamp = str(int(time.time())) + sha_1.update((unix_timestamp + ' ' + auth).encode('utf-8')) + return "SAPISIDHASH " + unix_timestamp + "_" + sha_1.hexdigest() \ No newline at end of file diff --git a/ytmusicapi/setup.py b/ytmusicapi/setup.py index 2ef962c..adcb35d 100644 --- a/ytmusicapi/setup.py +++ b/ytmusicapi/setup.py @@ -18,7 +18,7 @@ def setup(filepath=None): break contents.append(line) - required_headers = ["Authorization", "Cookie"] + required_headers = ["Cookie"] try: headers = {} for content in contents: diff --git a/ytmusicapi/ytmusic.py b/ytmusicapi/ytmusic.py index 95e1467..1ba8e52 100644 --- a/ytmusicapi/ytmusic.py +++ b/ytmusicapi/ytmusic.py @@ -45,12 +45,15 @@ class YTMusic: # verify authentication credentials work if auth: + self.sapisid = sapisid_from_cookie(self.headers['Cookie']) response = self.__send_request('guide', {}) if 'error' in response: raise Exception("The provided credentials are invalid. Reason given by the server: " + response['error']['status']) def __send_request(self, endpoint, body, additionalParams=""): body.update(self.context) + if self.auth: + self.headers["Authorization"] = get_authorization(self.sapisid + ' ' + self.headers['x-origin']) response = requests.post(base_url + endpoint + params + additionalParams, json=body, headers=self.headers) return json.loads(response.text)