helper.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import io
  2. import json
  3. import os.path
  4. import re
  5. import types
  6. import youtube_dl.extractor
  7. from youtube_dl import YoutubeDL, YoutubeDLHandler
  8. from youtube_dl.utils import (
  9. compat_cookiejar,
  10. compat_urllib_request,
  11. )
  12. # General configuration (from __init__, not very elegant...)
  13. jar = compat_cookiejar.CookieJar()
  14. cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
  15. proxy_handler = compat_urllib_request.ProxyHandler()
  16. opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
  17. compat_urllib_request.install_opener(opener)
  18. PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
  19. with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
  20. parameters = json.load(pf)
  21. class FakeYDL(YoutubeDL):
  22. def __init__(self):
  23. self.result = []
  24. # Different instances of the downloader can't share the same dictionary
  25. # some test set the "sublang" parameter, which would break the md5 checks.
  26. self.params = dict(parameters)
  27. def to_screen(self, s):
  28. print(s)
  29. def trouble(self, s, tb=None):
  30. raise Exception(s)
  31. def download(self, x):
  32. self.result.append(x)
  33. # def expect_warning(self, regex):
  34. # # Silence an expected warning matching a regex
  35. # def report_warning(self, message):
  36. # if re.match(regex, message): return
  37. # super(FakeYDL, self).report_warning(regex)
  38. # self.report_warning = types.MethodType(report_warning, self)
  39. def expect_warning(self, regex):
  40. # Silence an expected warning matching a regex
  41. old_report_warning = self.report_warning
  42. def report_warning(self, message):
  43. if re.match(regex, message): return
  44. old_report_warning(message)
  45. self.report_warning = types.MethodType(report_warning, self)
  46. def get_testcases():
  47. for ie in youtube_dl.extractor.gen_extractors():
  48. t = getattr(ie, '_TEST', None)
  49. if t:
  50. t['name'] = type(ie).__name__[:-len('IE')]
  51. yield t
  52. for t in getattr(ie, '_TESTS', []):
  53. t['name'] = type(ie).__name__[:-len('IE')]
  54. yield t