1
0
Fork 0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2025-03-09 12:50:23 -05:00
This commit is contained in:
c-basalt 2024-12-30 15:53:04 -05:00
parent 2a4ab042b8
commit 96c6cdeec4
2 changed files with 11 additions and 10 deletions

View file

@ -71,6 +71,7 @@ class DenoJSDomJSI(DenoJSI):
_BASE_PREFERENCE = 4 _BASE_PREFERENCE = 4
_DENO_FLAGS = ['--cached-only', '--no-prompt', '--no-check'] _DENO_FLAGS = ['--cached-only', '--no-prompt', '--no-check']
_JSDOM_IMPORT_CHECKED = False _JSDOM_IMPORT_CHECKED = False
_JSDOM_URL = 'https://cdn.esm.sh/jsdom'
@staticmethod @staticmethod
def serialize_cookie(cookiejar: YoutubeDLCookieJar | None, url: str): 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 # https://github.com/salesforce/tough-cookie/blob/master/lib/cookie/cookie.ts
if not cookiejar: if not cookiejar:
return json.dumps({'cookies': []}) 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': [{ return json.dumps({'cookies': [{
'key': cookie.name, 'key': cookie.name,
'value': cookie.value, '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, 'domain': cookie.domain.lstrip('.') or urllib.parse.urlparse(url).hostname,
'expires': int_or_none(cookie.expires, invscale=1000), 'expires': int_or_none(cookie.expires, invscale=1000),
'hostOnly': not cookie.domain_initial_dot, 'hostOnly': not cookie.domain_initial_dot,
@ -118,7 +119,7 @@ def apply_cookies(cookiejar: YoutubeDLCookieJar | None, cookies: list[dict]):
def _ensure_jsdom(self): def _ensure_jsdom(self):
if self._JSDOM_IMPORT_CHECKED: if self._JSDOM_IMPORT_CHECKED:
return 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] cmd = [self.exe, 'run', js_file.name]
self._run_deno(cmd) self._run_deno(cmd)
self._JSDOM_IMPORT_CHECKED = True 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() self._ensure_jsdom()
callback_varname = f'__callback_{random_string()}' callback_varname = f'__callback_{random_string()}'
script = f'''{self._init_script}; script = f'''{self._init_script};
import jsdom from "https://cdn.esm.sh/jsdom"; import jsdom from "{self._JSDOM_URL}";
const {callback_varname} = (() => {{ const {callback_varname} = (() => {{
const jar = jsdom.CookieJar.deserializeSync({json.dumps(self.serialize_cookie(cookiejar, location))}); const jar = jsdom.CookieJar.deserializeSync({json.dumps(self.serialize_cookie(cookiejar, location))});
const dom = new jsdom.JSDOM({json.dumps(str(html))}, {{ const dom = new jsdom.JSDOM({json.dumps(str(html))}, {{
{'url: %s,' % json.dumps(str(location)) if location else ''} {'url: %s,' % json.dumps(str(location)) if location else ''}
cookieJar: jar, cookieJar: jar,
runScripts: 'dangerously',
}}); }});
Object.keys(dom.window).forEach((key) => {{try {{window[key] = dom.window[key]}} catch (e) {{}}}}); Object.keys(dom.window).forEach((key) => {{try {{window[key] = dom.window[key]}} catch (e) {{}}}});
delete window.jsdom; delete window.jsdom;

View file

@ -11,6 +11,7 @@
from ..utils import ( from ..utils import (
ExtractorError, ExtractorError,
Popen, Popen,
filter_dict,
int_or_none, int_or_none,
is_outdated_version, is_outdated_version,
shell_quote, shell_quote,
@ -87,19 +88,17 @@ def _cookie_to_dict(cookie: http.cookiejar.Cookie):
if not cookie_dict['domain']: if not cookie_dict['domain']:
cookie_dict['domain'] = urllib.parse.urlparse(url).hostname cookie_dict['domain'] = urllib.parse.urlparse(url).hostname
cookie_dict['port'] = urllib.parse.urlparse(url).port 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): with contextlib.suppress(TypeError):
if (cookie.has_nonstandard_attr('httpOnly') if (cookie.has_nonstandard_attr('httpOnly')
or cookie.has_nonstandard_attr('httponly') or cookie.has_nonstandard_attr('httponly')
or cookie.has_nonstandard_attr('HttpOnly')): or cookie.has_nonstandard_attr('HttpOnly')):
cookie_dict['httponly'] = True cookie_dict['httponly'] = True
return cookie_dict return filter_dict(cookie_dict)
cookies = cookiejar.get_cookies_for_url(url) if cookiejar else [] cookies = cookiejar.get_cookies_for_url(url) if cookiejar else []
return json.dumps([_cookie_to_dict(cookie) for cookie in cookies]) 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: if not cookiejar:
return return
cookies = json.loads(cookies_json) cookies = json.loads(cookies_json)
@ -110,7 +109,7 @@ def _load_cookies(self, cookies_json: str, cookiejar):
cookie.get('path', '/'), True, cookie.get('path', '/'), True,
cookie.get('secure', False), cookie.get('expiry'), cookie.get('secure', False), cookie.get('expiry'),
cookie.get('discard', False), None, None, 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'): 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') html_file = TempFileWrapper(html, suffix='.html')
cookie_file = TempFileWrapper(self._save_cookies(url, cookiejar), suffix='.json') cookie_file = TempFileWrapper(self._save_cookies(url, cookiejar), suffix='.json')
jscode = self._TEMPLATE.format(**{ jscode = self._TEMPLATE.format_map({
'url': json.dumps(str(url)), 'url': json.dumps(str(url)),
'ua': json.dumps(str(self.user_agent)), 'ua': json.dumps(str(self.user_agent)),
'jscode': jscode, 'jscode': jscode,