helper.py 2.1 KB

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