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.

321 lines
12 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. #!/usr/bin/env python
  2. from __future__ import unicode_literals
  3. # Allow direct execution
  4. import os
  5. import sys
  6. import unittest
  7. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  8. from test.helper import FakeYDL, md5
  9. from youtube_dl.extractor import (
  10. BlipTVIE,
  11. YoutubeIE,
  12. DailymotionIE,
  13. TEDIE,
  14. VimeoIE,
  15. WallaIE,
  16. )
  17. class BaseTestSubtitles(unittest.TestCase):
  18. url = None
  19. IE = None
  20. def setUp(self):
  21. self.DL = FakeYDL()
  22. self.ie = self.IE(self.DL)
  23. def getInfoDict(self):
  24. info_dict = self.ie.extract(self.url)
  25. return info_dict
  26. def getSubtitles(self):
  27. info_dict = self.getInfoDict()
  28. return info_dict['subtitles']
  29. class TestYoutubeSubtitles(BaseTestSubtitles):
  30. url = 'QRS8MkLhQmM'
  31. IE = YoutubeIE
  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']), '3cb210999d3e021bd6c7f0ea751eab06')
  60. def test_youtube_list_subtitles(self):
  61. self.DL.expect_warning('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_translated_subtitles(self):
  72. # This video has a subtitles track, which can be translated
  73. self.url = 'Ky9eprVWzlI'
  74. self.DL.params['writeautomaticsub'] = True
  75. self.DL.params['subtitleslangs'] = ['it']
  76. subtitles = self.getSubtitles()
  77. self.assertTrue(subtitles['it'] is not None)
  78. def test_youtube_nosubtitles(self):
  79. self.DL.expect_warning('video doesn\'t have subtitles')
  80. self.url = 'n5BB19UTcdA'
  81. self.DL.params['writesubtitles'] = True
  82. self.DL.params['allsubtitles'] = True
  83. subtitles = self.getSubtitles()
  84. self.assertEqual(len(subtitles), 0)
  85. def test_youtube_multiple_langs(self):
  86. self.url = 'QRS8MkLhQmM'
  87. self.DL.params['writesubtitles'] = True
  88. langs = ['it', 'fr', 'de']
  89. self.DL.params['subtitleslangs'] = langs
  90. subtitles = self.getSubtitles()
  91. for lang in langs:
  92. self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
  93. class TestDailymotionSubtitles(BaseTestSubtitles):
  94. url = 'http://www.dailymotion.com/video/xczg00'
  95. IE = DailymotionIE
  96. def test_no_writesubtitles(self):
  97. subtitles = self.getSubtitles()
  98. self.assertEqual(subtitles, None)
  99. def test_subtitles(self):
  100. self.DL.params['writesubtitles'] = True
  101. subtitles = self.getSubtitles()
  102. self.assertEqual(md5(subtitles['en']), '976553874490cba125086bbfea3ff76f')
  103. def test_subtitles_lang(self):
  104. self.DL.params['writesubtitles'] = True
  105. self.DL.params['subtitleslangs'] = ['fr']
  106. subtitles = self.getSubtitles()
  107. self.assertEqual(md5(subtitles['fr']), '594564ec7d588942e384e920e5341792')
  108. def test_allsubtitles(self):
  109. self.DL.params['writesubtitles'] = True
  110. self.DL.params['allsubtitles'] = True
  111. subtitles = self.getSubtitles()
  112. self.assertEqual(len(subtitles.keys()), 5)
  113. def test_list_subtitles(self):
  114. self.DL.expect_warning('Automatic Captions not supported by this server')
  115. self.DL.params['listsubtitles'] = True
  116. info_dict = self.getInfoDict()
  117. self.assertEqual(info_dict, None)
  118. def test_automatic_captions(self):
  119. self.DL.expect_warning('Automatic Captions not supported by this server')
  120. self.DL.params['writeautomaticsub'] = True
  121. self.DL.params['subtitleslang'] = ['en']
  122. subtitles = self.getSubtitles()
  123. self.assertTrue(len(subtitles.keys()) == 0)
  124. def test_nosubtitles(self):
  125. self.DL.expect_warning('video doesn\'t have subtitles')
  126. self.url = 'http://www.dailymotion.com/video/x12u166_le-zapping-tele-star-du-08-aout-2013_tv'
  127. self.DL.params['writesubtitles'] = True
  128. self.DL.params['allsubtitles'] = True
  129. subtitles = self.getSubtitles()
  130. self.assertEqual(len(subtitles), 0)
  131. def test_multiple_langs(self):
  132. self.DL.params['writesubtitles'] = True
  133. langs = ['es', 'fr', 'de']
  134. self.DL.params['subtitleslangs'] = langs
  135. subtitles = self.getSubtitles()
  136. for lang in langs:
  137. self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
  138. class TestTedSubtitles(BaseTestSubtitles):
  139. url = 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html'
  140. IE = TEDIE
  141. def test_no_writesubtitles(self):
  142. subtitles = self.getSubtitles()
  143. self.assertEqual(subtitles, None)
  144. def test_subtitles(self):
  145. self.DL.params['writesubtitles'] = True
  146. subtitles = self.getSubtitles()
  147. self.assertEqual(md5(subtitles['en']), '4262c1665ff928a2dada178f62cb8d14')
  148. def test_subtitles_lang(self):
  149. self.DL.params['writesubtitles'] = True
  150. self.DL.params['subtitleslangs'] = ['fr']
  151. subtitles = self.getSubtitles()
  152. self.assertEqual(md5(subtitles['fr']), '66a63f7f42c97a50f8c0e90bc7797bb5')
  153. def test_allsubtitles(self):
  154. self.DL.params['writesubtitles'] = True
  155. self.DL.params['allsubtitles'] = True
  156. subtitles = self.getSubtitles()
  157. self.assertTrue(len(subtitles.keys()) >= 28)
  158. def test_list_subtitles(self):
  159. self.DL.expect_warning('Automatic Captions not supported by this server')
  160. self.DL.params['listsubtitles'] = True
  161. info_dict = self.getInfoDict()
  162. self.assertEqual(info_dict, None)
  163. def test_automatic_captions(self):
  164. self.DL.expect_warning('Automatic Captions not supported by this server')
  165. self.DL.params['writeautomaticsub'] = True
  166. self.DL.params['subtitleslang'] = ['en']
  167. subtitles = self.getSubtitles()
  168. self.assertTrue(len(subtitles.keys()) == 0)
  169. def test_multiple_langs(self):
  170. self.DL.params['writesubtitles'] = True
  171. langs = ['es', 'fr', 'de']
  172. self.DL.params['subtitleslangs'] = langs
  173. subtitles = self.getSubtitles()
  174. for lang in langs:
  175. self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
  176. class TestBlipTVSubtitles(BaseTestSubtitles):
  177. url = 'http://blip.tv/a/a-6603250'
  178. IE = BlipTVIE
  179. def test_list_subtitles(self):
  180. self.DL.expect_warning('Automatic Captions not supported by this server')
  181. self.DL.params['listsubtitles'] = True
  182. info_dict = self.getInfoDict()
  183. self.assertEqual(info_dict, None)
  184. def test_allsubtitles(self):
  185. self.DL.expect_warning('Automatic Captions not supported by this server')
  186. self.DL.params['writesubtitles'] = True
  187. self.DL.params['allsubtitles'] = True
  188. subtitles = self.getSubtitles()
  189. self.assertEqual(set(subtitles.keys()), set(['en']))
  190. self.assertEqual(md5(subtitles['en']), '5b75c300af65fe4476dff79478bb93e4')
  191. class TestVimeoSubtitles(BaseTestSubtitles):
  192. url = 'http://vimeo.com/76979871'
  193. IE = VimeoIE
  194. def test_no_writesubtitles(self):
  195. subtitles = self.getSubtitles()
  196. self.assertEqual(subtitles, None)
  197. def test_subtitles(self):
  198. self.DL.params['writesubtitles'] = True
  199. subtitles = self.getSubtitles()
  200. self.assertEqual(md5(subtitles['en']), '26399116d23ae3cf2c087cea94bc43b4')
  201. def test_subtitles_lang(self):
  202. self.DL.params['writesubtitles'] = True
  203. self.DL.params['subtitleslangs'] = ['fr']
  204. subtitles = self.getSubtitles()
  205. self.assertEqual(md5(subtitles['fr']), 'b6191146a6c5d3a452244d853fde6dc8')
  206. def test_allsubtitles(self):
  207. self.DL.params['writesubtitles'] = True
  208. self.DL.params['allsubtitles'] = True
  209. subtitles = self.getSubtitles()
  210. self.assertEqual(set(subtitles.keys()), set(['de', 'en', 'es', 'fr']))
  211. def test_list_subtitles(self):
  212. self.DL.expect_warning('Automatic Captions not supported by this server')
  213. self.DL.params['listsubtitles'] = True
  214. info_dict = self.getInfoDict()
  215. self.assertEqual(info_dict, None)
  216. def test_automatic_captions(self):
  217. self.DL.expect_warning('Automatic Captions not supported by this server')
  218. self.DL.params['writeautomaticsub'] = True
  219. self.DL.params['subtitleslang'] = ['en']
  220. subtitles = self.getSubtitles()
  221. self.assertTrue(len(subtitles.keys()) == 0)
  222. def test_nosubtitles(self):
  223. self.DL.expect_warning('video doesn\'t have subtitles')
  224. self.url = 'http://vimeo.com/56015672'
  225. self.DL.params['writesubtitles'] = True
  226. self.DL.params['allsubtitles'] = True
  227. subtitles = self.getSubtitles()
  228. self.assertEqual(len(subtitles), 0)
  229. def test_multiple_langs(self):
  230. self.DL.params['writesubtitles'] = True
  231. langs = ['es', 'fr', 'de']
  232. self.DL.params['subtitleslangs'] = langs
  233. subtitles = self.getSubtitles()
  234. for lang in langs:
  235. self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
  236. class TestWallaSubtitles(BaseTestSubtitles):
  237. url = 'http://vod.walla.co.il/movie/2705958/the-yes-men'
  238. IE = WallaIE
  239. def test_list_subtitles(self):
  240. self.DL.expect_warning('Automatic Captions not supported by this server')
  241. self.DL.params['listsubtitles'] = True
  242. info_dict = self.getInfoDict()
  243. self.assertEqual(info_dict, None)
  244. def test_allsubtitles(self):
  245. self.DL.expect_warning('Automatic Captions not supported by this server')
  246. self.DL.params['writesubtitles'] = True
  247. self.DL.params['allsubtitles'] = True
  248. subtitles = self.getSubtitles()
  249. self.assertEqual(set(subtitles.keys()), set(['heb']))
  250. self.assertEqual(md5(subtitles['heb']), 'e758c5d7cb982f6bef14f377ec7a3920')
  251. def test_nosubtitles(self):
  252. self.DL.expect_warning('video doesn\'t have subtitles')
  253. self.url = 'http://vod.walla.co.il/movie/2642630/one-direction-all-for-one'
  254. self.DL.params['writesubtitles'] = True
  255. self.DL.params['allsubtitles'] = True
  256. subtitles = self.getSubtitles()
  257. self.assertEqual(len(subtitles), 0)
  258. if __name__ == '__main__':
  259. unittest.main()