testurl.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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>\d+|all))?$'
  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. extractor = next(( # Check for exact match
  19. ie for ie in matching_extractors if ie.IE_NAME.lower() == extractor_id.lower()
  20. ), None) or next(( # Check for exact match without plugin suffix
  21. ie for ie in matching_extractors if ie.IE_NAME.split('+')[0].lower() == extractor_id.lower()
  22. ), None)
  23. if not extractor:
  24. raise ExtractorError(
  25. 'Found multiple matching extractors: {}'.format(' '.join(ie.IE_NAME for ie in matching_extractors)),
  26. expected=True)
  27. else:
  28. extractor = matching_extractors[0]
  29. testcases = tuple(extractor.get_testcases(True))
  30. if num == 'all':
  31. return self.playlist_result(
  32. [self.url_result(tc['url'], extractor) for tc in testcases],
  33. url, f'{extractor.IE_NAME} tests')
  34. try:
  35. tc = testcases[int(num or 0)]
  36. except IndexError:
  37. raise ExtractorError(
  38. f'Test case {num or 0} not found, got only {len(testcases)} tests', expected=True)
  39. self.to_screen(f'Test URL: {tc["url"]}')
  40. return self.url_result(tc['url'], extractor)