add consent cookie to avoid consent page trigger (could cause unauthenticated requests to fail)

This commit is contained in:
sigma67 2022-05-05 15:53:08 +02:00
commit 7c242ba40b
No known key found for this signature in database
GPG key ID: 1545A01D4474BAF0

View file

@ -17,7 +17,8 @@ from ytmusicapi.mixins.playlists import PlaylistsMixin
from ytmusicapi.mixins.uploads import UploadsMixin from ytmusicapi.mixins.uploads import UploadsMixin
class YTMusic(BrowsingMixin, SearchMixin, WatchMixin, ExploreMixin, LibraryMixin, PlaylistsMixin, UploadsMixin): class YTMusic(BrowsingMixin, SearchMixin, WatchMixin, ExploreMixin, LibraryMixin, PlaylistsMixin,
UploadsMixin):
""" """
Allows automated interactions with YouTube Music by emulating the YouTube web client's requests. Allows automated interactions with YouTube Music by emulating the YouTube web client's requests.
Permits both authenticated and non-authenticated requests. Permits both authenticated and non-authenticated requests.
@ -74,6 +75,7 @@ class YTMusic(BrowsingMixin, SearchMixin, WatchMixin, ExploreMixin, LibraryMixin
self._session = requests.api self._session = requests.api
self.proxies = proxies self.proxies = proxies
self.cookies = {'CONSENT': 'YES+1'}
# prepare headers # prepare headers
if auth: if auth:
@ -110,9 +112,7 @@ class YTMusic(BrowsingMixin, SearchMixin, WatchMixin, ExploreMixin, LibraryMixin
except locale.Error: except locale.Error:
with suppress(locale.Error): with suppress(locale.Error):
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8') locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
self.lang = gettext.translation('base', self.lang = gettext.translation('base', localedir=locale_dir, languages=[language])
localedir=locale_dir,
languages=[language])
self.parser = browsing.Parser(self.lang) self.parser = browsing.Parser(self.lang)
if user: if user:
@ -134,7 +134,8 @@ class YTMusic(BrowsingMixin, SearchMixin, WatchMixin, ExploreMixin, LibraryMixin
response = self._session.post(YTM_BASE_API + endpoint + YTM_PARAMS + additionalParams, response = self._session.post(YTM_BASE_API + endpoint + YTM_PARAMS + additionalParams,
json=body, json=body,
headers=self.headers, headers=self.headers,
proxies=self.proxies) proxies=self.proxies,
cookies=self.cookies)
response_text = json.loads(response.text) response_text = json.loads(response.text)
if response.status_code >= 400: if response.status_code >= 400:
message = "Server returned HTTP " + str( message = "Server returned HTTP " + str(
@ -144,7 +145,11 @@ class YTMusic(BrowsingMixin, SearchMixin, WatchMixin, ExploreMixin, LibraryMixin
return response_text return response_text
def _send_get_request(self, url: str, params: Dict = None): def _send_get_request(self, url: str, params: Dict = None):
response = self._session.get(url, params=params, headers=self.headers, proxies=self.proxies) response = self._session.get(url,
params=params,
headers=self.headers,
proxies=self.proxies,
cookies=self.cookies)
return response.text return response.text
def _check_auth(self): def _check_auth(self):