simplified solution

This commit is contained in:
sigma67 2025-02-04 21:37:10 +01:00 • committed by sigma67
commit c60950a2f4

View file

@ -4,7 +4,7 @@ import locale
import time import time
from collections.abc import Iterator from collections.abc import Iterator
from contextlib import contextmanager, suppress from contextlib import contextmanager, suppress
from functools import partial from functools import cached_property, partial
from pathlib import Path from pathlib import Path
from typing import Optional, Union from typing import Optional, Union
@ -15,14 +15,13 @@ from requests.structures import CaseInsensitiveDict
from ytmusicapi.helpers import ( from ytmusicapi.helpers import (
SUPPORTED_LANGUAGES, SUPPORTED_LANGUAGES,
SUPPORTED_LOCATIONS, SUPPORTED_LOCATIONS,
USER_AGENT,
YTM_BASE_API, YTM_BASE_API,
YTM_DOMAIN,
YTM_PARAMS, YTM_PARAMS,
YTM_PARAMS_KEY, YTM_PARAMS_KEY,
get_authorization, get_authorization,
get_visitor_id, get_visitor_id,
initialize_context, initialize_context,
initialize_headers,
sapisid_from_cookie, sapisid_from_cookie,
) )
from ytmusicapi.mixins.browsing import BrowsingMixin from ytmusicapi.mixins.browsing import BrowsingMixin
@ -150,29 +149,23 @@ class YTMusicBase:
except KeyError: except KeyError:
raise YTMusicUserError("Your cookie is missing the required value __Secure-3PAPISID") raise YTMusicUserError("Your cookie is missing the required value __Secure-3PAPISID")
@property @cached_property
def base_headers(self) -> CaseInsensitiveDict: def base_headers(self) -> CaseInsensitiveDict:
if self.auth_type == AuthType.BROWSER or self.auth_type == AuthType.OAUTH_CUSTOM_FULL: headers = (
return self._auth_headers self._auth_headers
if self.auth_type == AuthType.BROWSER or self.auth_type == AuthType.OAUTH_CUSTOM_FULL
return CaseInsensitiveDict( else initialize_headers()
{
"user-agent": USER_AGENT,
"accept": "*/*",
"accept-encoding": "gzip, deflate",
"content-type": "application/json",
"content-encoding": "gzip",
"origin": YTM_DOMAIN,
}
) )
if "X-Goog-Visitor-Id" not in headers:
headers.update(get_visitor_id(partial(self._send_get_request, use_base_headers=True)))
return headers
@property @property
def headers(self) -> CaseInsensitiveDict: def headers(self) -> CaseInsensitiveDict:
headers = self.base_headers headers = self.base_headers
if "X-Goog-Visitor-Id" not in headers:
headers.update(get_visitor_id(partial(self._send_get_request, use_base_headers=True)))
# keys updated each use, custom oauth implementations left untouched # keys updated each use, custom oauth implementations left untouched
if self.auth_type == AuthType.BROWSER: if self.auth_type == AuthType.BROWSER:
headers["authorization"] = get_authorization(self.sapisid + " " + self.origin) headers["authorization"] = get_authorization(self.sapisid + " " + self.origin)
@ -251,7 +244,7 @@ class YTMusicBase:
url, url,
params=params, params=params,
# handle first-use x-goog-visitor-id fetching # handle first-use x-goog-visitor-id fetching
headers=self.base_headers if use_base_headers else self.headers, headers=initialize_headers() if use_base_headers else self.headers,
proxies=self.proxies, proxies=self.proxies,
cookies=self.cookies, cookies=self.cookies,
) )