Simplify setup -> only cookie required
SAPISID Authorization now implemented correctly
This commit is contained in:
parent
4f963b6ba1
commit
e96c4f1ca8
5 changed files with 24 additions and 4 deletions
|
|
@ -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
|
||||
|
|
@ -5,6 +5,5 @@
|
|||
"Content-Type": "application/json",
|
||||
"X-Goog-AuthUser": "0",
|
||||
"x-origin": "https://music.youtube.com",
|
||||
"Authorization": "PASTE_AUTHORIZATION",
|
||||
"Cookie" : "PASTE_COOKIE"
|
||||
}
|
||||
|
|
@ -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
|
||||
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()
|
||||
|
|
@ -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:
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue