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.

283 lines
10 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. VimeoIE,
  14. )
  15. class BaseTestSubtitles(unittest.TestCase):
  16. url = None
  17. IE = None
  18. def setUp(self):
  19. self.DL = FakeYDL()
  20. self.ie = self.IE(self.DL)
  21. def getInfoDict(self):
  22. info_dict = self.ie.extract(self.url)
  23. return info_dict
  24. def getSubtitles(self):
  25. info_dict = self.getInfoDict()
  26. return info_dict['subtitles']
  27. class TestYoutubeSubtitles(BaseTestSubtitles):
  28. url = 'QRS8MkLhQmM'
  29. IE = YoutubeIE
  30. def test_youtube_no_writesubtitles(self):
  31. self.DL.params['writesubtitles'] = False
  32. subtitles = self.getSubtitles()
  33. self.assertEqual(subtitles, None)
  34. def test_youtube_subtitles(self):
  35. self.DL.params['writesubtitles'] = True
  36. subtitles = self.getSubtitles()
  37. self.assertEqual(md5(subtitles['en']), '4cd9278a35ba2305f47354ee13472260')
  38. def test_youtube_subtitles_lang(self):
  39. self.DL.params['writesubtitles'] = True
  40. self.DL.params['subtitleslangs'] = ['it']
  41. subtitles = self.getSubtitles()
  42. self.assertEqual(md5(subtitles['it']), '164a51f16f260476a05b50fe4c2f161d')
  43. def test_youtube_allsubtitles(self):
  44. self.DL.params['writesubtitles'] = True
  45. self.DL.params['allsubtitles'] = True
  46. subtitles = self.getSubtitles()
  47. self.assertEqual(len(subtitles.keys()), 13)
  48. def test_youtube_subtitles_sbv_format(self):
  49. self.DL.params['writesubtitles'] = True
  50. self.DL.params['subtitlesformat'] = 'sbv'
  51. subtitles = self.getSubtitles()
  52. self.assertEqual(md5(subtitles['en']), '13aeaa0c245a8bed9a451cb643e3ad8b')
  53. def test_youtube_subtitles_vtt_format(self):
  54. self.DL.params['writesubtitles'] = True
  55. self.DL.params['subtitlesformat'] = 'vtt'
  56. subtitles = self.getSubtitles()
  57. self.assertEqual(md5(subtitles['en']), '3cb210999d3e021bd6c7f0ea751eab06')
  58. def test_youtube_list_subtitles(self):
  59. self.DL.expect_warning(u'Video doesn\'t have automatic captions')
  60. self.DL.params['listsubtitles'] = True
  61. info_dict = self.getInfoDict()
  62. self.assertEqual(info_dict, None)
  63. def test_youtube_automatic_captions(self):
  64. self.url = '8YoUxe5ncPo'
  65. self.DL.params['writeautomaticsub'] = True
  66. self.DL.params['subtitleslangs'] = ['it']
  67. subtitles = self.getSubtitles()
  68. self.assertTrue(subtitles['it'] is not None)
  69. def test_youtube_nosubtitles(self):
  70. self.DL.expect_warning(u'video doesn\'t have subtitles')
  71. self.url = 'sAjKT8FhjI8'
  72. self.DL.params['writesubtitles'] = True
  73. self.DL.params['allsubtitles'] = True
  74. subtitles = self.getSubtitles()
  75. self.assertEqual(len(subtitles), 0)
  76. def test_youtube_multiple_langs(self):
  77. self.url = 'QRS8MkLhQmM'
  78. self.DL.params['writesubtitles'] = True
  79. langs = ['it', 'fr', 'de']
  80. self.DL.params['subtitleslangs'] = langs
  81. subtitles = self.getSubtitles()
  82. for lang in langs:
  83. self.assertTrue(subtitles.get(lang) is not None, u'Subtitles for \'%s\' not extracted' % lang)
  84. class TestDailymotionSubtitles(BaseTestSubtitles):
  85. url = 'http://www.dailymotion.com/video/xczg00'
  86. IE = DailymotionIE
  87. def test_no_writesubtitles(self):
  88. subtitles = self.getSubtitles()
  89. self.assertEqual(subtitles, None)
  90. def test_subtitles(self):
  91. self.DL.params['writesubtitles'] = True
  92. subtitles = self.getSubtitles()
  93. self.assertEqual(md5(subtitles['en']), '976553874490cba125086bbfea3ff76f')
  94. def test_subtitles_lang(self):
  95. self.DL.params['writesubtitles'] = True
  96. self.DL.params['subtitleslangs'] = ['fr']
  97. subtitles = self.getSubtitles()
  98. self.assertEqual(md5(subtitles['fr']), '594564ec7d588942e384e920e5341792')
  99. def test_allsubtitles(self):
  100. self.DL.params['writesubtitles'] = True
  101. self.DL.params['allsubtitles'] = True
  102. subtitles = self.getSubtitles()
  103. self.assertEqual(len(subtitles.keys()), 5)
  104. def test_list_subtitles(self):
  105. self.DL.expect_warning(u'Automatic Captions not supported by this server')
  106. self.DL.params['listsubtitles'] = True
  107. info_dict = self.getInfoDict()
  108. self.assertEqual(info_dict, None)
  109. def test_automatic_captions(self):
  110. self.DL.expect_warning(u'Automatic Captions not supported by this server')
  111. self.DL.params['writeautomaticsub'] = True
  112. self.DL.params['subtitleslang'] = ['en']
  113. subtitles = self.getSubtitles()
  114. self.assertTrue(len(subtitles.keys()) == 0)
  115. def test_nosubtitles(self):
  116. self.DL.expect_warning(u'video doesn\'t have subtitles')
  117. self.url = 'http://www.dailymotion.com/video/x12u166_le-zapping-tele-star-du-08-aout-2013_tv'
  118. self.DL.params['writesubtitles'] = True
  119. self.DL.params['allsubtitles'] = True
  120. subtitles = self.getSubtitles()
  121. self.assertEqual(len(subtitles), 0)
  122. def test_multiple_langs(self):
  123. self.DL.params['writesubtitles'] = True
  124. langs = ['es', 'fr', 'de']
  125. self.DL.params['subtitleslangs'] = langs
  126. subtitles = self.getSubtitles()
  127. for lang in langs:
  128. self.assertTrue(subtitles.get(lang) is not None, u'Subtitles for \'%s\' not extracted' % lang)
  129. class TestTedSubtitles(BaseTestSubtitles):
  130. url = 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html'
  131. IE = TEDIE
  132. def test_no_writesubtitles(self):
  133. subtitles = self.getSubtitles()
  134. self.assertEqual(subtitles, None)
  135. def test_subtitles(self):
  136. self.DL.params['writesubtitles'] = True
  137. subtitles = self.getSubtitles()
  138. self.assertEqual(md5(subtitles['en']), '4262c1665ff928a2dada178f62cb8d14')
  139. def test_subtitles_lang(self):
  140. self.DL.params['writesubtitles'] = True
  141. self.DL.params['subtitleslangs'] = ['fr']
  142. subtitles = self.getSubtitles()
  143. self.assertEqual(md5(subtitles['fr']), '66a63f7f42c97a50f8c0e90bc7797bb5')
  144. def test_allsubtitles(self):
  145. self.DL.params['writesubtitles'] = True
  146. self.DL.params['allsubtitles'] = True
  147. subtitles = self.getSubtitles()
  148. self.assertTrue(len(subtitles.keys()) >= 28)
  149. def test_list_subtitles(self):
  150. self.DL.expect_warning(u'Automatic Captions not supported by this server')
  151. self.DL.params['listsubtitles'] = True
  152. info_dict = self.getInfoDict()
  153. self.assertEqual(info_dict, None)
  154. def test_automatic_captions(self):
  155. self.DL.expect_warning(u'Automatic Captions not supported by this server')
  156. self.DL.params['writeautomaticsub'] = True
  157. self.DL.params['subtitleslang'] = ['en']
  158. subtitles = self.getSubtitles()
  159. self.assertTrue(len(subtitles.keys()) == 0)
  160. def test_multiple_langs(self):
  161. self.DL.params['writesubtitles'] = True
  162. langs = ['es', 'fr', 'de']
  163. self.DL.params['subtitleslangs'] = langs
  164. subtitles = self.getSubtitles()
  165. for lang in langs:
  166. self.assertTrue(subtitles.get(lang) is not None, u'Subtitles for \'%s\' not extracted' % lang)
  167. class TestBlipTVSubtitles(BaseTestSubtitles):
  168. url = 'http://blip.tv/a/a-6603250'
  169. IE = BlipTVIE
  170. def test_list_subtitles(self):
  171. self.DL.expect_warning(u'Automatic Captions not supported by this server')
  172. self.DL.params['listsubtitles'] = True
  173. info_dict = self.getInfoDict()
  174. self.assertEqual(info_dict, None)
  175. def test_allsubtitles(self):
  176. self.DL.expect_warning(u'Automatic Captions not supported by this server')
  177. self.DL.params['writesubtitles'] = True
  178. self.DL.params['allsubtitles'] = True
  179. subtitles = self.getSubtitles()
  180. self.assertEqual(set(subtitles.keys()), set(['en']))
  181. self.assertEqual(md5(subtitles['en']), '5b75c300af65fe4476dff79478bb93e4')
  182. class TestVimeoSubtitles(BaseTestSubtitles):
  183. url = 'http://vimeo.com/76979871'
  184. IE = VimeoIE
  185. def test_no_writesubtitles(self):
  186. subtitles = self.getSubtitles()
  187. self.assertEqual(subtitles, None)
  188. def test_subtitles(self):
  189. self.DL.params['writesubtitles'] = True
  190. subtitles = self.getSubtitles()
  191. self.assertEqual(md5(subtitles['en']), '8062383cf4dec168fc40a088aa6d5888')
  192. def test_subtitles_lang(self):
  193. self.DL.params['writesubtitles'] = True
  194. self.DL.params['subtitleslangs'] = ['fr']
  195. subtitles = self.getSubtitles()
  196. self.assertEqual(md5(subtitles['fr']), 'b6191146a6c5d3a452244d853fde6dc8')
  197. def test_allsubtitles(self):
  198. self.DL.params['writesubtitles'] = True
  199. self.DL.params['allsubtitles'] = True
  200. subtitles = self.getSubtitles()
  201. self.assertEqual(set(subtitles.keys()), set(['de', 'en', 'es', 'fr']))
  202. def test_list_subtitles(self):
  203. self.DL.expect_warning(u'Automatic Captions not supported by this server')
  204. self.DL.params['listsubtitles'] = True
  205. info_dict = self.getInfoDict()
  206. self.assertEqual(info_dict, None)
  207. def test_automatic_captions(self):
  208. self.DL.expect_warning(u'Automatic Captions not supported by this server')
  209. self.DL.params['writeautomaticsub'] = True
  210. self.DL.params['subtitleslang'] = ['en']
  211. subtitles = self.getSubtitles()
  212. self.assertTrue(len(subtitles.keys()) == 0)
  213. def test_nosubtitles(self):
  214. self.DL.expect_warning(u'video doesn\'t have subtitles')
  215. self.url = 'http://vimeo.com/56015672'
  216. self.DL.params['writesubtitles'] = True
  217. self.DL.params['allsubtitles'] = True
  218. subtitles = self.getSubtitles()
  219. self.assertEqual(len(subtitles), 0)
  220. def test_multiple_langs(self):
  221. self.DL.params['writesubtitles'] = True
  222. langs = ['es', 'fr', 'de']
  223. self.DL.params['subtitleslangs'] = langs
  224. subtitles = self.getSubtitles()
  225. for lang in langs:
  226. self.assertTrue(subtitles.get(lang) is not None, u'Subtitles for \'%s\' not extracted' % lang)
  227. if __name__ == '__main__':
  228. unittest.main()