aboutsummaryrefslogtreecommitdiffstats
path: root/hypervideo_dl/extractor/testurl.py
blob: dccca100467315302e8e1a4cf4d0696696419fdf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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>[0-9]+))?$'

    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:
            try:  # Check for exact match
                extractor = next(
                    ie for ie in matching_extractors
                    if ie.IE_NAME.lower() == extractor_id.lower())
            except StopIteration:
                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))
        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'])