]> Raphaƫl G. Git Repositories - youtubedl/blob - test/test_subtitles.py
debian/changelog: Start new release.
[youtubedl] / test / test_subtitles.py
1 #!/usr/bin/env python
2
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
9 from test.helper import FakeYDL, md5
10
11
12 from youtube_dl.extractor import (
13 BlipTVIE,
14 YoutubeIE,
15 DailymotionIE,
16 TEDIE,
17 VimeoIE,
18 WallaIE,
19 )
20
21
22 class BaseTestSubtitles(unittest.TestCase):
23 url = None
24 IE = None
25 def setUp(self):
26 self.DL = FakeYDL()
27 self.ie = self.IE(self.DL)
28
29 def getInfoDict(self):
30 info_dict = self.ie.extract(self.url)
31 return info_dict
32
33 def getSubtitles(self):
34 info_dict = self.getInfoDict()
35 return info_dict['subtitles']
36
37
38 class TestYoutubeSubtitles(BaseTestSubtitles):
39 url = 'QRS8MkLhQmM'
40 IE = YoutubeIE
41
42 def test_youtube_no_writesubtitles(self):
43 self.DL.params['writesubtitles'] = False
44 subtitles = self.getSubtitles()
45 self.assertEqual(subtitles, None)
46
47 def test_youtube_subtitles(self):
48 self.DL.params['writesubtitles'] = True
49 subtitles = self.getSubtitles()
50 self.assertEqual(md5(subtitles['en']), '4cd9278a35ba2305f47354ee13472260')
51
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
58 def test_youtube_allsubtitles(self):
59 self.DL.params['writesubtitles'] = True
60 self.DL.params['allsubtitles'] = True
61 subtitles = self.getSubtitles()
62 self.assertEqual(len(subtitles.keys()), 13)
63
64 def test_youtube_subtitles_sbv_format(self):
65 self.DL.params['writesubtitles'] = True
66 self.DL.params['subtitlesformat'] = 'sbv'
67 subtitles = self.getSubtitles()
68 self.assertEqual(md5(subtitles['en']), '13aeaa0c245a8bed9a451cb643e3ad8b')
69
70 def test_youtube_subtitles_vtt_format(self):
71 self.DL.params['writesubtitles'] = True
72 self.DL.params['subtitlesformat'] = 'vtt'
73 subtitles = self.getSubtitles()
74 self.assertEqual(md5(subtitles['en']), '3cb210999d3e021bd6c7f0ea751eab06')
75
76 def test_youtube_list_subtitles(self):
77 self.DL.expect_warning(u'Video doesn\'t have automatic captions')
78 self.DL.params['listsubtitles'] = True
79 info_dict = self.getInfoDict()
80 self.assertEqual(info_dict, None)
81
82 def test_youtube_automatic_captions(self):
83 self.url = '8YoUxe5ncPo'
84 self.DL.params['writeautomaticsub'] = True
85 self.DL.params['subtitleslangs'] = ['it']
86 subtitles = self.getSubtitles()
87 self.assertTrue(subtitles['it'] is not None)
88
89 def test_youtube_nosubtitles(self):
90 self.DL.expect_warning(u'video doesn\'t have subtitles')
91 self.url = 'n5BB19UTcdA'
92 self.DL.params['writesubtitles'] = True
93 self.DL.params['allsubtitles'] = True
94 subtitles = self.getSubtitles()
95 self.assertEqual(len(subtitles), 0)
96
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, u'Subtitles for \'%s\' not extracted' % lang)
105
106
107 class TestDailymotionSubtitles(BaseTestSubtitles):
108 url = 'http://www.dailymotion.com/video/xczg00'
109 IE = DailymotionIE
110
111 def test_no_writesubtitles(self):
112 subtitles = self.getSubtitles()
113 self.assertEqual(subtitles, None)
114
115 def test_subtitles(self):
116 self.DL.params['writesubtitles'] = True
117 subtitles = self.getSubtitles()
118 self.assertEqual(md5(subtitles['en']), '976553874490cba125086bbfea3ff76f')
119
120 def test_subtitles_lang(self):
121 self.DL.params['writesubtitles'] = True
122 self.DL.params['subtitleslangs'] = ['fr']
123 subtitles = self.getSubtitles()
124 self.assertEqual(md5(subtitles['fr']), '594564ec7d588942e384e920e5341792')
125
126 def test_allsubtitles(self):
127 self.DL.params['writesubtitles'] = True
128 self.DL.params['allsubtitles'] = True
129 subtitles = self.getSubtitles()
130 self.assertEqual(len(subtitles.keys()), 5)
131
132 def test_list_subtitles(self):
133 self.DL.expect_warning(u'Automatic Captions not supported by this server')
134 self.DL.params['listsubtitles'] = True
135 info_dict = self.getInfoDict()
136 self.assertEqual(info_dict, None)
137
138 def test_automatic_captions(self):
139 self.DL.expect_warning(u'Automatic Captions not supported by this server')
140 self.DL.params['writeautomaticsub'] = True
141 self.DL.params['subtitleslang'] = ['en']
142 subtitles = self.getSubtitles()
143 self.assertTrue(len(subtitles.keys()) == 0)
144
145 def test_nosubtitles(self):
146 self.DL.expect_warning(u'video doesn\'t have subtitles')
147 self.url = 'http://www.dailymotion.com/video/x12u166_le-zapping-tele-star-du-08-aout-2013_tv'
148 self.DL.params['writesubtitles'] = True
149 self.DL.params['allsubtitles'] = True
150 subtitles = self.getSubtitles()
151 self.assertEqual(len(subtitles), 0)
152
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, u'Subtitles for \'%s\' not extracted' % lang)
160
161
162 class TestTedSubtitles(BaseTestSubtitles):
163 url = 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html'
164 IE = TEDIE
165
166 def test_no_writesubtitles(self):
167 subtitles = self.getSubtitles()
168 self.assertEqual(subtitles, None)
169
170 def test_subtitles(self):
171 self.DL.params['writesubtitles'] = True
172 subtitles = self.getSubtitles()
173 self.assertEqual(md5(subtitles['en']), '4262c1665ff928a2dada178f62cb8d14')
174
175 def test_subtitles_lang(self):
176 self.DL.params['writesubtitles'] = True
177 self.DL.params['subtitleslangs'] = ['fr']
178 subtitles = self.getSubtitles()
179 self.assertEqual(md5(subtitles['fr']), '66a63f7f42c97a50f8c0e90bc7797bb5')
180
181 def test_allsubtitles(self):
182 self.DL.params['writesubtitles'] = True
183 self.DL.params['allsubtitles'] = True
184 subtitles = self.getSubtitles()
185 self.assertTrue(len(subtitles.keys()) >= 28)
186
187 def test_list_subtitles(self):
188 self.DL.expect_warning(u'Automatic Captions not supported by this server')
189 self.DL.params['listsubtitles'] = True
190 info_dict = self.getInfoDict()
191 self.assertEqual(info_dict, None)
192
193 def test_automatic_captions(self):
194 self.DL.expect_warning(u'Automatic Captions not supported by this server')
195 self.DL.params['writeautomaticsub'] = True
196 self.DL.params['subtitleslang'] = ['en']
197 subtitles = self.getSubtitles()
198 self.assertTrue(len(subtitles.keys()) == 0)
199
200 def test_multiple_langs(self):
201 self.DL.params['writesubtitles'] = True
202 langs = ['es', 'fr', 'de']
203 self.DL.params['subtitleslangs'] = langs
204 subtitles = self.getSubtitles()
205 for lang in langs:
206 self.assertTrue(subtitles.get(lang) is not None, u'Subtitles for \'%s\' not extracted' % lang)
207
208
209 class TestBlipTVSubtitles(BaseTestSubtitles):
210 url = 'http://blip.tv/a/a-6603250'
211 IE = BlipTVIE
212
213 def test_list_subtitles(self):
214 self.DL.expect_warning(u'Automatic Captions not supported by this server')
215 self.DL.params['listsubtitles'] = True
216 info_dict = self.getInfoDict()
217 self.assertEqual(info_dict, None)
218
219 def test_allsubtitles(self):
220 self.DL.expect_warning(u'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(['en']))
225 self.assertEqual(md5(subtitles['en']), '5b75c300af65fe4476dff79478bb93e4')
226
227
228 class TestVimeoSubtitles(BaseTestSubtitles):
229 url = 'http://vimeo.com/76979871'
230 IE = VimeoIE
231
232 def test_no_writesubtitles(self):
233 subtitles = self.getSubtitles()
234 self.assertEqual(subtitles, None)
235
236 def test_subtitles(self):
237 self.DL.params['writesubtitles'] = True
238 subtitles = self.getSubtitles()
239 self.assertEqual(md5(subtitles['en']), '8062383cf4dec168fc40a088aa6d5888')
240
241 def test_subtitles_lang(self):
242 self.DL.params['writesubtitles'] = True
243 self.DL.params['subtitleslangs'] = ['fr']
244 subtitles = self.getSubtitles()
245 self.assertEqual(md5(subtitles['fr']), 'b6191146a6c5d3a452244d853fde6dc8')
246
247 def test_allsubtitles(self):
248 self.DL.params['writesubtitles'] = True
249 self.DL.params['allsubtitles'] = True
250 subtitles = self.getSubtitles()
251 self.assertEqual(set(subtitles.keys()), set(['de', 'en', 'es', 'fr']))
252
253 def test_list_subtitles(self):
254 self.DL.expect_warning(u'Automatic Captions not supported by this server')
255 self.DL.params['listsubtitles'] = True
256 info_dict = self.getInfoDict()
257 self.assertEqual(info_dict, None)
258
259 def test_automatic_captions(self):
260 self.DL.expect_warning(u'Automatic Captions not supported by this server')
261 self.DL.params['writeautomaticsub'] = True
262 self.DL.params['subtitleslang'] = ['en']
263 subtitles = self.getSubtitles()
264 self.assertTrue(len(subtitles.keys()) == 0)
265
266 def test_nosubtitles(self):
267 self.DL.expect_warning(u'video doesn\'t have subtitles')
268 self.url = 'http://vimeo.com/56015672'
269 self.DL.params['writesubtitles'] = True
270 self.DL.params['allsubtitles'] = True
271 subtitles = self.getSubtitles()
272 self.assertEqual(len(subtitles), 0)
273
274 def test_multiple_langs(self):
275 self.DL.params['writesubtitles'] = True
276 langs = ['es', 'fr', 'de']
277 self.DL.params['subtitleslangs'] = langs
278 subtitles = self.getSubtitles()
279 for lang in langs:
280 self.assertTrue(subtitles.get(lang) is not None, u'Subtitles for \'%s\' not extracted' % lang)
281
282
283 class TestWallaSubtitles(BaseTestSubtitles):
284 url = 'http://vod.walla.co.il/movie/2705958/the-yes-men'
285 IE = WallaIE
286
287 def test_list_subtitles(self):
288 self.DL.expect_warning(u'Automatic Captions not supported by this server')
289 self.DL.params['listsubtitles'] = True
290 info_dict = self.getInfoDict()
291 self.assertEqual(info_dict, None)
292
293 def test_allsubtitles(self):
294 self.DL.expect_warning(u'Automatic Captions not supported by this server')
295 self.DL.params['writesubtitles'] = True
296 self.DL.params['allsubtitles'] = True
297 subtitles = self.getSubtitles()
298 self.assertEqual(set(subtitles.keys()), set(['heb']))
299 self.assertEqual(md5(subtitles['heb']), 'e758c5d7cb982f6bef14f377ec7a3920')
300
301 def test_nosubtitles(self):
302 self.DL.expect_warning(u'video doesn\'t have subtitles')
303 self.url = 'http://vod.walla.co.il/movie/2642630/one-direction-all-for-one'
304 self.DL.params['writesubtitles'] = True
305 self.DL.params['allsubtitles'] = True
306 subtitles = self.getSubtitles()
307 self.assertEqual(len(subtitles), 0)
308
309
310 if __name__ == '__main__':
311 unittest.main()