[testurl] Add extractor
[youtube-dl] / 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 adressing 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
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 {
63             '_type': 'url',
64             'url': tc['url'],
65             'id': video_id,
66         }