diff --git a/yt_dlp/jsinterp/_deno.py b/yt_dlp/jsinterp/_deno.py index 84f19ed0a..999bc0c22 100644 --- a/yt_dlp/jsinterp/_deno.py +++ b/yt_dlp/jsinterp/_deno.py @@ -71,6 +71,7 @@ class DenoJSDomJSI(DenoJSI): _BASE_PREFERENCE = 4 _DENO_FLAGS = ['--cached-only', '--no-prompt', '--no-check'] _JSDOM_IMPORT_CHECKED = False + _JSDOM_URL = 'https://cdn.esm.sh/jsdom' @staticmethod def serialize_cookie(cookiejar: YoutubeDLCookieJar | None, url: str): @@ -80,11 +81,11 @@ def serialize_cookie(cookiejar: YoutubeDLCookieJar | None, url: str): # https://github.com/salesforce/tough-cookie/blob/master/lib/cookie/cookie.ts if not cookiejar: return json.dumps({'cookies': []}) - cookies: list[http.cookiejar.Cookie] = [cookie for cookie in cookiejar.get_cookies_for_url(url)] + cookies: list[http.cookiejar.Cookie] = list(cookiejar.get_cookies_for_url(url)) return json.dumps({'cookies': [{ 'key': cookie.name, 'value': cookie.value, - # leading dot must be removed, otherwise will fail to match + # leading dot of domain must be removed, otherwise will fail to match 'domain': cookie.domain.lstrip('.') or urllib.parse.urlparse(url).hostname, 'expires': int_or_none(cookie.expires, invscale=1000), 'hostOnly': not cookie.domain_initial_dot, @@ -118,7 +119,7 @@ def apply_cookies(cookiejar: YoutubeDLCookieJar | None, cookies: list[dict]): def _ensure_jsdom(self): if self._JSDOM_IMPORT_CHECKED: return - with TempFileWrapper('import jsdom from "https://cdn.esm.sh/jsdom"', suffix='.js') as js_file: + with TempFileWrapper(f'import jsdom from "{self._JSDOM_URL}"', suffix='.js') as js_file: cmd = [self.exe, 'run', js_file.name] self._run_deno(cmd) self._JSDOM_IMPORT_CHECKED = True @@ -128,12 +129,13 @@ def execute(self, jscode, video_id=None, note='Executing JS in Deno', location=' self._ensure_jsdom() callback_varname = f'__callback_{random_string()}' script = f'''{self._init_script}; - import jsdom from "https://cdn.esm.sh/jsdom"; + import jsdom from "{self._JSDOM_URL}"; const {callback_varname} = (() => {{ const jar = jsdom.CookieJar.deserializeSync({json.dumps(self.serialize_cookie(cookiejar, location))}); const dom = new jsdom.JSDOM({json.dumps(str(html))}, {{ {'url: %s,' % json.dumps(str(location)) if location else ''} cookieJar: jar, + runScripts: 'dangerously', }}); Object.keys(dom.window).forEach((key) => {{try {{window[key] = dom.window[key]}} catch (e) {{}}}}); delete window.jsdom; diff --git a/yt_dlp/jsinterp/_phantomjs.py b/yt_dlp/jsinterp/_phantomjs.py index 42aad4d3b..37b42a212 100644 --- a/yt_dlp/jsinterp/_phantomjs.py +++ b/yt_dlp/jsinterp/_phantomjs.py @@ -11,6 +11,7 @@ from ..utils import ( ExtractorError, Popen, + filter_dict, int_or_none, is_outdated_version, shell_quote, @@ -87,19 +88,17 @@ def _cookie_to_dict(cookie: http.cookiejar.Cookie): if not cookie_dict['domain']: cookie_dict['domain'] = urllib.parse.urlparse(url).hostname cookie_dict['port'] = urllib.parse.urlparse(url).port - for key in [key for key, value in cookie_dict.items() if value is None]: - cookie_dict.pop(key) with contextlib.suppress(TypeError): if (cookie.has_nonstandard_attr('httpOnly') or cookie.has_nonstandard_attr('httponly') or cookie.has_nonstandard_attr('HttpOnly')): cookie_dict['httponly'] = True - return cookie_dict + return filter_dict(cookie_dict) cookies = cookiejar.get_cookies_for_url(url) if cookiejar else [] return json.dumps([_cookie_to_dict(cookie) for cookie in cookies]) - def _load_cookies(self, cookies_json: str, cookiejar): + def _load_cookies(self, cookies_json: str, cookiejar: YoutubeDLCookieJar | None): if not cookiejar: return cookies = json.loads(cookies_json) @@ -110,7 +109,7 @@ def _load_cookies(self, cookies_json: str, cookiejar): cookie.get('path', '/'), True, cookie.get('secure', False), cookie.get('expiry'), cookie.get('discard', False), None, None, - {'httpOnly': None} if cookie.get('httponly') is True else {} + {'httpOnly': None} if cookie.get('httponly') is True else {}, )) def _execute(self, jscode: str, video_id=None, *, note='Executing JS in PhantomJS'): @@ -139,7 +138,7 @@ def _execute_html(self, jscode: str, url: str, html: str, cookiejar, video_id=No html_file = TempFileWrapper(html, suffix='.html') cookie_file = TempFileWrapper(self._save_cookies(url, cookiejar), suffix='.json') - jscode = self._TEMPLATE.format(**{ + jscode = self._TEMPLATE.format_map({ 'url': json.dumps(str(url)), 'ua': json.dumps(str(self.user_agent)), 'jscode': jscode,