]> jfr.im git - yt-dlp.git/blobdiff - yt_dlp/extractor/testurl.py
[cleanup] Misc
[yt-dlp.git] / yt_dlp / extractor / testurl.py
index 140fa4a9634c52b2842a344853f693a48f8f4a84..3cf00177650262236ed78c8de568330ba923295d 100644 (file)
@@ -8,55 +8,43 @@ class TestURLIE(InfoExtractor):
     """ Allows addressing of the test cases as test:yout.*be_1 """
 
     IE_DESC = False  # Do not list
-    _VALID_URL = r'test(?:url)?:(?P<id>(?P<extractor>.+?)(?:_(?P<num>[0-9]+))?)$'
+    _VALID_URL = r'test(?:url)?:(?P<extractor>.*?)(?:_(?P<num>\d+|all))?$'
 
     def _real_extract(self, url):
-        from ..extractor import gen_extractors
+        from . import gen_extractor_classes
 
-        mobj = self._match_valid_url(url)
-        video_id = mobj.group('id')
-        extractor_id = mobj.group('extractor')
-        all_extractors = gen_extractors()
+        extractor_id, num = self._match_valid_url(url).group('extractor', 'num')
+        if not extractor_id:
+            return {'id': ':test', 'title': '', 'url': url}
 
         rex = re.compile(extractor_id, flags=re.IGNORECASE)
-        matching_extractors = [
-            e for e in all_extractors if rex.search(e.IE_NAME)]
+        matching_extractors = [e for e in gen_extractor_classes() if rex.search(e.IE_NAME)]
 
         if len(matching_extractors) == 0:
-            raise ExtractorError(
-                'No extractors matching %r found' % extractor_id,
-                expected=True)
+            raise ExtractorError(f'No extractors matching {extractor_id!r} found', expected=True)
         elif len(matching_extractors) > 1:
-            # Is it obvious which one to pick?
-            try:
-                extractor = next(
-                    ie for ie in matching_extractors
-                    if ie.IE_NAME.lower() == extractor_id.lower())
-            except StopIteration:
+            extractor = next((  # Check for exact match
+                ie for ie in matching_extractors if ie.IE_NAME.lower() == extractor_id.lower()
+            ), None) or next((  # Check for exact match without plugin suffix
+                ie for ie in matching_extractors if ie.IE_NAME.split('+')[0].lower() == extractor_id.lower()
+            ), None)
+            if not extractor:
                 raise ExtractorError(
-                    ('Found multiple matching extractors: %s' %
-                        ' '.join(ie.IE_NAME for ie in matching_extractors)),
+                    'Found multiple matching extractors: %s' % ' '.join(ie.IE_NAME for ie in matching_extractors),
                     expected=True)
         else:
             extractor = matching_extractors[0]
 
-        num_str = mobj.group('num')
-        num = int(num_str) if num_str else 0
-
-        testcases = []
-        t = getattr(extractor, '_TEST', None)
-        if t:
-            testcases.append(t)
-        testcases.extend(getattr(extractor, '_TESTS', []))
-
+        testcases = tuple(extractor.get_testcases(True))
+        if num == 'all':
+            return self.playlist_result(
+                [self.url_result(tc['url'], extractor) for tc in testcases],
+                url, f'{extractor.IE_NAME} tests')
         try:
-            tc = testcases[num]
+            tc = testcases[int(num or 0)]
         except IndexError:
             raise ExtractorError(
-                ('Test case %d not found, got only %d tests' %
-                    (num, len(testcases))),
-                expected=True)
-
-        self.to_screen('Test URL: %s' % tc['url'])
+                f'Test case {num or 0} not found, got only {len(testcases)} tests', expected=True)
 
-        return self.url_result(tc['url'], video_id=video_id)
+        self.to_screen(f'Test URL: {tc["url"]}')
+        return self.url_result(tc['url'], extractor)