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.

119 lines
4.3 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_sbv_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_subtitles_vtt_format(self):
  88. DL = FakeYDL()
  89. DL.params['writesubtitles'] = True
  90. DL.params['subtitlesformat'] = 'vtt'
  91. IE = YoutubeIE(DL)
  92. info_dict = IE.extract('QRS8MkLhQmM')
  93. sub = info_dict[0]['subtitles'][0]
  94. self.assertEqual(md5(sub[2]), '356cdc577fde0c6783b9b822e7206ff7')
  95. def test_youtube_list_subtitles(self):
  96. DL = FakeYDL()
  97. DL.params['listsubtitles'] = True
  98. IE = YoutubeIE(DL)
  99. info_dict = IE.extract('QRS8MkLhQmM')
  100. self.assertEqual(info_dict, None)
  101. def test_youtube_automatic_captions(self):
  102. DL = FakeYDL()
  103. DL.params['writeautomaticsub'] = True
  104. DL.params['subtitleslang'] = 'it'
  105. IE = YoutubeIE(DL)
  106. info_dict = IE.extract('8YoUxe5ncPo')
  107. sub = info_dict[0]['subtitles'][0]
  108. self.assertTrue(sub[2] is not None)
  109. if __name__ == '__main__':
  110. unittest.main()