logo

youtube-dl

[mirror] Download/Watch videos from video hostersgit clone https://hacktivis.me/git/mirror/youtube-dl.git

testurl.py (2111B)


  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 addressing 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. else:
  34. extractor = matching_extractors[0]
  35. num_str = mobj.group('num')
  36. num = int(num_str) if num_str else 0
  37. testcases = []
  38. t = getattr(extractor, '_TEST', None)
  39. if t:
  40. testcases.append(t)
  41. testcases.extend(getattr(extractor, '_TESTS', []))
  42. try:
  43. tc = testcases[num]
  44. except IndexError:
  45. raise ExtractorError(
  46. ('Test case %d not found, got only %d tests' %
  47. (num, len(testcases))),
  48. expected=True)
  49. self.to_screen('Test URL: %s' % tc['url'])
  50. return self.url_result(tc['url'], video_id=video_id)