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.

311 lines
11 KiB

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