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.

111 lines
4.0 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
12 years ago
  1. #!/usr/bin/env python
  2. import sys
  3. import unittest
  4. import json
  5. import io
  6. import hashlib
  7. # Allow direct execution
  8. import os
  9. sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  10. from youtube_dl.extractor import YoutubeIE
  11. from youtube_dl.utils import *
  12. from youtube_dl import YoutubeDL
  13. PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
  14. with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
  15. parameters = json.load(pf)
  16. # General configuration (from __init__, not very elegant...)
  17. jar = compat_cookiejar.CookieJar()
  18. cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
  19. proxy_handler = compat_urllib_request.ProxyHandler()
  20. opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
  21. compat_urllib_request.install_opener(opener)
  22. class FakeYDL(YoutubeDL):
  23. def __init__(self):
  24. self.result = []
  25. # Different instances of the downloader can't share the same dictionary
  26. # some test set the "sublang" parameter, which would break the md5 checks.
  27. self.params = dict(parameters)
  28. def to_screen(self, s):
  29. print(s)
  30. def trouble(self, s, tb=None):
  31. raise Exception(s)
  32. def download(self, x):
  33. self.result.append(x)
  34. md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest()
  35. class TestYoutubeSubtitles(unittest.TestCase):
  36. def setUp(self):
  37. DL = FakeYDL()
  38. DL.params['allsubtitles'] = False
  39. DL.params['writesubtitles'] = False
  40. DL.params['subtitlesformat'] = 'srt'
  41. DL.params['listsubtitles'] = False
  42. def test_youtube_no_subtitles(self):
  43. DL = FakeYDL()
  44. DL.params['writesubtitles'] = False
  45. IE = YoutubeIE(DL)
  46. info_dict = IE.extract('QRS8MkLhQmM')
  47. subtitles = info_dict[0]['subtitles']
  48. self.assertEqual(subtitles, None)
  49. def test_youtube_subtitles(self):
  50. DL = FakeYDL()
  51. DL.params['writesubtitles'] = True
  52. IE = YoutubeIE(DL)
  53. info_dict = IE.extract('QRS8MkLhQmM')
  54. sub = info_dict[0]['subtitles'][0]
  55. self.assertEqual(md5(sub[2]), '4cd9278a35ba2305f47354ee13472260')
  56. def test_youtube_subtitles_it(self):
  57. DL = FakeYDL()
  58. DL.params['writesubtitles'] = True
  59. DL.params['subtitleslang'] = 'it'
  60. IE = YoutubeIE(DL)
  61. info_dict = IE.extract('QRS8MkLhQmM')
  62. sub = info_dict[0]['subtitles'][0]
  63. self.assertEqual(md5(sub[2]), '164a51f16f260476a05b50fe4c2f161d')
  64. def test_youtube_onlysubtitles(self):
  65. DL = FakeYDL()
  66. DL.params['writesubtitles'] = True
  67. DL.params['onlysubtitles'] = True
  68. IE = YoutubeIE(DL)
  69. info_dict = IE.extract('QRS8MkLhQmM')
  70. sub = info_dict[0]['subtitles'][0]
  71. self.assertEqual(md5(sub[2]), '4cd9278a35ba2305f47354ee13472260')
  72. def test_youtube_allsubtitles(self):
  73. DL = FakeYDL()
  74. DL.params['allsubtitles'] = True
  75. IE = YoutubeIE(DL)
  76. info_dict = IE.extract('QRS8MkLhQmM')
  77. subtitles = info_dict[0]['subtitles']
  78. self.assertEqual(len(subtitles), 13)
  79. def test_youtube_subtitles_format(self):
  80. DL = FakeYDL()
  81. DL.params['writesubtitles'] = True
  82. DL.params['subtitlesformat'] = 'sbv'
  83. IE = YoutubeIE(DL)
  84. info_dict = IE.extract('QRS8MkLhQmM')
  85. sub = info_dict[0]['subtitles'][0]
  86. self.assertEqual(md5(sub[2]), '13aeaa0c245a8bed9a451cb643e3ad8b')
  87. def test_youtube_list_subtitles(self):
  88. DL = FakeYDL()
  89. DL.params['listsubtitles'] = True
  90. IE = YoutubeIE(DL)
  91. info_dict = IE.extract('QRS8MkLhQmM')
  92. self.assertEqual(info_dict, None)
  93. def test_youtube_automatic_captions(self):
  94. DL = FakeYDL()
  95. DL.params['writeautomaticsub'] = True
  96. DL.params['subtitleslang'] = 'it'
  97. IE = YoutubeIE(DL)
  98. info_dict = IE.extract('8YoUxe5ncPo')
  99. sub = info_dict[0]['subtitles'][0]
  100. self.assertTrue(sub[2] is not None)
  101. if __name__ == '__main__':
  102. unittest.main()