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.

95 lines
3.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
  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 helper import FakeYDL
  13. md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest()
  14. class TestYoutubeSubtitles(unittest.TestCase):
  15. def setUp(self):
  16. DL = FakeYDL()
  17. DL.params['allsubtitles'] = False
  18. DL.params['writesubtitles'] = False
  19. DL.params['subtitlesformat'] = 'srt'
  20. DL.params['listsubtitles'] = False
  21. def test_youtube_no_subtitles(self):
  22. DL = FakeYDL()
  23. DL.params['writesubtitles'] = False
  24. IE = YoutubeIE(DL)
  25. info_dict = IE.extract('QRS8MkLhQmM')
  26. subtitles = info_dict[0]['subtitles']
  27. self.assertEqual(subtitles, None)
  28. def test_youtube_subtitles(self):
  29. DL = FakeYDL()
  30. DL.params['writesubtitles'] = True
  31. IE = YoutubeIE(DL)
  32. info_dict = IE.extract('QRS8MkLhQmM')
  33. sub = info_dict[0]['subtitles'][0]
  34. self.assertEqual(md5(sub[2]), '4cd9278a35ba2305f47354ee13472260')
  35. def test_youtube_subtitles_it(self):
  36. DL = FakeYDL()
  37. DL.params['writesubtitles'] = True
  38. DL.params['subtitleslang'] = 'it'
  39. IE = YoutubeIE(DL)
  40. info_dict = IE.extract('QRS8MkLhQmM')
  41. sub = info_dict[0]['subtitles'][0]
  42. self.assertEqual(md5(sub[2]), '164a51f16f260476a05b50fe4c2f161d')
  43. def test_youtube_onlysubtitles(self):
  44. DL = FakeYDL()
  45. DL.params['writesubtitles'] = True
  46. DL.params['onlysubtitles'] = True
  47. IE = YoutubeIE(DL)
  48. info_dict = IE.extract('QRS8MkLhQmM')
  49. sub = info_dict[0]['subtitles'][0]
  50. self.assertEqual(md5(sub[2]), '4cd9278a35ba2305f47354ee13472260')
  51. def test_youtube_allsubtitles(self):
  52. DL = FakeYDL()
  53. DL.params['allsubtitles'] = True
  54. IE = YoutubeIE(DL)
  55. info_dict = IE.extract('QRS8MkLhQmM')
  56. subtitles = info_dict[0]['subtitles']
  57. self.assertEqual(len(subtitles), 13)
  58. def test_youtube_subtitles_sbv_format(self):
  59. DL = FakeYDL()
  60. DL.params['writesubtitles'] = True
  61. DL.params['subtitlesformat'] = 'sbv'
  62. IE = YoutubeIE(DL)
  63. info_dict = IE.extract('QRS8MkLhQmM')
  64. sub = info_dict[0]['subtitles'][0]
  65. self.assertEqual(md5(sub[2]), '13aeaa0c245a8bed9a451cb643e3ad8b')
  66. def test_youtube_subtitles_vtt_format(self):
  67. DL = FakeYDL()
  68. DL.params['writesubtitles'] = True
  69. DL.params['subtitlesformat'] = 'vtt'
  70. IE = YoutubeIE(DL)
  71. info_dict = IE.extract('QRS8MkLhQmM')
  72. sub = info_dict[0]['subtitles'][0]
  73. self.assertEqual(md5(sub[2]), '356cdc577fde0c6783b9b822e7206ff7')
  74. def test_youtube_list_subtitles(self):
  75. DL = FakeYDL()
  76. DL.params['listsubtitles'] = True
  77. IE = YoutubeIE(DL)
  78. info_dict = IE.extract('QRS8MkLhQmM')
  79. self.assertEqual(info_dict, None)
  80. def test_youtube_automatic_captions(self):
  81. DL = FakeYDL()
  82. DL.params['writeautomaticsub'] = True
  83. DL.params['subtitleslang'] = 'it'
  84. IE = YoutubeIE(DL)
  85. info_dict = IE.extract('8YoUxe5ncPo')
  86. sub = info_dict[0]['subtitles'][0]
  87. self.assertTrue(sub[2] is not None)
  88. if __name__ == '__main__':
  89. unittest.main()