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.

72 lines
1.9 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. import errno
  2. import io
  3. import hashlib
  4. import json
  5. import os.path
  6. import re
  7. import types
  8. import youtube_dl.extractor
  9. from youtube_dl import YoutubeDL
  10. def global_setup():
  11. youtube_dl._setup_opener(timeout=10)
  12. def get_params(override=None):
  13. PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)),
  14. "parameters.json")
  15. with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
  16. parameters = json.load(pf)
  17. if override:
  18. parameters.update(override)
  19. return parameters
  20. def try_rm(filename):
  21. """ Remove a file if it exists """
  22. try:
  23. os.remove(filename)
  24. except OSError as ose:
  25. if ose.errno != errno.ENOENT:
  26. raise
  27. class FakeYDL(YoutubeDL):
  28. def __init__(self):
  29. # Different instances of the downloader can't share the same dictionary
  30. # some test set the "sublang" parameter, which would break the md5 checks.
  31. params = get_params()
  32. super(FakeYDL, self).__init__(params)
  33. self.result = []
  34. def to_screen(self, s, skip_eol=None):
  35. print(s)
  36. def trouble(self, s, tb=None):
  37. raise Exception(s)
  38. def download(self, x):
  39. self.result.append(x)
  40. def expect_warning(self, regex):
  41. # Silence an expected warning matching a regex
  42. old_report_warning = self.report_warning
  43. def report_warning(self, message):
  44. if re.match(regex, message): return
  45. old_report_warning(message)
  46. self.report_warning = types.MethodType(report_warning, self)
  47. def get_testcases():
  48. for ie in youtube_dl.extractor.gen_extractors():
  49. t = getattr(ie, '_TEST', None)
  50. if t:
  51. t['name'] = type(ie).__name__[:-len('IE')]
  52. yield t
  53. for t in getattr(ie, '_TESTS', []):
  54. t['name'] = type(ie).__name__[:-len('IE')]
  55. yield t
  56. md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest()