]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/testurl.py
32cae429ee9110624d3234a50f72e7eba6ca1d3c
[yt-dlp.git] / yt_dlp / extractor / testurl.py
1 import re
2
3 from .common import InfoExtractor
4 from ..utils import ExtractorError
5
6
7 class TestURLIE(InfoExtractor):
8 """ Allows addressing of the test cases as test:yout.*be_1 """
9
10 IE_DESC = False # Do not list
11 _VALID_URL = r'test(?:url)?:(?P<extractor>.+?)(?:_(?P<num>[0-9]+))?$'
12
13 def _real_extract(self, url):
14 from ..extractor import gen_extractor_classes
15
16 extractor_id, num = self._match_valid_url(url).group('extractor', 'num')
17
18 rex = re.compile(extractor_id, flags=re.IGNORECASE)
19 matching_extractors = [e for e in gen_extractor_classes() if rex.search(e.IE_NAME)]
20
21 if len(matching_extractors) == 0:
22 raise ExtractorError('No extractors matching {extractor_id!r} found', expected=True)
23 elif len(matching_extractors) > 1:
24 try: # Check for exact match
25 extractor = next(
26 ie for ie in matching_extractors
27 if ie.IE_NAME.lower() == extractor_id.lower())
28 except StopIteration:
29 raise ExtractorError(
30 'Found multiple matching extractors: %s' % ' '.join(ie.IE_NAME for ie in matching_extractors),
31 expected=True)
32 else:
33 extractor = matching_extractors[0]
34
35 testcases = tuple(extractor.get_testcases(True))
36 try:
37 tc = testcases[int(num or 0)]
38 except IndexError:
39 raise ExtractorError(
40 f'Test case {num or 0} not found, got only {len(testcases)} tests', expected=True)
41
42 self.to_screen(f'Test URL: {tc["url"]}')
43 return self.url_result(tc['url'])