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.

310 lines
11 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
  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_subtitles(self):
  42. self.DL.params['writesubtitles'] = True
  43. subtitles = self.getSubtitles()
  44. self.assertEqual(md5(subtitles['en']), '4cd9278a35ba2305f47354ee13472260')
  45. def test_youtube_subtitles_lang(self):
  46. self.DL.params['writesubtitles'] = True
  47. self.DL.params['subtitleslangs'] = ['it']
  48. subtitles = self.getSubtitles()
  49. self.assertEqual(md5(subtitles['it']), '164a51f16f260476a05b50fe4c2f161d')
  50. def test_youtube_allsubtitles(self):
  51. self.DL.params['writesubtitles'] = True
  52. self.DL.params['allsubtitles'] = True
  53. subtitles = self.getSubtitles()
  54. self.assertEqual(len(subtitles.keys()), 13)
  55. def test_youtube_subtitles_sbv_format(self):
  56. self.DL.params['writesubtitles'] = True
  57. self.DL.params['subtitlesformat'] = 'sbv'
  58. subtitles = self.getSubtitles()
  59. self.assertEqual(md5(subtitles['en']), '13aeaa0c245a8bed9a451cb643e3ad8b')
  60. def test_youtube_subtitles_vtt_format(self):
  61. self.DL.params['writesubtitles'] = True
  62. self.DL.params['subtitlesformat'] = 'vtt'
  63. subtitles = self.getSubtitles()
  64. self.assertEqual(md5(subtitles['en']), '3cb210999d3e021bd6c7f0ea751eab06')
  65. def test_youtube_list_subtitles(self):
  66. self.DL.expect_warning('Video doesn\'t have automatic captions')
  67. self.DL.params['listsubtitles'] = True
  68. info_dict = self.getInfoDict()
  69. self.assertEqual(info_dict, None)
  70. def test_youtube_automatic_captions(self):
  71. self.url = '8YoUxe5ncPo'
  72. self.DL.params['writeautomaticsub'] = True
  73. self.DL.params['subtitleslangs'] = ['it']
  74. subtitles = self.getSubtitles()
  75. self.assertTrue(subtitles['it'] is not None)
  76. def test_youtube_translated_subtitles(self):
  77. # This video has a subtitles track, which can be translated
  78. self.url = 'Ky9eprVWzlI'
  79. self.DL.params['writeautomaticsub'] = True
  80. self.DL.params['subtitleslangs'] = ['it']
  81. subtitles = self.getSubtitles()
  82. self.assertTrue(subtitles['it'] is not None)
  83. def test_youtube_nosubtitles(self):
  84. self.DL.expect_warning('video doesn\'t have subtitles')
  85. self.url = 'n5BB19UTcdA'
  86. self.DL.params['writesubtitles'] = True
  87. self.DL.params['allsubtitles'] = True
  88. subtitles = self.getSubtitles()
  89. self.assertEqual(len(subtitles), 0)
  90. def test_youtube_multiple_langs(self):
  91. self.url = 'QRS8MkLhQmM'
  92. self.DL.params['writesubtitles'] = True
  93. langs = ['it', 'fr', 'de']
  94. self.DL.params['subtitleslangs'] = langs
  95. subtitles = self.getSubtitles()
  96. for lang in langs:
  97. self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
  98. class TestDailymotionSubtitles(BaseTestSubtitles):
  99. url = 'http://www.dailymotion.com/video/xczg00'
  100. IE = DailymotionIE
  101. def test_subtitles(self):
  102. self.DL.params['writesubtitles'] = True
  103. subtitles = self.getSubtitles()
  104. self.assertEqual(md5(subtitles['en']), '976553874490cba125086bbfea3ff76f')
  105. def test_subtitles_lang(self):
  106. self.DL.params['writesubtitles'] = True
  107. self.DL.params['subtitleslangs'] = ['fr']
  108. subtitles = self.getSubtitles()
  109. self.assertEqual(md5(subtitles['fr']), '594564ec7d588942e384e920e5341792')
  110. def test_allsubtitles(self):
  111. self.DL.params['writesubtitles'] = True
  112. self.DL.params['allsubtitles'] = True
  113. subtitles = self.getSubtitles()
  114. self.assertTrue(len(subtitles.keys()) >= 6)
  115. def test_nosubtitles(self):
  116. self.DL.expect_warning('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, '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.assertFalse(subtitles)
  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.params['listsubtitles'] = True
  151. info_dict = self.getInfoDict()
  152. self.assertEqual(info_dict, None)
  153. def test_multiple_langs(self):
  154. self.DL.params['writesubtitles'] = True
  155. langs = ['es', 'fr', 'de']
  156. self.DL.params['subtitleslangs'] = langs
  157. subtitles = self.getSubtitles()
  158. for lang in langs:
  159. self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
  160. class TestBlipTVSubtitles(BaseTestSubtitles):
  161. url = 'http://blip.tv/a/a-6603250'
  162. IE = BlipTVIE
  163. def test_list_subtitles(self):
  164. self.DL.expect_warning('Automatic Captions not supported by this server')
  165. self.DL.params['listsubtitles'] = True
  166. info_dict = self.getInfoDict()
  167. self.assertEqual(info_dict, None)
  168. def test_allsubtitles(self):
  169. self.DL.expect_warning('Automatic Captions not supported by this server')
  170. self.DL.params['writesubtitles'] = True
  171. self.DL.params['allsubtitles'] = True
  172. subtitles = self.getSubtitles()
  173. self.assertEqual(set(subtitles.keys()), set(['en']))
  174. self.assertEqual(md5(subtitles['en']), '5b75c300af65fe4476dff79478bb93e4')
  175. class TestVimeoSubtitles(BaseTestSubtitles):
  176. url = 'http://vimeo.com/76979871'
  177. IE = VimeoIE
  178. def test_subtitles(self):
  179. self.DL.params['writesubtitles'] = True
  180. subtitles = self.getSubtitles()
  181. self.assertEqual(md5(subtitles['en']), '8062383cf4dec168fc40a088aa6d5888')
  182. def test_subtitles_lang(self):
  183. self.DL.params['writesubtitles'] = True
  184. self.DL.params['subtitleslangs'] = ['fr']
  185. subtitles = self.getSubtitles()
  186. self.assertEqual(md5(subtitles['fr']), 'b6191146a6c5d3a452244d853fde6dc8')
  187. def test_allsubtitles(self):
  188. self.DL.params['writesubtitles'] = True
  189. self.DL.params['allsubtitles'] = True
  190. subtitles = self.getSubtitles()
  191. self.assertEqual(set(subtitles.keys()), set(['de', 'en', 'es', 'fr']))
  192. def test_nosubtitles(self):
  193. self.DL.expect_warning('video doesn\'t have subtitles')
  194. self.url = 'http://vimeo.com/56015672'
  195. self.DL.params['writesubtitles'] = True
  196. self.DL.params['allsubtitles'] = True
  197. subtitles = self.getSubtitles()
  198. self.assertEqual(len(subtitles), 0)
  199. def test_multiple_langs(self):
  200. self.DL.params['writesubtitles'] = True
  201. langs = ['es', 'fr', 'de']
  202. self.DL.params['subtitleslangs'] = langs
  203. subtitles = self.getSubtitles()
  204. for lang in langs:
  205. self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
  206. class TestWallaSubtitles(BaseTestSubtitles):
  207. url = 'http://vod.walla.co.il/movie/2705958/the-yes-men'
  208. IE = WallaIE
  209. def test_list_subtitles(self):
  210. self.DL.expect_warning('Automatic Captions not supported by this server')
  211. self.DL.params['listsubtitles'] = True
  212. info_dict = self.getInfoDict()
  213. self.assertEqual(info_dict, None)
  214. def test_allsubtitles(self):
  215. self.DL.expect_warning('Automatic Captions not supported by this server')
  216. self.DL.params['writesubtitles'] = True
  217. self.DL.params['allsubtitles'] = True
  218. subtitles = self.getSubtitles()
  219. self.assertEqual(set(subtitles.keys()), set(['heb']))
  220. self.assertEqual(md5(subtitles['heb']), 'e758c5d7cb982f6bef14f377ec7a3920')
  221. def test_nosubtitles(self):
  222. self.DL.expect_warning('video doesn\'t have subtitles')
  223. self.url = 'http://vod.walla.co.il/movie/2642630/one-direction-all-for-one'
  224. self.DL.params['writesubtitles'] = True
  225. self.DL.params['allsubtitles'] = True
  226. subtitles = self.getSubtitles()
  227. self.assertEqual(len(subtitles), 0)
  228. class TestCeskaTelevizeSubtitles(BaseTestSubtitles):
  229. url = 'http://www.ceskatelevize.cz/ivysilani/10600540290-u6-uzasny-svet-techniky'
  230. IE = CeskaTelevizeIE
  231. def test_list_subtitles(self):
  232. self.DL.expect_warning('Automatic Captions not supported by this server')
  233. self.DL.params['listsubtitles'] = True
  234. info_dict = self.getInfoDict()
  235. self.assertEqual(info_dict, None)
  236. def test_allsubtitles(self):
  237. self.DL.expect_warning('Automatic Captions not supported by this server')
  238. self.DL.params['writesubtitles'] = True
  239. self.DL.params['allsubtitles'] = True
  240. subtitles = self.getSubtitles()
  241. self.assertEqual(set(subtitles.keys()), set(['cs']))
  242. self.assertTrue(len(subtitles['cs']) > 20000)
  243. def test_nosubtitles(self):
  244. self.DL.expect_warning('video doesn\'t have subtitles')
  245. self.url = 'http://www.ceskatelevize.cz/ivysilani/ivysilani/10441294653-hyde-park-civilizace/214411058091220'
  246. self.DL.params['writesubtitles'] = True
  247. self.DL.params['allsubtitles'] = True
  248. subtitles = self.getSubtitles()
  249. self.assertEqual(len(subtitles), 0)
  250. if __name__ == '__main__':
  251. unittest.main()