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.

206 lines
7.3 KiB

  1. #!/usr/bin/env python
  2. # Allow direct execution
  3. import os
  4. import sys
  5. import unittest
  6. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  7. from test.helper import FakeYDL, md5
  8. from youtube_dl.extractor import (
  9. YoutubeIE,
  10. DailymotionIE,
  11. TEDIE,
  12. )
  13. class BaseTestSubtitles(unittest.TestCase):
  14. url = None
  15. IE = None
  16. def setUp(self):
  17. self.DL = FakeYDL()
  18. self.ie = self.IE(self.DL)
  19. def getInfoDict(self):
  20. info_dict = self.ie.extract(self.url)
  21. return info_dict
  22. def getSubtitles(self):
  23. info_dict = self.getInfoDict()
  24. return info_dict['subtitles']
  25. class TestYoutubeSubtitles(BaseTestSubtitles):
  26. url = 'QRS8MkLhQmM'
  27. IE = YoutubeIE
  28. def test_youtube_no_writesubtitles(self):
  29. self.DL.params['writesubtitles'] = False
  30. subtitles = self.getSubtitles()
  31. self.assertEqual(subtitles, None)
  32. def test_youtube_subtitles(self):
  33. self.DL.params['writesubtitles'] = True
  34. subtitles = self.getSubtitles()
  35. self.assertEqual(md5(subtitles['en']), '4cd9278a35ba2305f47354ee13472260')
  36. def test_youtube_subtitles_lang(self):
  37. self.DL.params['writesubtitles'] = True
  38. self.DL.params['subtitleslangs'] = ['it']
  39. subtitles = self.getSubtitles()
  40. self.assertEqual(md5(subtitles['it']), '164a51f16f260476a05b50fe4c2f161d')
  41. def test_youtube_allsubtitles(self):
  42. self.DL.params['writesubtitles'] = True
  43. self.DL.params['allsubtitles'] = True
  44. subtitles = self.getSubtitles()
  45. self.assertEqual(len(subtitles.keys()), 13)
  46. def test_youtube_subtitles_sbv_format(self):
  47. self.DL.params['writesubtitles'] = True
  48. self.DL.params['subtitlesformat'] = 'sbv'
  49. subtitles = self.getSubtitles()
  50. self.assertEqual(md5(subtitles['en']), '13aeaa0c245a8bed9a451cb643e3ad8b')
  51. def test_youtube_subtitles_vtt_format(self):
  52. self.DL.params['writesubtitles'] = True
  53. self.DL.params['subtitlesformat'] = 'vtt'
  54. subtitles = self.getSubtitles()
  55. self.assertEqual(md5(subtitles['en']), '3cb210999d3e021bd6c7f0ea751eab06')
  56. def test_youtube_list_subtitles(self):
  57. self.DL.expect_warning(u'Video doesn\'t have automatic captions')
  58. self.DL.params['listsubtitles'] = True
  59. info_dict = self.getInfoDict()
  60. self.assertEqual(info_dict, None)
  61. def test_youtube_automatic_captions(self):
  62. self.url = '8YoUxe5ncPo'
  63. self.DL.params['writeautomaticsub'] = True
  64. self.DL.params['subtitleslangs'] = ['it']
  65. subtitles = self.getSubtitles()
  66. self.assertTrue(subtitles['it'] is not None)
  67. def test_youtube_nosubtitles(self):
  68. self.DL.expect_warning(u'video doesn\'t have subtitles')
  69. self.url = 'sAjKT8FhjI8'
  70. self.DL.params['writesubtitles'] = True
  71. self.DL.params['allsubtitles'] = True
  72. subtitles = self.getSubtitles()
  73. self.assertEqual(len(subtitles), 0)
  74. def test_youtube_multiple_langs(self):
  75. self.url = 'QRS8MkLhQmM'
  76. self.DL.params['writesubtitles'] = True
  77. langs = ['it', 'fr', 'de']
  78. self.DL.params['subtitleslangs'] = langs
  79. subtitles = self.getSubtitles()
  80. for lang in langs:
  81. self.assertTrue(subtitles.get(lang) is not None, u'Subtitles for \'%s\' not extracted' % lang)
  82. class TestDailymotionSubtitles(BaseTestSubtitles):
  83. url = 'http://www.dailymotion.com/video/xczg00'
  84. IE = DailymotionIE
  85. def test_no_writesubtitles(self):
  86. subtitles = self.getSubtitles()
  87. self.assertEqual(subtitles, None)
  88. def test_subtitles(self):
  89. self.DL.params['writesubtitles'] = True
  90. subtitles = self.getSubtitles()
  91. self.assertEqual(md5(subtitles['en']), '976553874490cba125086bbfea3ff76f')
  92. def test_subtitles_lang(self):
  93. self.DL.params['writesubtitles'] = True
  94. self.DL.params['subtitleslangs'] = ['fr']
  95. subtitles = self.getSubtitles()
  96. self.assertEqual(md5(subtitles['fr']), '594564ec7d588942e384e920e5341792')
  97. def test_allsubtitles(self):
  98. self.DL.params['writesubtitles'] = True
  99. self.DL.params['allsubtitles'] = True
  100. subtitles = self.getSubtitles()
  101. self.assertEqual(len(subtitles.keys()), 5)
  102. def test_list_subtitles(self):
  103. self.DL.expect_warning(u'Automatic Captions not supported by this server')
  104. self.DL.params['listsubtitles'] = True
  105. info_dict = self.getInfoDict()
  106. self.assertEqual(info_dict, None)
  107. def test_automatic_captions(self):
  108. self.DL.expect_warning(u'Automatic Captions not supported by this server')
  109. self.DL.params['writeautomaticsub'] = True
  110. self.DL.params['subtitleslang'] = ['en']
  111. subtitles = self.getSubtitles()
  112. self.assertTrue(len(subtitles.keys()) == 0)
  113. def test_nosubtitles(self):
  114. self.DL.expect_warning(u'video doesn\'t have subtitles')
  115. self.url = 'http://www.dailymotion.com/video/x12u166_le-zapping-tele-star-du-08-aout-2013_tv'
  116. self.DL.params['writesubtitles'] = True
  117. self.DL.params['allsubtitles'] = True
  118. subtitles = self.getSubtitles()
  119. self.assertEqual(len(subtitles), 0)
  120. def test_multiple_langs(self):
  121. self.DL.params['writesubtitles'] = True
  122. langs = ['es', 'fr', 'de']
  123. self.DL.params['subtitleslangs'] = langs
  124. subtitles = self.getSubtitles()
  125. for lang in langs:
  126. self.assertTrue(subtitles.get(lang) is not None, u'Subtitles for \'%s\' not extracted' % lang)
  127. class TestTedSubtitles(BaseTestSubtitles):
  128. url = 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html'
  129. IE = TEDIE
  130. def test_no_writesubtitles(self):
  131. subtitles = self.getSubtitles()
  132. self.assertEqual(subtitles, None)
  133. def test_subtitles(self):
  134. self.DL.params['writesubtitles'] = True
  135. subtitles = self.getSubtitles()
  136. self.assertEqual(md5(subtitles['en']), '2154f31ff9b9f89a0aa671537559c21d')
  137. def test_subtitles_lang(self):
  138. self.DL.params['writesubtitles'] = True
  139. self.DL.params['subtitleslangs'] = ['fr']
  140. subtitles = self.getSubtitles()
  141. self.assertEqual(md5(subtitles['fr']), '7616cbc6df20ec2c1204083c83871cf6')
  142. def test_allsubtitles(self):
  143. self.DL.params['writesubtitles'] = True
  144. self.DL.params['allsubtitles'] = True
  145. subtitles = self.getSubtitles()
  146. self.assertEqual(len(subtitles.keys()), 28)
  147. def test_list_subtitles(self):
  148. self.DL.expect_warning(u'Automatic Captions not supported by this server')
  149. self.DL.params['listsubtitles'] = True
  150. info_dict = self.getInfoDict()
  151. self.assertEqual(info_dict, None)
  152. def test_automatic_captions(self):
  153. self.DL.expect_warning(u'Automatic Captions not supported by this server')
  154. self.DL.params['writeautomaticsub'] = True
  155. self.DL.params['subtitleslang'] = ['en']
  156. subtitles = self.getSubtitles()
  157. self.assertTrue(len(subtitles.keys()) == 0)
  158. def test_multiple_langs(self):
  159. self.DL.params['writesubtitles'] = True
  160. langs = ['es', 'fr', 'de']
  161. self.DL.params['subtitleslangs'] = langs
  162. subtitles = self.getSubtitles()
  163. for lang in langs:
  164. self.assertTrue(subtitles.get(lang) is not None, u'Subtitles for \'%s\' not extracted' % lang)
  165. if __name__ == '__main__':
  166. unittest.main()