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.

404 lines
15 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
  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. LyndaIE,
  18. NPOIE,
  19. ComedyCentralIE,
  20. NRKTVIE,
  21. RaiIE,
  22. VikiIE,
  23. ThePlatformIE,
  24. )
  25. class BaseTestSubtitles(unittest.TestCase):
  26. url = None
  27. IE = None
  28. def setUp(self):
  29. self.DL = FakeYDL()
  30. self.ie = self.IE()
  31. self.DL.add_info_extractor(self.ie)
  32. def getInfoDict(self):
  33. info_dict = self.DL.extract_info(self.url, download=False)
  34. return info_dict
  35. def getSubtitles(self):
  36. info_dict = self.getInfoDict()
  37. subtitles = info_dict['requested_subtitles']
  38. if not subtitles:
  39. return subtitles
  40. for sub_info in subtitles.values():
  41. if sub_info.get('data') is None:
  42. uf = self.DL.urlopen(sub_info['url'])
  43. sub_info['data'] = uf.read().decode('utf-8')
  44. return dict((l, sub_info['data']) for l, sub_info in subtitles.items())
  45. class TestYoutubeSubtitles(BaseTestSubtitles):
  46. url = 'QRS8MkLhQmM'
  47. IE = YoutubeIE
  48. def test_youtube_subtitles(self):
  49. self.DL.params['writesubtitles'] = True
  50. subtitles = self.getSubtitles()
  51. self.assertEqual(md5(subtitles['en']), '4cd9278a35ba2305f47354ee13472260')
  52. def test_youtube_subtitles_lang(self):
  53. self.DL.params['writesubtitles'] = True
  54. self.DL.params['subtitleslangs'] = ['it']
  55. subtitles = self.getSubtitles()
  56. self.assertEqual(md5(subtitles['it']), '164a51f16f260476a05b50fe4c2f161d')
  57. def test_youtube_allsubtitles(self):
  58. self.DL.params['writesubtitles'] = True
  59. self.DL.params['allsubtitles'] = True
  60. subtitles = self.getSubtitles()
  61. self.assertEqual(len(subtitles.keys()), 13)
  62. def test_youtube_subtitles_sbv_format(self):
  63. self.DL.params['writesubtitles'] = True
  64. self.DL.params['subtitlesformat'] = 'sbv'
  65. subtitles = self.getSubtitles()
  66. self.assertEqual(md5(subtitles['en']), '13aeaa0c245a8bed9a451cb643e3ad8b')
  67. def test_youtube_subtitles_vtt_format(self):
  68. self.DL.params['writesubtitles'] = True
  69. self.DL.params['subtitlesformat'] = 'vtt'
  70. subtitles = self.getSubtitles()
  71. self.assertEqual(md5(subtitles['en']), '3cb210999d3e021bd6c7f0ea751eab06')
  72. def test_youtube_list_subtitles(self):
  73. self.DL.expect_warning('Video doesn\'t have automatic captions')
  74. self.DL.params['listsubtitles'] = True
  75. info_dict = self.getInfoDict()
  76. self.assertEqual(info_dict, None)
  77. def test_youtube_automatic_captions(self):
  78. self.url = '8YoUxe5ncPo'
  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_translated_subtitles(self):
  84. # This video has a subtitles track, which can be translated
  85. self.url = 'Ky9eprVWzlI'
  86. self.DL.params['writeautomaticsub'] = True
  87. self.DL.params['subtitleslangs'] = ['it']
  88. subtitles = self.getSubtitles()
  89. self.assertTrue(subtitles['it'] is not None)
  90. def test_youtube_nosubtitles(self):
  91. self.DL.expect_warning('video doesn\'t have subtitles')
  92. self.url = 'n5BB19UTcdA'
  93. self.DL.params['writesubtitles'] = True
  94. self.DL.params['allsubtitles'] = True
  95. subtitles = self.getSubtitles()
  96. self.assertFalse(subtitles)
  97. def test_youtube_multiple_langs(self):
  98. self.url = 'QRS8MkLhQmM'
  99. self.DL.params['writesubtitles'] = True
  100. langs = ['it', 'fr', 'de']
  101. self.DL.params['subtitleslangs'] = langs
  102. subtitles = self.getSubtitles()
  103. for lang in langs:
  104. self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
  105. class TestDailymotionSubtitles(BaseTestSubtitles):
  106. url = 'http://www.dailymotion.com/video/xczg00'
  107. IE = DailymotionIE
  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_nosubtitles(self):
  123. self.DL.expect_warning('video doesn\'t have subtitles')
  124. self.url = 'http://www.dailymotion.com/video/x12u166_le-zapping-tele-star-du-08-aout-2013_tv'
  125. self.DL.params['writesubtitles'] = True
  126. self.DL.params['allsubtitles'] = True
  127. subtitles = self.getSubtitles()
  128. self.assertFalse(subtitles)
  129. def test_multiple_langs(self):
  130. self.DL.params['writesubtitles'] = True
  131. langs = ['es', 'fr', 'de']
  132. self.DL.params['subtitleslangs'] = langs
  133. subtitles = self.getSubtitles()
  134. for lang in langs:
  135. self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
  136. class TestTedSubtitles(BaseTestSubtitles):
  137. url = 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html'
  138. IE = TEDIE
  139. def test_no_writesubtitles(self):
  140. subtitles = self.getSubtitles()
  141. self.assertFalse(subtitles)
  142. def test_subtitles(self):
  143. self.DL.params['writesubtitles'] = True
  144. subtitles = self.getSubtitles()
  145. self.assertEqual(md5(subtitles['en']), '4262c1665ff928a2dada178f62cb8d14')
  146. def test_subtitles_lang(self):
  147. self.DL.params['writesubtitles'] = True
  148. self.DL.params['subtitleslangs'] = ['fr']
  149. subtitles = self.getSubtitles()
  150. self.assertEqual(md5(subtitles['fr']), '66a63f7f42c97a50f8c0e90bc7797bb5')
  151. def test_allsubtitles(self):
  152. self.DL.params['writesubtitles'] = True
  153. self.DL.params['allsubtitles'] = True
  154. subtitles = self.getSubtitles()
  155. self.assertTrue(len(subtitles.keys()) >= 28)
  156. def test_list_subtitles(self):
  157. self.DL.params['listsubtitles'] = True
  158. info_dict = self.getInfoDict()
  159. self.assertEqual(info_dict, None)
  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, '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.params['listsubtitles'] = True
  172. info_dict = self.getInfoDict()
  173. self.assertEqual(info_dict, None)
  174. def test_allsubtitles(self):
  175. self.DL.params['writesubtitles'] = True
  176. self.DL.params['allsubtitles'] = True
  177. subtitles = self.getSubtitles()
  178. self.assertEqual(set(subtitles.keys()), set(['en']))
  179. self.assertEqual(md5(subtitles['en']), '5b75c300af65fe4476dff79478bb93e4')
  180. class TestVimeoSubtitles(BaseTestSubtitles):
  181. url = 'http://vimeo.com/76979871'
  182. IE = VimeoIE
  183. def test_subtitles(self):
  184. self.DL.params['writesubtitles'] = True
  185. subtitles = self.getSubtitles()
  186. self.assertEqual(md5(subtitles['en']), '8062383cf4dec168fc40a088aa6d5888')
  187. def test_subtitles_lang(self):
  188. self.DL.params['writesubtitles'] = True
  189. self.DL.params['subtitleslangs'] = ['fr']
  190. subtitles = self.getSubtitles()
  191. self.assertEqual(md5(subtitles['fr']), 'b6191146a6c5d3a452244d853fde6dc8')
  192. def test_allsubtitles(self):
  193. self.DL.params['writesubtitles'] = True
  194. self.DL.params['allsubtitles'] = True
  195. subtitles = self.getSubtitles()
  196. self.assertEqual(set(subtitles.keys()), set(['de', 'en', 'es', 'fr']))
  197. def test_nosubtitles(self):
  198. self.DL.expect_warning('video doesn\'t have subtitles')
  199. self.url = 'http://vimeo.com/56015672'
  200. self.DL.params['writesubtitles'] = True
  201. self.DL.params['allsubtitles'] = True
  202. subtitles = self.getSubtitles()
  203. self.assertFalse(subtitles)
  204. def test_multiple_langs(self):
  205. self.DL.params['writesubtitles'] = True
  206. langs = ['es', 'fr', 'de']
  207. self.DL.params['subtitleslangs'] = langs
  208. subtitles = self.getSubtitles()
  209. for lang in langs:
  210. self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
  211. class TestWallaSubtitles(BaseTestSubtitles):
  212. url = 'http://vod.walla.co.il/movie/2705958/the-yes-men'
  213. IE = WallaIE
  214. def test_list_subtitles(self):
  215. self.DL.expect_warning('Automatic Captions not supported by this server')
  216. self.DL.params['listsubtitles'] = True
  217. info_dict = self.getInfoDict()
  218. self.assertEqual(info_dict, None)
  219. def test_allsubtitles(self):
  220. self.DL.expect_warning('Automatic Captions not supported by this server')
  221. self.DL.params['writesubtitles'] = True
  222. self.DL.params['allsubtitles'] = True
  223. subtitles = self.getSubtitles()
  224. self.assertEqual(set(subtitles.keys()), set(['heb']))
  225. self.assertEqual(md5(subtitles['heb']), 'e758c5d7cb982f6bef14f377ec7a3920')
  226. def test_nosubtitles(self):
  227. self.DL.expect_warning('video doesn\'t have subtitles')
  228. self.url = 'http://vod.walla.co.il/movie/2642630/one-direction-all-for-one'
  229. self.DL.params['writesubtitles'] = True
  230. self.DL.params['allsubtitles'] = True
  231. subtitles = self.getSubtitles()
  232. self.assertFalse(subtitles)
  233. class TestCeskaTelevizeSubtitles(BaseTestSubtitles):
  234. url = 'http://www.ceskatelevize.cz/ivysilani/10600540290-u6-uzasny-svet-techniky'
  235. IE = CeskaTelevizeIE
  236. def test_list_subtitles(self):
  237. self.DL.expect_warning('Automatic Captions not supported by this server')
  238. self.DL.params['listsubtitles'] = True
  239. info_dict = self.getInfoDict()
  240. self.assertEqual(info_dict, None)
  241. def test_allsubtitles(self):
  242. self.DL.expect_warning('Automatic Captions not supported by this server')
  243. self.DL.params['writesubtitles'] = True
  244. self.DL.params['allsubtitles'] = True
  245. subtitles = self.getSubtitles()
  246. self.assertEqual(set(subtitles.keys()), set(['cs']))
  247. self.assertTrue(len(subtitles['cs']) > 20000)
  248. def test_nosubtitles(self):
  249. self.DL.expect_warning('video doesn\'t have subtitles')
  250. self.url = 'http://www.ceskatelevize.cz/ivysilani/ivysilani/10441294653-hyde-park-civilizace/214411058091220'
  251. self.DL.params['writesubtitles'] = True
  252. self.DL.params['allsubtitles'] = True
  253. subtitles = self.getSubtitles()
  254. self.assertFalse(subtitles)
  255. class TestLyndaSubtitles(BaseTestSubtitles):
  256. url = 'http://www.lynda.com/Bootstrap-tutorials/Using-exercise-files/110885/114408-4.html'
  257. IE = LyndaIE
  258. def test_allsubtitles(self):
  259. self.DL.params['writesubtitles'] = True
  260. self.DL.params['allsubtitles'] = True
  261. subtitles = self.getSubtitles()
  262. self.assertEqual(set(subtitles.keys()), set(['en']))
  263. self.assertEqual(md5(subtitles['en']), '09bbe67222259bed60deaa26997d73a7')
  264. class TestNPOSubtitles(BaseTestSubtitles):
  265. url = 'http://www.npo.nl/nos-journaal/28-08-2014/POW_00722860'
  266. IE = NPOIE
  267. def test_allsubtitles(self):
  268. self.DL.params['writesubtitles'] = True
  269. self.DL.params['allsubtitles'] = True
  270. subtitles = self.getSubtitles()
  271. self.assertEqual(set(subtitles.keys()), set(['nl']))
  272. self.assertEqual(md5(subtitles['nl']), 'fc6435027572b63fb4ab143abd5ad3f4')
  273. class TestMTVSubtitles(BaseTestSubtitles):
  274. url = 'http://www.cc.com/video-clips/kllhuv/stand-up-greg-fitzsimmons--uncensored---too-good-of-a-mother'
  275. IE = ComedyCentralIE
  276. def getInfoDict(self):
  277. return super(TestMTVSubtitles, self).getInfoDict()['entries'][0]
  278. def test_allsubtitles(self):
  279. self.DL.params['writesubtitles'] = True
  280. self.DL.params['allsubtitles'] = True
  281. subtitles = self.getSubtitles()
  282. self.assertEqual(set(subtitles.keys()), set(['en']))
  283. self.assertEqual(md5(subtitles['en']), 'b9f6ca22a6acf597ec76f61749765e65')
  284. class TestNRKSubtitles(BaseTestSubtitles):
  285. url = 'http://tv.nrk.no/serie/ikke-gjoer-dette-hjemme/DMPV73000411/sesong-2/episode-1'
  286. IE = NRKTVIE
  287. def test_allsubtitles(self):
  288. self.DL.params['writesubtitles'] = True
  289. self.DL.params['allsubtitles'] = True
  290. subtitles = self.getSubtitles()
  291. self.assertEqual(set(subtitles.keys()), set(['no']))
  292. self.assertEqual(md5(subtitles['no']), '1d221e6458c95c5494dcd38e6a1f129a')
  293. class TestRaiSubtitles(BaseTestSubtitles):
  294. url = 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-cb27157f-9dd0-4aee-b788-b1f67643a391.html'
  295. IE = RaiIE
  296. def test_allsubtitles(self):
  297. self.DL.params['writesubtitles'] = True
  298. self.DL.params['allsubtitles'] = True
  299. subtitles = self.getSubtitles()
  300. self.assertEqual(set(subtitles.keys()), set(['it']))
  301. self.assertEqual(md5(subtitles['it']), 'b1d90a98755126b61e667567a1f6680a')
  302. class TestVikiSubtitles(BaseTestSubtitles):
  303. url = 'http://www.viki.com/videos/1060846v-punch-episode-18'
  304. IE = VikiIE
  305. def test_allsubtitles(self):
  306. self.DL.params['writesubtitles'] = True
  307. self.DL.params['allsubtitles'] = True
  308. subtitles = self.getSubtitles()
  309. self.assertEqual(set(subtitles.keys()), set(['en']))
  310. self.assertEqual(md5(subtitles['en']), 'b0b781eeb45efd3f6398a925b259150b')
  311. class TestThePlatformSubtitles(BaseTestSubtitles):
  312. # from http://www.3playmedia.com/services-features/tools/integrations/theplatform/
  313. # (see http://theplatform.com/about/partners/type/subtitles-closed-captioning/)
  314. url = 'theplatform:JFUjUE1_ehvq'
  315. IE = ThePlatformIE
  316. def test_allsubtitles(self):
  317. self.DL.params['writesubtitles'] = True
  318. self.DL.params['allsubtitles'] = True
  319. subtitles = self.getSubtitles()
  320. self.assertEqual(set(subtitles.keys()), set(['en']))
  321. self.assertEqual(md5(subtitles['en']), '97e7670cbae3c4d26ae8bcc7fdd78d4b')
  322. if __name__ == '__main__':
  323. unittest.main()