You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
2.1 KiB

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