]> jfr.im git - yt-dlp.git/blame - yt_dlp/extractor/testurl.py
[cleanup] Add more ruff rules (#10149)
[yt-dlp.git] / yt_dlp / extractor / testurl.py
CommitLineData
a9c7198a
PH
1import re
2
3from .common import InfoExtractor
4from ..utils import ExtractorError
5
6
7class TestURLIE(InfoExtractor):
dfb1b146 8 """ Allows addressing of the test cases as test:yout.*be_1 """
a9c7198a
PH
9
10 IE_DESC = False # Do not list
812cdfa0 11 _VALID_URL = r'test(?:url)?:(?P<extractor>.*?)(?:_(?P<num>\d+|all))?$'
a9c7198a
PH
12
13 def _real_extract(self, url):
c487cf00 14 from . import gen_extractor_classes
a9c7198a 15
82d02080 16 extractor_id, num = self._match_valid_url(url).group('extractor', 'num')
e5458d1d 17 if not extractor_id:
18 return {'id': ':test', 'title': '', 'url': url}
a9c7198a
PH
19
20 rex = re.compile(extractor_id, flags=re.IGNORECASE)
82d02080 21 matching_extractors = [e for e in gen_extractor_classes() if rex.search(e.IE_NAME)]
a9c7198a
PH
22
23 if len(matching_extractors) == 0:
7aaf4cd2 24 raise ExtractorError(f'No extractors matching {extractor_id!r} found', expected=True)
a9c7198a 25 elif len(matching_extractors) > 1:
e756f45b
M
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:
a9c7198a 32 raise ExtractorError(
add96eb9 33 'Found multiple matching extractors: {}'.format(' '.join(ie.IE_NAME for ie in matching_extractors)),
a9c7198a 34 expected=True)
4d756a9c
PH
35 else:
36 extractor = matching_extractors[0]
a9c7198a 37
82d02080 38 testcases = tuple(extractor.get_testcases(True))
812cdfa0 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')
a9c7198a 43 try:
82d02080 44 tc = testcases[int(num or 0)]
a9c7198a
PH
45 except IndexError:
46 raise ExtractorError(
82d02080 47 f'Test case {num or 0} not found, got only {len(testcases)} tests', expected=True)
a9c7198a 48
82d02080 49 self.to_screen(f'Test URL: {tc["url"]}')
812cdfa0 50 return self.url_result(tc['url'], extractor)