]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/extractor/openload.py
[ie/youtube] Suppress "Unavailable videos are hidden" warning (#10159)
[yt-dlp.git] / yt_dlp / extractor / openload.py
index e66ed4831bc03220d2d8e2dbd7a9a19e07aa3c69..2d56252b162918b94bfd2a696ffaaab66cf6a161 100644 (file)
@@ -4,8 +4,8 @@
 import os
 import subprocess
 import tempfile
+import urllib.parse
 
-from ..compat import compat_urlparse
 from ..utils import (
     ExtractorError,
     Popen,
@@ -52,6 +52,8 @@ class PhantomJSwrapper:
     This class is experimental.
     """
 
+    INSTALL_HINT = 'Please download it from https://phantomjs.org/download.html'
+
     _BASE_JS = R'''
         phantom.onError = function(msg, trace) {{
           var msgStack = ['PHANTOM ERROR: ' + msg];
@@ -110,8 +112,7 @@ def __init__(self, extractor, required_version=None, timeout=10000):
 
         self.exe = check_executable('phantomjs', ['-v'])
         if not self.exe:
-            raise ExtractorError(
-                'PhantomJS not found, Please download it from https://phantomjs.org/download.html', expected=True)
+            raise ExtractorError(f'PhantomJS not found, {self.INSTALL_HINT}', expected=True)
 
         self.extractor = extractor
 
@@ -120,7 +121,7 @@ def __init__(self, extractor, required_version=None, timeout=10000):
             if is_outdated_version(version, required_version):
                 self.extractor._downloader.report_warning(
                     'Your copy of PhantomJS is outdated, update it to version '
-                    '%s or newer if you encounter any errors.' % required_version)
+                    f'{required_version} or newer if you encounter any errors.')
 
         for name in self._TMP_FILE_NAMES:
             tmp = tempfile.NamedTemporaryFile(delete=False)
@@ -145,9 +146,9 @@ def _save_cookies(self, url):
             if 'path' not in cookie:
                 cookie['path'] = '/'
             if 'domain' not in cookie:
-                cookie['domain'] = compat_urlparse.urlparse(url).netloc
+                cookie['domain'] = urllib.parse.urlparse(url).netloc
         with open(self._TMP_FILES['cookies'].name, 'wb') as f:
-            f.write(json.dumps(cookies).encode('utf-8'))
+            f.write(json.dumps(cookies).encode())
 
     def _load_cookies(self):
         with open(self._TMP_FILES['cookies'].name, 'rb') as f:
@@ -200,7 +201,7 @@ def get(self, url, html=None, video_id=None, note=None, note2='Executing JS on w
         if not html:
             html = self.extractor._download_webpage(url, video_id, note=note, headers=headers)
         with open(self._TMP_FILES['html'].name, 'wb') as f:
-            f.write(html.encode('utf-8'))
+            f.write(html.encode())
 
         self._save_cookies(url)
 
@@ -211,7 +212,7 @@ def get(self, url, html=None, video_id=None, note=None, note2='Executing JS on w
             'jscode': jscode,
         }))
 
-        stdout = self.execute(jscode, video_id, note2)
+        stdout = self.execute(jscode, video_id, note=note2)
 
         with open(self._TMP_FILES['html'].name, 'rb') as f:
             html = f.read().decode('utf-8')
@@ -219,7 +220,7 @@ def get(self, url, html=None, video_id=None, note=None, note2='Executing JS on w
 
         return html, stdout
 
-    def execute(self, jscode, video_id=None, note='Executing JS'):
+    def execute(self, jscode, video_id=None, *, note='Executing JS'):
         """Execute JS and return stdout"""
         if 'phantom.exit();' not in jscode:
             jscode += ';\nphantom.exit();'
@@ -231,8 +232,12 @@ def execute(self, jscode, video_id=None, note='Executing JS'):
 
         cmd = [self.exe, '--ssl-protocol=any', self._TMP_FILES['script'].name]
         self.extractor.write_debug(f'PhantomJS command line: {shell_quote(cmd)}')
-        stdout, stderr, returncode = Popen.run(cmd, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+        try:
+            stdout, stderr, returncode = Popen.run(cmd, timeout=self.options['timeout'] / 1000,
+                                                   text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
+        except Exception as e:
+            raise ExtractorError(f'{note} failed: Unable to run PhantomJS binary', cause=e)
         if returncode:
-            raise ExtractorError(f'Executing JS failed:\n{stderr.strip()}')
+            raise ExtractorError(f'{note} failed with returncode {returncode}:\n{stderr.strip()}')
 
         return stdout