diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 16:49:24 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 16:49:24 +0000 |
commit | 2415e66f889f38503b73e8ebc5f43ca342390e5c (patch) | |
tree | ac48ab69d1d96bae3d83756134921e0d90593aa5 /yt_dlp/extractor/testurl.py | |
parent | Initial commit. (diff) | |
download | yt-dlp-2415e66f889f38503b73e8ebc5f43ca342390e5c.tar.xz yt-dlp-2415e66f889f38503b73e8ebc5f43ca342390e5c.zip |
Adding upstream version 2024.03.10.upstream/2024.03.10
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'yt_dlp/extractor/testurl.py')
-rw-r--r-- | yt_dlp/extractor/testurl.py | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/yt_dlp/extractor/testurl.py b/yt_dlp/extractor/testurl.py new file mode 100644 index 0000000..3cf0017 --- /dev/null +++ b/yt_dlp/extractor/testurl.py @@ -0,0 +1,50 @@ +import re + +from .common import InfoExtractor +from ..utils import ExtractorError + + +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<extractor>.*?)(?:_(?P<num>\d+|all))?$' + + def _real_extract(self, url): + from . import gen_extractor_classes + + 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 gen_extractor_classes() if rex.search(e.IE_NAME)] + + if len(matching_extractors) == 0: + raise ExtractorError(f'No extractors matching {extractor_id!r} found', expected=True) + elif len(matching_extractors) > 1: + 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), + expected=True) + else: + extractor = matching_extractors[0] + + 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[int(num or 0)] + except IndexError: + raise ExtractorError( + f'Test case {num or 0} not found, got only {len(testcases)} tests', expected=True) + + self.to_screen(f'Test URL: {tc["url"]}') + return self.url_result(tc['url'], extractor) |