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.

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