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.

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