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.

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