]> Raphaƫl G. Git Repositories - youtubedl/blob - youtube_dl/extractor/testurl.py
Update upstream source from tag 'upstream/2018.03.14'
[youtubedl] / youtube_dl / extractor / testurl.py
1 from __future__ import unicode_literals
2
3 import re
4
5 from .common import InfoExtractor
6 from ..utils import ExtractorError
7
8
9 class TestURLIE(InfoExtractor):
10 """ Allows addressing of the test cases as test:yout.*be_1 """
11
12 IE_DESC = False # Do not list
13 _VALID_URL = r'test(?:url)?:(?P<id>(?P<extractor>.+?)(?:_(?P<num>[0-9]+))?)$'
14
15 def _real_extract(self, url):
16 from ..extractor import gen_extractors
17
18 mobj = re.match(self._VALID_URL, url)
19 video_id = mobj.group('id')
20 extractor_id = mobj.group('extractor')
21 all_extractors = gen_extractors()
22
23 rex = re.compile(extractor_id, flags=re.IGNORECASE)
24 matching_extractors = [
25 e for e in all_extractors if rex.search(e.IE_NAME)]
26
27 if len(matching_extractors) == 0:
28 raise ExtractorError(
29 'No extractors matching %r found' % extractor_id,
30 expected=True)
31 elif len(matching_extractors) > 1:
32 # Is it obvious which one to pick?
33 try:
34 extractor = next(
35 ie for ie in matching_extractors
36 if ie.IE_NAME.lower() == extractor_id.lower())
37 except StopIteration:
38 raise ExtractorError(
39 ('Found multiple matching extractors: %s' %
40 ' '.join(ie.IE_NAME for ie in matching_extractors)),
41 expected=True)
42 else:
43 extractor = matching_extractors[0]
44
45 num_str = mobj.group('num')
46 num = int(num_str) if num_str else 0
47
48 testcases = []
49 t = getattr(extractor, '_TEST', None)
50 if t:
51 testcases.append(t)
52 testcases.extend(getattr(extractor, '_TESTS', []))
53
54 try:
55 tc = testcases[num]
56 except IndexError:
57 raise ExtractorError(
58 ('Test case %d not found, got only %d tests' %
59 (num, len(testcases))),
60 expected=True)
61
62 self.to_screen('Test URL: %s' % tc['url'])
63
64 return {
65 '_type': 'url',
66 'url': tc['url'],
67 'id': video_id,
68 }