From 0f955aa783b753b652311285f02e51a5f330d49e Mon Sep 17 00:00:00 2001 From: Hellow <74311245+HeIIow2@users.noreply.github.com> Date: Sun, 10 Sep 2023 23:12:49 +0200 Subject: [PATCH] feat: cookie consent and innertube context --- src/music_kraken/connection/connection.py | 2 +- .../pages/youtube_music/youtube_music.py | 50 +++++++++++++++++-- 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/src/music_kraken/connection/connection.py b/src/music_kraken/connection/connection.py index 916e49a..ee93517 100644 --- a/src/music_kraken/connection/connection.py +++ b/src/music_kraken/connection/connection.py @@ -229,7 +229,7 @@ class Connection: def post( self, url: str, - json: dict, + json: dict = None, refer_from_origin: bool = True, stream: bool = False, accepted_response_codes: set = None, diff --git a/src/music_kraken/pages/youtube_music/youtube_music.py b/src/music_kraken/pages/youtube_music/youtube_music.py index 5b40e88..7cc71a4 100644 --- a/src/music_kraken/pages/youtube_music/youtube_music.py +++ b/src/music_kraken/pages/youtube_music/youtube_music.py @@ -5,6 +5,7 @@ import random import json from dataclasses import dataclass import re +import requests from ...utils.exception.config import SettingValueError from ...utils.config import main_settings, youtube_settings, logging_settings @@ -59,11 +60,25 @@ class YoutubeMusicConnection(Connection): # cookie consent for youtube # https://stackoverflow.com/a/66940841/16804841 + """ self.session.cookies.set( - name='CONSENT', value='YES+cb.20210328-17-p0.en-GB+FX+{}'.format(random.randint(100, 999)), + name='CONSENT', + value='YES+cb.20250328-17-p0.en-GB+FX+{}'.format(random.randint(100, 999)), + path='/', domain='.youtube.com' + ) + + self.session.cookies.set( + name='CONSENT', + value='YES+cb.20250328-17-p0.en-GB+FX+{}'.format(random.randint(100, 999)), path='/', domain='.youtube.com' ) - self.start_hearthbeat() + """ + self.session.cookies.set( + name='CONSENT', + value='PENDING+258', + path='/', domain='.youtube.com' + ) + # self.start_hearthbeat() def hearthbeat(self): r = self.get("https://music.youtube.com/verify_session", is_hearthbeat=True) @@ -118,12 +133,39 @@ class YoutubeMusic(SuperYouTube): can be found at `view-source:https://music.youtube.com/` search for: "innertubeApiKey" """ + r = self.connection.get("https://music.youtube.com/") if r is None: return + if urlparse(r.url).netloc == "consent.youtube.com": + r = self.connection.post("https://consent.youtube.com/save", data={ + 'gl': 'DE', + 'm': '0', + 'app': '0', + 'pc': 'ytm', + 'continue': 'https://music.youtube.com/?cbrd=1', + 'x': '6', + 'bl': 'boq_identityfrontenduiserver_20230905.04_p0', + 'hl': 'en', + 'src': '1', + 'cm': '2', + 'set_ytc': 'true', + 'set_apyt': 'true', + 'set_eom': 'false' + }) + if r is None: + return + + r = self.connection.get("https://music.youtube.com/") + if r is None: + return + content = r.text + if DEBUG: + dump_to_file(f"youtube_music_index.html", r.text, exit_after_dump=False) + # api key api_key_pattern = ( r"(?<=\"innertubeApiKey\":\")(.*?)(?=\")", @@ -155,10 +197,10 @@ class YoutubeMusic(SuperYouTube): # context context_pattern = r"(?<=\"INNERTUBE_CONTEXT\":{)(.*?)(?=},\"INNERTUBE_CONTEXT_CLIENT_NAME\":)" found_context = False - for context_string in re.findall(context_pattern, content): + for context_string in re.findall(context_pattern, content, re.M): try: youtube_settings["youtube_music_innertube_context"] = json.loads("{" + context_string + "}") - found_context + found_context = True except json.decoder.JSONDecodeError: continue