Release 2024.12.03
[yt-dlp3.git] / yt_dlp / extractor / testurl.py
blob31e3c4d9cbe28a4251677ddb639d92c6abd0ddc1
1 import re
3 from .common import InfoExtractor
4 from ..utils import ExtractorError
7 class TestURLIE(InfoExtractor):
8 """ Allows addressing of the test cases as test:yout.*be_1 """
10 IE_DESC = False # Do not list
11 _VALID_URL = r'test(?:url)?:(?P<extractor>.*?)(?:_(?P<num>\d+|all))?$'
13 def _real_extract(self, url):
14 from . import gen_extractor_classes
16 extractor_id, num = self._match_valid_url(url).group('extractor', 'num')
17 if not extractor_id:
18 return {'id': ':test', 'title': '', 'url': url}
20 rex = re.compile(extractor_id, flags=re.IGNORECASE)
21 matching_extractors = [e for e in gen_extractor_classes() if rex.search(e.IE_NAME)]
23 if len(matching_extractors) == 0:
24 raise ExtractorError(f'No extractors matching {extractor_id!r} found', expected=True)
25 elif len(matching_extractors) > 1:
26 extractor = next(( # Check for exact match
27 ie for ie in matching_extractors if ie.IE_NAME.lower() == extractor_id.lower()
28 ), None) or next(( # Check for exact match without plugin suffix
29 ie for ie in matching_extractors if ie.IE_NAME.split('+')[0].lower() == extractor_id.lower()
30 ), None)
31 if not extractor:
32 raise ExtractorError(
33 'Found multiple matching extractors: {}'.format(' '.join(ie.IE_NAME for ie in matching_extractors)),
34 expected=True)
35 else:
36 extractor = matching_extractors[0]
38 testcases = tuple(extractor.get_testcases(True))
39 if num == 'all':
40 return self.playlist_result(
41 [self.url_result(tc['url'], extractor) for tc in testcases],
42 url, f'{extractor.IE_NAME} tests')
43 try:
44 tc = testcases[int(num or 0)]
45 except IndexError:
46 raise ExtractorError(
47 f'Test case {num or 0} not found, got only {len(testcases)} tests', expected=True)
49 self.to_screen(f'Test URL: {tc["url"]}')
50 return self.url_result(tc['url'], extractor)