helper.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import errno
  2. import io
  3. import hashlib
  4. import json
  5. import os.path
  6. import re
  7. import types
  8. import sys
  9. import youtube_dl.extractor
  10. from youtube_dl import YoutubeDL
  11. from youtube_dl.utils import preferredencoding
  12. def global_setup():
  13. youtube_dl._setup_opener(timeout=10)
  14. def get_params(override=None):
  15. PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)),
  16. "parameters.json")
  17. with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
  18. parameters = json.load(pf)
  19. if override:
  20. parameters.update(override)
  21. return parameters
  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. def report_warning(message):
  30. '''
  31. Print the message to stderr, it will be prefixed with 'WARNING:'
  32. If stderr is a tty file the 'WARNING:' will be colored
  33. '''
  34. if sys.stderr.isatty() and os.name != 'nt':
  35. _msg_header = u'\033[0;33mWARNING:\033[0m'
  36. else:
  37. _msg_header = u'WARNING:'
  38. output = u'%s %s\n' % (_msg_header, message)
  39. if 'b' in getattr(sys.stderr, 'mode', '') or sys.version_info[0] < 3:
  40. output = output.encode(preferredencoding())
  41. sys.stderr.write(output)
  42. class FakeYDL(YoutubeDL):
  43. def __init__(self, override=None):
  44. # Different instances of the downloader can't share the same dictionary
  45. # some test set the "sublang" parameter, which would break the md5 checks.
  46. params = get_params(override=override)
  47. super(FakeYDL, self).__init__(params)
  48. self.result = []
  49. def to_screen(self, s, skip_eol=None):
  50. print(s)
  51. def trouble(self, s, tb=None):
  52. raise Exception(s)
  53. def download(self, x):
  54. self.result.append(x)
  55. def expect_warning(self, regex):
  56. # Silence an expected warning matching a regex
  57. old_report_warning = self.report_warning
  58. def report_warning(self, message):
  59. if re.match(regex, message): return
  60. old_report_warning(message)
  61. self.report_warning = types.MethodType(report_warning, self)
  62. def get_testcases():
  63. for ie in youtube_dl.extractor.gen_extractors():
  64. t = getattr(ie, '_TEST', None)
  65. if t:
  66. t['name'] = type(ie).__name__[:-len('IE')]
  67. yield t
  68. for t in getattr(ie, '_TESTS', []):
  69. t['name'] = type(ie).__name__[:-len('IE')]
  70. yield t
  71. md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest()