feat: support age-restricted videos when cookies are provided

This commit is contained in:
2025-11-20 13:02:38 +01:00
parent c979c97077
commit eebf434f3e
3 changed files with 70 additions and 14 deletions

View File

@@ -1,5 +1,6 @@
#!/usr/bin/python3
import brotli, yt_dlp, requests, json, time
from http.cookiejar import MozillaCookieJar
from ythdd_globals import safeTraverse
import ythdd_proto
import ythdd_globals
@@ -19,7 +20,11 @@ ytdl_opts = {
# "formats": ["dashy"]
}
},
"simulate": True
"simulate": True,
"js_runtimes": {
"deno": {}
},
'remote_components': ['ejs:github']
}
stage1_headers = {
@@ -129,7 +134,7 @@ web_context_dict = {
}
}
def extract(url: str, getcomments=False, maxcomments="", manifest_fix=False):
def extract(url: str, getcomments=False, maxcomments="", manifest_fix=False, use_cookies=None):
# TODO: check user-agent and cookiefile
ytdl_context = ytdl_opts.copy()
@@ -137,9 +142,6 @@ def extract(url: str, getcomments=False, maxcomments="", manifest_fix=False):
if ythdd_globals.config['extractor']['user-agent']:
yt_dlp.utils.std_headers['User-Agent'] = ythdd_globals.config['extractor']['user-agent']
if ythdd_globals.config['extractor']['cookies_path']:
ytdl_context['cookiefile'] = ythdd_globals.config['extractor']['cookies_path']
if len(url) == 11:
url = "https://www.youtube.com/watch?v=" + url
if getcomments:
@@ -153,7 +155,27 @@ def extract(url: str, getcomments=False, maxcomments="", manifest_fix=False):
ytdl_context['extractor_args']['youtube']['player_client'] = [ythdd_globals.config['extractor']['preferred_extractor']]
else:
ytdl_context['extractor_args']['youtube']['player_client'] = ['android_vr']
with yt_dlp.YoutubeDL(ytdl_opts) as ytdl:
if use_cookies is not None:
# can be either "global", "agegated" or None
deno_path = ythdd_globals.config['extractor']['deno_path']
match use_cookies:
case "global":
ytdl_context['cookiefile'] = ythdd_globals.config['extractor']['cookies_path']
ytdl_context['extractor_args']['youtube']['player_client'] = ['mweb', 'tv']
if not deno_path:
print("FATAL ERROR: deno path is required for playback using cookies!")
ytdl_context['js_runtimes']['deno']['path'] = deno_path if deno_path else ""
case "agegated":
ytdl_context['cookiefile'] = ythdd_globals.config['extractor']['age_restricted_cookies_path']
ytdl_context['extractor_args']['youtube']['player_client'] = ['mweb', 'tv']
if not deno_path:
print("FATAL ERROR: deno path is required for playback of age-restricted content!")
ytdl_context['js_runtimes']['deno']['path'] = deno_path if deno_path else ""
case None | _:
pass
with yt_dlp.YoutubeDL(ytdl_context) as ytdl:
result = ytdl.sanitize_info(ytdl.extract_info(url, download=False))
return result
@@ -177,7 +199,7 @@ def WEBrelated(url: str):
return extracted_json["contents"]['twoColumnWatchNextResults']["secondaryResults"]
def WEBextractSinglePage(uri: str):
def WEBextractSinglePage(uri: str, use_cookies=None):
# WARNING! HIGHLY EXPERIMENTAL, DUE TO BREAK ANYTIME
start_time = time.time()
@@ -185,7 +207,21 @@ def WEBextractSinglePage(uri: str):
if len(uri) != 11:
raise ValueError("WEBextractSinglePage expects a single, 11-character long argument")
response = requests.get("https://www.youtube.com/watch?v=" + uri, headers=ythdd_globals.getHeaders(caller='extractor'))
cookies = None
if use_cookies is not None:
match use_cookies:
case "global":
ythdd_globals.print_debug("wdata: using global cookies")
cookies = MozillaCookieJar(ythdd_globals.config["extractor"]["cookies_path"])
cookies.load()
case "agegated":
ythdd_globals.print_debug("wdata: using agegated cookies")
cookies = MozillaCookieJar(ythdd_globals.config["extractor"]["age_restricted_cookies_path"])
cookies.load()
case None | _:
pass
response = requests.get("https://www.youtube.com/watch?v=" + uri, headers=ythdd_globals.getHeaders(caller='extractor'), cookies=cookies)
extracted_string = str(response.content.decode('utf8', 'unicode_escape'))
start = extracted_string.find('{"responseContext":{"serviceTrackingParams":')
end = extracted_string.find(';var ', start)