testurl.py 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import re
  2. from .common import InfoExtractor
  3. from ..utils import ExtractorError
  4. class TestURLIE(InfoExtractor):
  5. """ Allows addressing of the test cases as test:yout.*be_1 """
  6. IE_DESC = False # Do not list
  7. _VALID_URL = r'test(?:url)?:(?P<extractor>.*?)(?:_(?P<num>[0-9]+))?$'
  8. def _real_extract(self, url):
  9. from . import gen_extractor_classes
  10. extractor_id, num = self._match_valid_url(url).group('extractor', 'num')
  11. if not extractor_id:
  12. return {'id': ':test', 'title': '', 'url': url}
  13. rex = re.compile(extractor_id, flags=re.IGNORECASE)
  14. matching_extractors = [e for e in gen_extractor_classes() if rex.search(e.IE_NAME)]
  15. if len(matching_extractors) == 0:
  16. raise ExtractorError(f'No extractors matching {extractor_id!r} found', expected=True)
  17. elif len(matching_extractors) > 1:
  18. try: # Check for exact match
  19. extractor = next(
  20. ie for ie in matching_extractors
  21. if ie.IE_NAME.lower() == extractor_id.lower())
  22. except StopIteration:
  23. raise ExtractorError(
  24. 'Found multiple matching extractors: %s' % ' '.join(ie.IE_NAME for ie in matching_extractors),
  25. expected=True)
  26. else:
  27. extractor = matching_extractors[0]
  28. testcases = tuple(extractor.get_testcases(True))
  29. try:
  30. tc = testcases[int(num or 0)]
  31. except IndexError:
  32. raise ExtractorError(
  33. f'Test case {num or 0} not found, got only {len(testcases)} tests', expected=True)
  34. self.to_screen(f'Test URL: {tc["url"]}')
  35. return self.url_result(tc['url'])