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.

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