mirror of
https://github.com/yt-dlp/yt-dlp.git
synced 2026-03-06 22:30:55 +00:00
test cookie
This commit is contained in:
@@ -2,7 +2,6 @@ from __future__ import annotations
|
||||
|
||||
import http.cookiejar
|
||||
import json
|
||||
import re
|
||||
import subprocess
|
||||
import typing
|
||||
import urllib.parse
|
||||
@@ -16,7 +15,7 @@ from ..utils import (
|
||||
shell_quote,
|
||||
unified_timestamp,
|
||||
)
|
||||
from ._helper import TempFileWrapper, random_string
|
||||
from ._helper import TempFileWrapper, random_string, override_navigator_js, extract_script_tags
|
||||
from .common import ExternalJSI, register_jsi
|
||||
|
||||
|
||||
@@ -36,15 +35,7 @@ class DenoJSI(ExternalJSI):
|
||||
|
||||
@property
|
||||
def _override_navigator_js(self):
|
||||
return '\n'.join([
|
||||
'Object.defineProperty(navigator, "%s", { value: %s, configurable: true });' % (k, json.dumps(v))
|
||||
for k, v in {
|
||||
'userAgent': self.user_agent,
|
||||
'language': 'en-US',
|
||||
'languages': ['en-US'],
|
||||
'webdriver': False,
|
||||
}.items()
|
||||
])
|
||||
return override_navigator_js(self.user_agent)
|
||||
|
||||
def _run_deno(self, cmd):
|
||||
self.write_debug(f'Deno command line: {shell_quote(cmd)}')
|
||||
@@ -137,21 +128,13 @@ class DenoJSDomJSI(DenoJSI):
|
||||
self._run_deno(cmd)
|
||||
self._JSDOM_IMPORT_CHECKED = True
|
||||
|
||||
def _parse_script_tags(self, html: str):
|
||||
for match_start in re.finditer(r'<script[^>]*>', html, re.DOTALL):
|
||||
end = html.find('</script>', match_start.end())
|
||||
if end > match_start.end():
|
||||
yield html[match_start.end():end]
|
||||
|
||||
def execute(self, jscode, video_id=None, note='Executing JS in Deno', location='', html='', cookiejar=None):
|
||||
self.report_note(video_id, note)
|
||||
self._ensure_jsdom()
|
||||
callback_varname = f'__callback_{random_string()}'
|
||||
|
||||
inline_scripts = '\n'.join([
|
||||
'try { %s } catch (e) {}' % script
|
||||
for script in self._parse_script_tags(html)
|
||||
])
|
||||
html, inline_scripts = extract_script_tags(html)
|
||||
wrapper_scripts = '\n'.join(['try { %s } catch (e) {}' % script for script in inline_scripts])
|
||||
|
||||
script = f'''{self._init_script};
|
||||
{self._override_navigator_js};
|
||||
@@ -164,27 +147,33 @@ class DenoJSDomJSI(DenoJSI):
|
||||
}});
|
||||
Object.keys(dom.window).forEach((key) => {{try {{window[key] = dom.window[key]}} catch (e) {{}}}});
|
||||
delete window.jsdom;
|
||||
const origLog = console.log;
|
||||
console.log = () => {{}};
|
||||
console.info = () => {{}};
|
||||
return () => {{
|
||||
const stdout = [];
|
||||
const origLog = console.log;
|
||||
console.log = (...msg) => stdout.push(msg.map(m => m.toString()).join(' '));
|
||||
return () => {{ origLog(JSON.stringify({{
|
||||
stdout: stdout.join('\\n'), cookies: jar.serializeSync().cookies}})); }}
|
||||
}}
|
||||
}})();
|
||||
await (async () => {{
|
||||
{inline_scripts}
|
||||
}})();
|
||||
{callback_varname} = {callback_varname}();
|
||||
await (async () => {{
|
||||
{wrapper_scripts}
|
||||
{callback_varname} = {callback_varname}(); // begin to capture console.log
|
||||
try {{
|
||||
{jscode}
|
||||
}})().finally({callback_varname});
|
||||
}} finally {{
|
||||
{callback_varname}();
|
||||
}}
|
||||
'''
|
||||
|
||||
location_args = ['--location', location] if location else []
|
||||
with TempFileWrapper(script, suffix='.js') as js_file:
|
||||
cmd = [self.exe, 'run', *self._flags, *location_args, js_file.name]
|
||||
data = json.loads(self._run_deno(cmd))
|
||||
result = self._run_deno(cmd)
|
||||
try:
|
||||
data = json.loads(result)
|
||||
except json.JSONDecodeError as e:
|
||||
raise ExtractorError(f'Failed to parse JSON output from Deno: {result}', cause=e)
|
||||
self.apply_cookies(cookiejar, data['cookies'])
|
||||
return data['stdout']
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
from __future__ import annotations
|
||||
import contextlib
|
||||
import json
|
||||
import os
|
||||
import random
|
||||
import re
|
||||
import string
|
||||
import tempfile
|
||||
|
||||
@@ -69,3 +71,31 @@ class TempFileWrapper:
|
||||
|
||||
def random_string(length: int = 10) -> str:
|
||||
return ''.join(random.choices(string.ascii_letters, k=length))
|
||||
|
||||
|
||||
def override_navigator_js(user_agent: str) -> str:
|
||||
return '\n'.join([
|
||||
'Object.defineProperty(navigator, "%s", { value: %s, configurable: true });' % (k, json.dumps(v))
|
||||
for k, v in {
|
||||
'userAgent': user_agent,
|
||||
'language': 'en-US',
|
||||
'languages': ['en-US'],
|
||||
'webdriver': False,
|
||||
}.items()
|
||||
])
|
||||
|
||||
|
||||
def extract_script_tags(html: str) -> tuple[str, list[str]]:
|
||||
script_indicies = []
|
||||
inline_scripts = []
|
||||
|
||||
for match_start in re.finditer(r'<script[^>]*>', html, re.DOTALL):
|
||||
end = html.find('</script>', match_start.end())
|
||||
if end > match_start.end():
|
||||
script_indicies.append((match_start.start(), end + len('</script>')))
|
||||
inline_scripts.append(html[match_start.end():end])
|
||||
|
||||
for start, end in script_indicies:
|
||||
html = html[:start] + html[end:]
|
||||
|
||||
return html, inline_scripts
|
||||
|
||||
@@ -16,7 +16,7 @@ from ..utils import (
|
||||
is_outdated_version,
|
||||
shell_quote,
|
||||
)
|
||||
from ._helper import TempFileWrapper, random_string
|
||||
from ._helper import TempFileWrapper, random_string, extract_script_tags
|
||||
from .common import ExternalJSI, register_jsi
|
||||
|
||||
|
||||
@@ -135,19 +135,23 @@ class PhantomJSJSI(ExternalJSI):
|
||||
if 'saveAndExit();' not in jscode:
|
||||
raise ExtractorError('`saveAndExit();` not found in `jscode`')
|
||||
|
||||
html, inline_scripts = extract_script_tags(html)
|
||||
wrapped_scripts = '\n'.join([
|
||||
'page.evaluate(function() { try { %s } catch (e) {} });' % inline for inline in inline_scripts])
|
||||
|
||||
html_file = TempFileWrapper(html, suffix='.html')
|
||||
cookie_file = TempFileWrapper(self._save_cookies(url, cookiejar), suffix='.json')
|
||||
|
||||
jscode = self._TEMPLATE.format_map({
|
||||
script = self._TEMPLATE.format_map({
|
||||
'url': json.dumps(str(url)),
|
||||
'ua': json.dumps(str(self.user_agent)),
|
||||
'jscode': jscode,
|
||||
'jscode': f'{wrapped_scripts}\n{jscode}',
|
||||
'html_fn': json.dumps(html_file.name),
|
||||
'cookies_fn': json.dumps(cookie_file.name),
|
||||
'timeout': int(self.timeout * 1000),
|
||||
})
|
||||
|
||||
stdout = self._execute(jscode, video_id, note=note)
|
||||
stdout = self._execute(script, video_id, note=note)
|
||||
self._load_cookies(cookie_file.read(), cookiejar)
|
||||
new_html = html_file.read()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user