feat: cookie consent and innertube context

This commit is contained in:
Hellow 2023-09-10 23:12:49 +02:00
parent 0ce6be5d2f
commit 0f955aa783
2 changed files with 47 additions and 5 deletions

View File

@ -229,7 +229,7 @@ class Connection:
def post( def post(
self, self,
url: str, url: str,
json: dict, json: dict = None,
refer_from_origin: bool = True, refer_from_origin: bool = True,
stream: bool = False, stream: bool = False,
accepted_response_codes: set = None, accepted_response_codes: set = None,

View File

@ -5,6 +5,7 @@ import random
import json import json
from dataclasses import dataclass from dataclasses import dataclass
import re import re
import requests
from ...utils.exception.config import SettingValueError from ...utils.exception.config import SettingValueError
from ...utils.config import main_settings, youtube_settings, logging_settings from ...utils.config import main_settings, youtube_settings, logging_settings
@ -59,11 +60,25 @@ class YoutubeMusicConnection(Connection):
# cookie consent for youtube # cookie consent for youtube
# https://stackoverflow.com/a/66940841/16804841 # https://stackoverflow.com/a/66940841/16804841
"""
self.session.cookies.set( 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' 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): def hearthbeat(self):
r = self.get("https://music.youtube.com/verify_session", is_hearthbeat=True) 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/` can be found at `view-source:https://music.youtube.com/`
search for: "innertubeApiKey" search for: "innertubeApiKey"
""" """
r = self.connection.get("https://music.youtube.com/") r = self.connection.get("https://music.youtube.com/")
if r is None: if r is None:
return 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 content = r.text
if DEBUG:
dump_to_file(f"youtube_music_index.html", r.text, exit_after_dump=False)
# api key # api key
api_key_pattern = ( api_key_pattern = (
r"(?<=\"innertubeApiKey\":\")(.*?)(?=\")", r"(?<=\"innertubeApiKey\":\")(.*?)(?=\")",
@ -155,10 +197,10 @@ class YoutubeMusic(SuperYouTube):
# context # context
context_pattern = r"(?<=\"INNERTUBE_CONTEXT\":{)(.*?)(?=},\"INNERTUBE_CONTEXT_CLIENT_NAME\":)" context_pattern = r"(?<=\"INNERTUBE_CONTEXT\":{)(.*?)(?=},\"INNERTUBE_CONTEXT_CLIENT_NAME\":)"
found_context = False found_context = False
for context_string in re.findall(context_pattern, content): for context_string in re.findall(context_pattern, content, re.M):
try: try:
youtube_settings["youtube_music_innertube_context"] = json.loads("{" + context_string + "}") youtube_settings["youtube_music_innertube_context"] = json.loads("{" + context_string + "}")
found_context found_context = True
except json.decoder.JSONDecodeError: except json.decoder.JSONDecodeError:
continue continue