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.

332 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
  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. CeskaTelevizeIE,
  17. )
  18. class BaseTestSubtitles(unittest.TestCase):
  19. url = None
  20. IE = None
  21. def setUp(self):
  22. self.DL = FakeYDL()
  23. self.ie = self.IE()
  24. self.DL.add_info_extractor(self.ie)
  25. def getInfoDict(self):
  26. info_dict = self.DL.extract_info(self.url, download=False)
  27. return info_dict
  28. def getSubtitles(self):
  29. info_dict = self.getInfoDict()
  30. subtitles = info_dict['requested_subtitles']
  31. if not subtitles:
  32. return subtitles
  33. for sub_info in subtitles.values():
  34. if sub_info.get('data') is None:
  35. uf = self.DL.urlopen(sub_info['url'])
  36. sub_info['data'] = uf.read().decode('utf-8')
  37. return dict((l, sub_info['data']) for l, sub_info in subtitles.items())
  38. class TestYoutubeSubtitles(BaseTestSubtitles):
  39. url = 'QRS8MkLhQmM'
  40. IE = YoutubeIE
  41. def test_youtube_no_writesubtitles(self):
  42. self.DL.params['writesubtitles'] = False
  43. subtitles = self.getSubtitles()
  44. self.assertEqual(subtitles, None)
  45. def test_youtube_subtitles(self):
  46. self.DL.params['writesubtitles'] = True
  47. subtitles = self.getSubtitles()
  48. self.assertEqual(md5(subtitles['en']), '4cd9278a35ba2305f47354ee13472260')
  49. def test_youtube_subtitles_lang(self):
  50. self.DL.params['writesubtitles'] = True
  51. self.DL.params['subtitleslangs'] = ['it']
  52. subtitles = self.getSubtitles()
  53. self.assertEqual(md5(subtitles['it']), '164a51f16f260476a05b50fe4c2f161d')
  54. def test_youtube_allsubtitles(self):
  55. self.DL.params['writesubtitles'] = True
  56. self.DL.params['allsubtitles'] = True
  57. subtitles = self.getSubtitles()
  58. self.assertEqual(len(subtitles.keys()), 13)
  59. def test_youtube_subtitles_sbv_format(self):
  60. self.DL.params['writesubtitles'] = True
  61. self.DL.params['subtitlesformat'] = 'sbv'
  62. subtitles = self.getSubtitles()
  63. self.assertEqual(md5(subtitles['en']), '13aeaa0c245a8bed9a451cb643e3ad8b')
  64. def test_youtube_subtitles_vtt_format(self):
  65. self.DL.params['writesubtitles'] = True
  66. self.DL.params['subtitlesformat'] = 'vtt'
  67. subtitles = self.getSubtitles()
  68. self.assertEqual(md5(subtitles['en']), '3cb210999d3e021bd6c7f0ea751eab06')
  69. def test_youtube_list_subtitles(self):
  70. self.DL.expect_warning('Video doesn\'t have automatic captions')
  71. self.DL.params['listsubtitles'] = True
  72. info_dict = self.getInfoDict()
  73. self.assertEqual(info_dict, None)
  74. def test_youtube_automatic_captions(self):
  75. self.url = '8YoUxe5ncPo'
  76. self.DL.params['writeautomaticsub'] = True
  77. self.DL.params['subtitleslangs'] = ['it']
  78. subtitles = self.getSubtitles()
  79. self.assertTrue(subtitles['it'] is not None)
  80. def test_youtube_translated_subtitles(self):
  81. # This video has a subtitles track, which can be translated
  82. self.url = 'Ky9eprVWzlI'
  83. self.DL.params['writeautomaticsub'] = True
  84. self.DL.params['subtitleslangs'] = ['it']
  85. subtitles = self.getSubtitles()
  86. self.assertTrue(subtitles['it'] is not None)
  87. def test_youtube_nosubtitles(self):
  88. self.DL.expect_warning('video doesn\'t have subtitles')
  89. self.url = 'n5BB19UTcdA'
  90. self.DL.params['writesubtitles'] = True
  91. self.DL.params['allsubtitles'] = True
  92. subtitles = self.getSubtitles()
  93. self.assertEqual(len(subtitles), 0)
  94. def test_youtube_multiple_langs(self):
  95. self.url = 'QRS8MkLhQmM'
  96. self.DL.params['writesubtitles'] = True
  97. langs = ['it', 'fr', 'de']
  98. self.DL.params['subtitleslangs'] = langs
  99. subtitles = self.getSubtitles()
  100. for lang in langs:
  101. self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
  102. class TestDailymotionSubtitles(BaseTestSubtitles):
  103. url = 'http://www.dailymotion.com/video/xczg00'
  104. IE = DailymotionIE
  105. def test_no_writesubtitles(self):
  106. subtitles = self.getSubtitles()
  107. self.assertEqual(subtitles, None)
  108. def test_subtitles(self):
  109. self.DL.params['writesubtitles'] = True
  110. subtitles = self.getSubtitles()
  111. self.assertEqual(md5(subtitles['en']), '976553874490cba125086bbfea3ff76f')
  112. def test_subtitles_lang(self):
  113. self.DL.params['writesubtitles'] = True
  114. self.DL.params['subtitleslangs'] = ['fr']
  115. subtitles = self.getSubtitles()
  116. self.assertEqual(md5(subtitles['fr']), '594564ec7d588942e384e920e5341792')
  117. def test_allsubtitles(self):
  118. self.DL.params['writesubtitles'] = True
  119. self.DL.params['allsubtitles'] = True
  120. subtitles = self.getSubtitles()
  121. self.assertTrue(len(subtitles.keys()) >= 6)
  122. def test_list_subtitles(self):
  123. self.DL.expect_warning('Automatic Captions not supported by this server')
  124. self.DL.params['listsubtitles'] = True
  125. info_dict = self.getInfoDict()
  126. self.assertEqual(info_dict, None)
  127. def test_automatic_captions(self):
  128. self.DL.expect_warning('Automatic Captions not supported by this server')
  129. self.DL.params['writeautomaticsub'] = True
  130. self.DL.params['subtitleslang'] = ['en']
  131. subtitles = self.getSubtitles()
  132. self.assertTrue(len(subtitles.keys()) == 0)
  133. def test_nosubtitles(self):
  134. self.DL.expect_warning('video doesn\'t have subtitles')
  135. self.url = 'http://www.dailymotion.com/video/x12u166_le-zapping-tele-star-du-08-aout-2013_tv'
  136. self.DL.params['writesubtitles'] = True
  137. self.DL.params['allsubtitles'] = True
  138. subtitles = self.getSubtitles()
  139. self.assertEqual(len(subtitles), 0)
  140. def test_multiple_langs(self):
  141. self.DL.params['writesubtitles'] = True
  142. langs = ['es', 'fr', 'de']
  143. self.DL.params['subtitleslangs'] = langs
  144. subtitles = self.getSubtitles()
  145. for lang in langs:
  146. self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
  147. class TestTedSubtitles(BaseTestSubtitles):
  148. url = 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html'
  149. IE = TEDIE
  150. def test_no_writesubtitles(self):
  151. subtitles = self.getSubtitles()
  152. self.assertFalse(subtitles)
  153. def test_subtitles(self):
  154. self.DL.params['writesubtitles'] = True
  155. subtitles = self.getSubtitles()
  156. self.assertEqual(md5(subtitles['en']), '4262c1665ff928a2dada178f62cb8d14')
  157. def test_subtitles_lang(self):
  158. self.DL.params['writesubtitles'] = True
  159. self.DL.params['subtitleslangs'] = ['fr']
  160. subtitles = self.getSubtitles()
  161. self.assertEqual(md5(subtitles['fr']), '66a63f7f42c97a50f8c0e90bc7797bb5')
  162. def test_allsubtitles(self):
  163. self.DL.params['writesubtitles'] = True
  164. self.DL.params['allsubtitles'] = True
  165. subtitles = self.getSubtitles()
  166. self.assertTrue(len(subtitles.keys()) >= 28)
  167. def test_list_subtitles(self):
  168. self.DL.params['listsubtitles'] = True
  169. info_dict = self.getInfoDict()
  170. self.assertEqual(info_dict, None)
  171. def test_multiple_langs(self):
  172. self.DL.params['writesubtitles'] = True
  173. langs = ['es', 'fr', 'de']
  174. self.DL.params['subtitleslangs'] = langs
  175. subtitles = self.getSubtitles()
  176. for lang in langs:
  177. self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
  178. class TestBlipTVSubtitles(BaseTestSubtitles):
  179. url = 'http://blip.tv/a/a-6603250'
  180. IE = BlipTVIE
  181. def test_list_subtitles(self):
  182. self.DL.expect_warning('Automatic Captions not supported by this server')
  183. self.DL.params['listsubtitles'] = True
  184. info_dict = self.getInfoDict()
  185. self.assertEqual(info_dict, None)
  186. def test_allsubtitles(self):
  187. self.DL.expect_warning('Automatic Captions not supported by this server')
  188. self.DL.params['writesubtitles'] = True
  189. self.DL.params['allsubtitles'] = True
  190. subtitles = self.getSubtitles()
  191. self.assertEqual(set(subtitles.keys()), set(['en']))
  192. self.assertEqual(md5(subtitles['en']), '5b75c300af65fe4476dff79478bb93e4')
  193. class TestVimeoSubtitles(BaseTestSubtitles):
  194. url = 'http://vimeo.com/76979871'
  195. IE = VimeoIE
  196. def test_subtitles(self):
  197. self.DL.params['writesubtitles'] = True
  198. subtitles = self.getSubtitles()
  199. self.assertEqual(md5(subtitles['en']), '8062383cf4dec168fc40a088aa6d5888')
  200. def test_subtitles_lang(self):
  201. self.DL.params['writesubtitles'] = True
  202. self.DL.params['subtitleslangs'] = ['fr']
  203. subtitles = self.getSubtitles()
  204. self.assertEqual(md5(subtitles['fr']), 'b6191146a6c5d3a452244d853fde6dc8')
  205. def test_allsubtitles(self):
  206. self.DL.params['writesubtitles'] = True
  207. self.DL.params['allsubtitles'] = True
  208. subtitles = self.getSubtitles()
  209. self.assertEqual(set(subtitles.keys()), set(['de', 'en', 'es', 'fr']))
  210. def test_nosubtitles(self):
  211. self.DL.expect_warning('video doesn\'t have subtitles')
  212. self.url = 'http://vimeo.com/56015672'
  213. self.DL.params['writesubtitles'] = True
  214. self.DL.params['allsubtitles'] = True
  215. subtitles = self.getSubtitles()
  216. self.assertEqual(len(subtitles), 0)
  217. def test_multiple_langs(self):
  218. self.DL.params['writesubtitles'] = True
  219. langs = ['es', 'fr', 'de']
  220. self.DL.params['subtitleslangs'] = langs
  221. subtitles = self.getSubtitles()
  222. for lang in langs:
  223. self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
  224. class TestWallaSubtitles(BaseTestSubtitles):
  225. url = 'http://vod.walla.co.il/movie/2705958/the-yes-men'
  226. IE = WallaIE
  227. def test_list_subtitles(self):
  228. self.DL.expect_warning('Automatic Captions not supported by this server')
  229. self.DL.params['listsubtitles'] = True
  230. info_dict = self.getInfoDict()
  231. self.assertEqual(info_dict, None)
  232. def test_allsubtitles(self):
  233. self.DL.expect_warning('Automatic Captions not supported by this server')
  234. self.DL.params['writesubtitles'] = True
  235. self.DL.params['allsubtitles'] = True
  236. subtitles = self.getSubtitles()
  237. self.assertEqual(set(subtitles.keys()), set(['heb']))
  238. self.assertEqual(md5(subtitles['heb']), 'e758c5d7cb982f6bef14f377ec7a3920')
  239. def test_nosubtitles(self):
  240. self.DL.expect_warning('video doesn\'t have subtitles')
  241. self.url = 'http://vod.walla.co.il/movie/2642630/one-direction-all-for-one'
  242. self.DL.params['writesubtitles'] = True
  243. self.DL.params['allsubtitles'] = True
  244. subtitles = self.getSubtitles()
  245. self.assertEqual(len(subtitles), 0)
  246. class TestCeskaTelevizeSubtitles(BaseTestSubtitles):
  247. url = 'http://www.ceskatelevize.cz/ivysilani/10600540290-u6-uzasny-svet-techniky'
  248. IE = CeskaTelevizeIE
  249. def test_list_subtitles(self):
  250. self.DL.expect_warning('Automatic Captions not supported by this server')
  251. self.DL.params['listsubtitles'] = True
  252. info_dict = self.getInfoDict()
  253. self.assertEqual(info_dict, None)
  254. def test_allsubtitles(self):
  255. self.DL.expect_warning('Automatic Captions not supported by this server')
  256. self.DL.params['writesubtitles'] = True
  257. self.DL.params['allsubtitles'] = True
  258. subtitles = self.getSubtitles()
  259. self.assertEqual(set(subtitles.keys()), set(['cs']))
  260. self.assertTrue(len(subtitles['cs']) > 20000)
  261. def test_nosubtitles(self):
  262. self.DL.expect_warning('video doesn\'t have subtitles')
  263. self.url = 'http://www.ceskatelevize.cz/ivysilani/ivysilani/10441294653-hyde-park-civilizace/214411058091220'
  264. self.DL.params['writesubtitles'] = True
  265. self.DL.params['allsubtitles'] = True
  266. subtitles = self.getSubtitles()
  267. self.assertEqual(len(subtitles), 0)
  268. if __name__ == '__main__':
  269. unittest.main()