]> jfr.im git - yt-dlp.git/blob - yt_dlp/extractor/testurl.py
[spotify] Detect iframe embeds (#3430)
[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<id>(?P<extractor>.+?)(?:_(?P<num>[0-9]+))?)$'
12
13 def _real_extract(self, url):
14 from ..extractor import gen_extractors
15
16 mobj = self._match_valid_url(url)
17 video_id = mobj.group('id')
18 extractor_id = mobj.group('extractor')
19 all_extractors = gen_extractors()
20
21 rex = re.compile(extractor_id, flags=re.IGNORECASE)
22 matching_extractors = [
23 e for e in all_extractors if rex.search(e.IE_NAME)]
24
25 if len(matching_extractors) == 0:
26 raise ExtractorError(
27 'No extractors matching %r found' % extractor_id,
28 expected=True)
29 elif len(matching_extractors) > 1:
30 # Is it obvious which one to pick?
31 try:
32 extractor = next(
33 ie for ie in matching_extractors
34 if ie.IE_NAME.lower() == extractor_id.lower())
35 except StopIteration:
36 raise ExtractorError(
37 ('Found multiple matching extractors: %s' %
38 ' '.join(ie.IE_NAME for ie in matching_extractors)),
39 expected=True)
40 else:
41 extractor = matching_extractors[0]
42
43 num_str = mobj.group('num')
44 num = int(num_str) if num_str else 0
45
46 testcases = []
47 t = getattr(extractor, '_TEST', None)
48 if t:
49 testcases.append(t)
50 testcases.extend(getattr(extractor, '_TESTS', []))
51
52 try:
53 tc = testcases[num]
54 except IndexError:
55 raise ExtractorError(
56 ('Test case %d not found, got only %d tests' %
57 (num, len(testcases))),
58 expected=True)
59
60 self.to_screen('Test URL: %s' % tc['url'])
61
62 return self.url_result(tc['url'], video_id=video_id)