]> git.bitcoin.ninja Git - youtube-dl/blob - test/test_subtitles.py
[compat] compat_etree_fromstring: also decode the text attribute
[youtube-dl] / test / test_subtitles.py
1 #!/usr/bin/env python
2 from __future__ import unicode_literals
3
4 # Allow direct execution
5 import os
6 import sys
7 import unittest
8 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9
10 from test.helper import FakeYDL, md5
11
12
13 from youtube_dl.extractor import (
14     BlipTVIE,
15     YoutubeIE,
16     DailymotionIE,
17     TEDIE,
18     VimeoIE,
19     WallaIE,
20     CeskaTelevizeIE,
21     LyndaIE,
22     NPOIE,
23     ComedyCentralIE,
24     NRKTVIE,
25     RaiIE,
26     VikiIE,
27     ThePlatformIE,
28     ThePlatformFeedIE,
29     RTVEALaCartaIE,
30     FunnyOrDieIE,
31 )
32
33
34 class BaseTestSubtitles(unittest.TestCase):
35     url = None
36     IE = None
37
38     def setUp(self):
39         self.DL = FakeYDL()
40         self.ie = self.IE()
41         self.DL.add_info_extractor(self.ie)
42
43     def getInfoDict(self):
44         info_dict = self.DL.extract_info(self.url, download=False)
45         return info_dict
46
47     def getSubtitles(self):
48         info_dict = self.getInfoDict()
49         subtitles = info_dict['requested_subtitles']
50         if not subtitles:
51             return subtitles
52         for sub_info in subtitles.values():
53             if sub_info.get('data') is None:
54                 uf = self.DL.urlopen(sub_info['url'])
55                 sub_info['data'] = uf.read().decode('utf-8')
56         return dict((l, sub_info['data']) for l, sub_info in subtitles.items())
57
58
59 class TestYoutubeSubtitles(BaseTestSubtitles):
60     url = 'QRS8MkLhQmM'
61     IE = YoutubeIE
62
63     def test_youtube_allsubtitles(self):
64         self.DL.params['writesubtitles'] = True
65         self.DL.params['allsubtitles'] = True
66         subtitles = self.getSubtitles()
67         self.assertEqual(len(subtitles.keys()), 13)
68         self.assertEqual(md5(subtitles['en']), '4cd9278a35ba2305f47354ee13472260')
69         self.assertEqual(md5(subtitles['it']), '164a51f16f260476a05b50fe4c2f161d')
70         for lang in ['it', 'fr', 'de']:
71             self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
72
73     def test_youtube_subtitles_sbv_format(self):
74         self.DL.params['writesubtitles'] = True
75         self.DL.params['subtitlesformat'] = 'sbv'
76         subtitles = self.getSubtitles()
77         self.assertEqual(md5(subtitles['en']), '13aeaa0c245a8bed9a451cb643e3ad8b')
78
79     def test_youtube_subtitles_vtt_format(self):
80         self.DL.params['writesubtitles'] = True
81         self.DL.params['subtitlesformat'] = 'vtt'
82         subtitles = self.getSubtitles()
83         self.assertEqual(md5(subtitles['en']), '3cb210999d3e021bd6c7f0ea751eab06')
84
85     def test_youtube_automatic_captions(self):
86         self.url = '8YoUxe5ncPo'
87         self.DL.params['writeautomaticsub'] = True
88         self.DL.params['subtitleslangs'] = ['it']
89         subtitles = self.getSubtitles()
90         self.assertTrue(subtitles['it'] is not None)
91
92     def test_youtube_translated_subtitles(self):
93         # This video has a subtitles track, which can be translated
94         self.url = 'Ky9eprVWzlI'
95         self.DL.params['writeautomaticsub'] = True
96         self.DL.params['subtitleslangs'] = ['it']
97         subtitles = self.getSubtitles()
98         self.assertTrue(subtitles['it'] is not None)
99
100     def test_youtube_nosubtitles(self):
101         self.DL.expect_warning('video doesn\'t have subtitles')
102         self.url = 'n5BB19UTcdA'
103         self.DL.params['writesubtitles'] = True
104         self.DL.params['allsubtitles'] = True
105         subtitles = self.getSubtitles()
106         self.assertFalse(subtitles)
107
108
109 class TestDailymotionSubtitles(BaseTestSubtitles):
110     url = 'http://www.dailymotion.com/video/xczg00'
111     IE = DailymotionIE
112
113     def test_allsubtitles(self):
114         self.DL.params['writesubtitles'] = True
115         self.DL.params['allsubtitles'] = True
116         subtitles = self.getSubtitles()
117         self.assertTrue(len(subtitles.keys()) >= 6)
118         self.assertEqual(md5(subtitles['en']), '976553874490cba125086bbfea3ff76f')
119         self.assertEqual(md5(subtitles['fr']), '594564ec7d588942e384e920e5341792')
120         for lang in ['es', 'fr', 'de']:
121             self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
122
123     def test_nosubtitles(self):
124         self.DL.expect_warning('video doesn\'t have subtitles')
125         self.url = 'http://www.dailymotion.com/video/x12u166_le-zapping-tele-star-du-08-aout-2013_tv'
126         self.DL.params['writesubtitles'] = True
127         self.DL.params['allsubtitles'] = True
128         subtitles = self.getSubtitles()
129         self.assertFalse(subtitles)
130
131
132 class TestTedSubtitles(BaseTestSubtitles):
133     url = 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html'
134     IE = TEDIE
135
136     def test_allsubtitles(self):
137         self.DL.params['writesubtitles'] = True
138         self.DL.params['allsubtitles'] = True
139         subtitles = self.getSubtitles()
140         self.assertTrue(len(subtitles.keys()) >= 28)
141         self.assertEqual(md5(subtitles['en']), '4262c1665ff928a2dada178f62cb8d14')
142         self.assertEqual(md5(subtitles['fr']), '66a63f7f42c97a50f8c0e90bc7797bb5')
143         for lang in ['es', 'fr', 'de']:
144             self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang)
145
146
147 class TestBlipTVSubtitles(BaseTestSubtitles):
148     url = 'http://blip.tv/a/a-6603250'
149     IE = BlipTVIE
150
151     def test_allsubtitles(self):
152         self.DL.params['writesubtitles'] = True
153         self.DL.params['allsubtitles'] = True
154         subtitles = self.getSubtitles()
155         self.assertEqual(set(subtitles.keys()), set(['en']))
156         self.assertEqual(md5(subtitles['en']), '5b75c300af65fe4476dff79478bb93e4')
157
158
159 class TestVimeoSubtitles(BaseTestSubtitles):
160     url = 'http://vimeo.com/76979871'
161     IE = VimeoIE
162
163     def test_allsubtitles(self):
164         self.DL.params['writesubtitles'] = True
165         self.DL.params['allsubtitles'] = True
166         subtitles = self.getSubtitles()
167         self.assertEqual(set(subtitles.keys()), set(['de', 'en', 'es', 'fr']))
168         self.assertEqual(md5(subtitles['en']), '8062383cf4dec168fc40a088aa6d5888')
169         self.assertEqual(md5(subtitles['fr']), 'b6191146a6c5d3a452244d853fde6dc8')
170
171     def test_nosubtitles(self):
172         self.DL.expect_warning('video doesn\'t have subtitles')
173         self.url = 'http://vimeo.com/56015672'
174         self.DL.params['writesubtitles'] = True
175         self.DL.params['allsubtitles'] = True
176         subtitles = self.getSubtitles()
177         self.assertFalse(subtitles)
178
179
180 class TestWallaSubtitles(BaseTestSubtitles):
181     url = 'http://vod.walla.co.il/movie/2705958/the-yes-men'
182     IE = WallaIE
183
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(['heb']))
190         self.assertEqual(md5(subtitles['heb']), 'e758c5d7cb982f6bef14f377ec7a3920')
191
192     def test_nosubtitles(self):
193         self.DL.expect_warning('video doesn\'t have subtitles')
194         self.url = 'http://vod.walla.co.il/movie/2642630/one-direction-all-for-one'
195         self.DL.params['writesubtitles'] = True
196         self.DL.params['allsubtitles'] = True
197         subtitles = self.getSubtitles()
198         self.assertFalse(subtitles)
199
200
201 class TestCeskaTelevizeSubtitles(BaseTestSubtitles):
202     url = 'http://www.ceskatelevize.cz/ivysilani/10600540290-u6-uzasny-svet-techniky'
203     IE = CeskaTelevizeIE
204
205     def test_allsubtitles(self):
206         self.DL.expect_warning('Automatic Captions not supported by this server')
207         self.DL.params['writesubtitles'] = True
208         self.DL.params['allsubtitles'] = True
209         subtitles = self.getSubtitles()
210         self.assertEqual(set(subtitles.keys()), set(['cs']))
211         self.assertTrue(len(subtitles['cs']) > 20000)
212
213     def test_nosubtitles(self):
214         self.DL.expect_warning('video doesn\'t have subtitles')
215         self.url = 'http://www.ceskatelevize.cz/ivysilani/ivysilani/10441294653-hyde-park-civilizace/214411058091220'
216         self.DL.params['writesubtitles'] = True
217         self.DL.params['allsubtitles'] = True
218         subtitles = self.getSubtitles()
219         self.assertFalse(subtitles)
220
221
222 class TestLyndaSubtitles(BaseTestSubtitles):
223     url = 'http://www.lynda.com/Bootstrap-tutorials/Using-exercise-files/110885/114408-4.html'
224     IE = LyndaIE
225
226     def test_allsubtitles(self):
227         self.DL.params['writesubtitles'] = True
228         self.DL.params['allsubtitles'] = True
229         subtitles = self.getSubtitles()
230         self.assertEqual(set(subtitles.keys()), set(['en']))
231         self.assertEqual(md5(subtitles['en']), '09bbe67222259bed60deaa26997d73a7')
232
233
234 class TestNPOSubtitles(BaseTestSubtitles):
235     url = 'http://www.npo.nl/nos-journaal/28-08-2014/POW_00722860'
236     IE = NPOIE
237
238     def test_allsubtitles(self):
239         self.DL.params['writesubtitles'] = True
240         self.DL.params['allsubtitles'] = True
241         subtitles = self.getSubtitles()
242         self.assertEqual(set(subtitles.keys()), set(['nl']))
243         self.assertEqual(md5(subtitles['nl']), 'fc6435027572b63fb4ab143abd5ad3f4')
244
245
246 class TestMTVSubtitles(BaseTestSubtitles):
247     url = 'http://www.cc.com/video-clips/kllhuv/stand-up-greg-fitzsimmons--uncensored---too-good-of-a-mother'
248     IE = ComedyCentralIE
249
250     def getInfoDict(self):
251         return super(TestMTVSubtitles, self).getInfoDict()['entries'][0]
252
253     def test_allsubtitles(self):
254         self.DL.params['writesubtitles'] = True
255         self.DL.params['allsubtitles'] = True
256         subtitles = self.getSubtitles()
257         self.assertEqual(set(subtitles.keys()), set(['en']))
258         self.assertEqual(md5(subtitles['en']), 'b9f6ca22a6acf597ec76f61749765e65')
259
260
261 class TestNRKSubtitles(BaseTestSubtitles):
262     url = 'http://tv.nrk.no/serie/ikke-gjoer-dette-hjemme/DMPV73000411/sesong-2/episode-1'
263     IE = NRKTVIE
264
265     def test_allsubtitles(self):
266         self.DL.params['writesubtitles'] = True
267         self.DL.params['allsubtitles'] = True
268         subtitles = self.getSubtitles()
269         self.assertEqual(set(subtitles.keys()), set(['no']))
270         self.assertEqual(md5(subtitles['no']), '544fa917d3197fcbee64634559221cc2')
271
272
273 class TestRaiSubtitles(BaseTestSubtitles):
274     url = 'http://www.rai.tv/dl/RaiTV/programmi/media/ContentItem-cb27157f-9dd0-4aee-b788-b1f67643a391.html'
275     IE = RaiIE
276
277     def test_allsubtitles(self):
278         self.DL.params['writesubtitles'] = True
279         self.DL.params['allsubtitles'] = True
280         subtitles = self.getSubtitles()
281         self.assertEqual(set(subtitles.keys()), set(['it']))
282         self.assertEqual(md5(subtitles['it']), 'b1d90a98755126b61e667567a1f6680a')
283
284
285 class TestVikiSubtitles(BaseTestSubtitles):
286     url = 'http://www.viki.com/videos/1060846v-punch-episode-18'
287     IE = VikiIE
288
289     def test_allsubtitles(self):
290         self.DL.params['writesubtitles'] = True
291         self.DL.params['allsubtitles'] = True
292         subtitles = self.getSubtitles()
293         self.assertEqual(set(subtitles.keys()), set(['en']))
294         self.assertEqual(md5(subtitles['en']), '53cb083a5914b2d84ef1ab67b880d18a')
295
296
297 class TestThePlatformSubtitles(BaseTestSubtitles):
298     # from http://www.3playmedia.com/services-features/tools/integrations/theplatform/
299     # (see http://theplatform.com/about/partners/type/subtitles-closed-captioning/)
300     url = 'theplatform:JFUjUE1_ehvq'
301     IE = ThePlatformIE
302
303     def test_allsubtitles(self):
304         self.DL.params['writesubtitles'] = True
305         self.DL.params['allsubtitles'] = True
306         subtitles = self.getSubtitles()
307         self.assertEqual(set(subtitles.keys()), set(['en']))
308         self.assertEqual(md5(subtitles['en']), '97e7670cbae3c4d26ae8bcc7fdd78d4b')
309
310
311 class TestThePlatformFeedSubtitles(BaseTestSubtitles):
312     url = 'http://feed.theplatform.com/f/7wvmTC/msnbc_video-p-test?form=json&pretty=true&range=-40&byGuid=n_hardball_5biden_140207'
313     IE = ThePlatformFeedIE
314
315     def test_allsubtitles(self):
316         self.DL.params['writesubtitles'] = True
317         self.DL.params['allsubtitles'] = True
318         subtitles = self.getSubtitles()
319         self.assertEqual(set(subtitles.keys()), set(['en']))
320         self.assertEqual(md5(subtitles['en']), '48649a22e82b2da21c9a67a395eedade')
321
322
323 class TestRtveSubtitles(BaseTestSubtitles):
324     url = 'http://www.rtve.es/alacarta/videos/los-misterios-de-laura/misterios-laura-capitulo-32-misterio-del-numero-17-2-parte/2428621/'
325     IE = RTVEALaCartaIE
326
327     def test_allsubtitles(self):
328         print('Skipping, only available from Spain')
329         return
330         self.DL.params['writesubtitles'] = True
331         self.DL.params['allsubtitles'] = True
332         subtitles = self.getSubtitles()
333         self.assertEqual(set(subtitles.keys()), set(['es']))
334         self.assertEqual(md5(subtitles['es']), '69e70cae2d40574fb7316f31d6eb7fca')
335
336
337 class TestFunnyOrDieSubtitles(BaseTestSubtitles):
338     url = 'http://www.funnyordie.com/videos/224829ff6d/judd-apatow-will-direct-your-vine'
339     IE = FunnyOrDieIE
340
341     def test_allsubtitles(self):
342         self.DL.params['writesubtitles'] = True
343         self.DL.params['allsubtitles'] = True
344         subtitles = self.getSubtitles()
345         self.assertEqual(set(subtitles.keys()), set(['en']))
346         self.assertEqual(md5(subtitles['en']), 'c5593c193eacd353596c11c2d4f9ecc4')
347
348
349 if __name__ == '__main__':
350     unittest.main()