]>
Commit | Line | Data |
---|---|---|
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 | ) | |
22 | ||
23 | ||
24 | class BaseTestSubtitles(unittest.TestCase): | |
25 | url = None | |
26 | IE = None | |
27 | ||
28 | def setUp(self): | |
29 | self.DL = FakeYDL() | |
30 | self.ie = self.IE() | |
31 | self.DL.add_info_extractor(self.ie) | |
32 | ||
33 | def getInfoDict(self): | |
34 | info_dict = self.DL.extract_info(self.url, download=False) | |
35 | return info_dict | |
36 | ||
37 | def getSubtitles(self): | |
38 | info_dict = self.getInfoDict() | |
39 | subtitles = info_dict['requested_subtitles'] | |
40 | if not subtitles: | |
41 | return subtitles | |
42 | for sub_info in subtitles.values(): | |
43 | if sub_info.get('data') is None: | |
44 | uf = self.DL.urlopen(sub_info['url']) | |
45 | sub_info['data'] = uf.read().decode('utf-8') | |
46 | return dict((l, sub_info['data']) for l, sub_info in subtitles.items()) | |
47 | ||
48 | ||
49 | class TestYoutubeSubtitles(BaseTestSubtitles): | |
50 | url = 'QRS8MkLhQmM' | |
51 | IE = YoutubeIE | |
52 | ||
53 | def test_youtube_subtitles(self): | |
54 | self.DL.params['writesubtitles'] = True | |
55 | subtitles = self.getSubtitles() | |
56 | self.assertEqual(md5(subtitles['en']), '4cd9278a35ba2305f47354ee13472260') | |
57 | ||
58 | def test_youtube_subtitles_lang(self): | |
59 | self.DL.params['writesubtitles'] = True | |
60 | self.DL.params['subtitleslangs'] = ['it'] | |
61 | subtitles = self.getSubtitles() | |
62 | self.assertEqual(md5(subtitles['it']), '164a51f16f260476a05b50fe4c2f161d') | |
63 | ||
64 | def test_youtube_allsubtitles(self): | |
65 | self.DL.params['writesubtitles'] = True | |
66 | self.DL.params['allsubtitles'] = True | |
67 | subtitles = self.getSubtitles() | |
68 | self.assertEqual(len(subtitles.keys()), 13) | |
69 | ||
70 | def test_youtube_subtitles_sbv_format(self): | |
71 | self.DL.params['writesubtitles'] = True | |
72 | self.DL.params['subtitlesformat'] = 'sbv' | |
73 | subtitles = self.getSubtitles() | |
74 | self.assertEqual(md5(subtitles['en']), '13aeaa0c245a8bed9a451cb643e3ad8b') | |
75 | ||
76 | def test_youtube_subtitles_vtt_format(self): | |
77 | self.DL.params['writesubtitles'] = True | |
78 | self.DL.params['subtitlesformat'] = 'vtt' | |
79 | subtitles = self.getSubtitles() | |
80 | self.assertEqual(md5(subtitles['en']), '3cb210999d3e021bd6c7f0ea751eab06') | |
81 | ||
82 | def test_youtube_list_subtitles(self): | |
83 | self.DL.expect_warning('Video doesn\'t have automatic captions') | |
84 | self.DL.params['listsubtitles'] = True | |
85 | info_dict = self.getInfoDict() | |
86 | self.assertEqual(info_dict, None) | |
87 | ||
88 | def test_youtube_automatic_captions(self): | |
89 | self.url = '8YoUxe5ncPo' | |
90 | self.DL.params['writeautomaticsub'] = True | |
91 | self.DL.params['subtitleslangs'] = ['it'] | |
92 | subtitles = self.getSubtitles() | |
93 | self.assertTrue(subtitles['it'] is not None) | |
94 | ||
95 | def test_youtube_translated_subtitles(self): | |
96 | # This video has a subtitles track, which can be translated | |
97 | self.url = 'Ky9eprVWzlI' | |
98 | self.DL.params['writeautomaticsub'] = True | |
99 | self.DL.params['subtitleslangs'] = ['it'] | |
100 | subtitles = self.getSubtitles() | |
101 | self.assertTrue(subtitles['it'] is not None) | |
102 | ||
103 | def test_youtube_nosubtitles(self): | |
104 | self.DL.expect_warning('video doesn\'t have subtitles') | |
105 | self.url = 'n5BB19UTcdA' | |
106 | self.DL.params['writesubtitles'] = True | |
107 | self.DL.params['allsubtitles'] = True | |
108 | subtitles = self.getSubtitles() | |
109 | self.assertEqual(len(subtitles), 0) | |
110 | ||
111 | def test_youtube_multiple_langs(self): | |
112 | self.url = 'QRS8MkLhQmM' | |
113 | self.DL.params['writesubtitles'] = True | |
114 | langs = ['it', 'fr', 'de'] | |
115 | self.DL.params['subtitleslangs'] = langs | |
116 | subtitles = self.getSubtitles() | |
117 | for lang in langs: | |
118 | self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang) | |
119 | ||
120 | ||
121 | class TestDailymotionSubtitles(BaseTestSubtitles): | |
122 | url = 'http://www.dailymotion.com/video/xczg00' | |
123 | IE = DailymotionIE | |
124 | ||
125 | def test_subtitles(self): | |
126 | self.DL.params['writesubtitles'] = True | |
127 | subtitles = self.getSubtitles() | |
128 | self.assertEqual(md5(subtitles['en']), '976553874490cba125086bbfea3ff76f') | |
129 | ||
130 | def test_subtitles_lang(self): | |
131 | self.DL.params['writesubtitles'] = True | |
132 | self.DL.params['subtitleslangs'] = ['fr'] | |
133 | subtitles = self.getSubtitles() | |
134 | self.assertEqual(md5(subtitles['fr']), '594564ec7d588942e384e920e5341792') | |
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()) >= 6) | |
141 | ||
142 | def test_nosubtitles(self): | |
143 | self.DL.expect_warning('video doesn\'t have subtitles') | |
144 | self.url = 'http://www.dailymotion.com/video/x12u166_le-zapping-tele-star-du-08-aout-2013_tv' | |
145 | self.DL.params['writesubtitles'] = True | |
146 | self.DL.params['allsubtitles'] = True | |
147 | subtitles = self.getSubtitles() | |
148 | self.assertEqual(len(subtitles), 0) | |
149 | ||
150 | def test_multiple_langs(self): | |
151 | self.DL.params['writesubtitles'] = True | |
152 | langs = ['es', 'fr', 'de'] | |
153 | self.DL.params['subtitleslangs'] = langs | |
154 | subtitles = self.getSubtitles() | |
155 | for lang in langs: | |
156 | self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang) | |
157 | ||
158 | ||
159 | class TestTedSubtitles(BaseTestSubtitles): | |
160 | url = 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html' | |
161 | IE = TEDIE | |
162 | ||
163 | def test_no_writesubtitles(self): | |
164 | subtitles = self.getSubtitles() | |
165 | self.assertFalse(subtitles) | |
166 | ||
167 | def test_subtitles(self): | |
168 | self.DL.params['writesubtitles'] = True | |
169 | subtitles = self.getSubtitles() | |
170 | self.assertEqual(md5(subtitles['en']), '4262c1665ff928a2dada178f62cb8d14') | |
171 | ||
172 | def test_subtitles_lang(self): | |
173 | self.DL.params['writesubtitles'] = True | |
174 | self.DL.params['subtitleslangs'] = ['fr'] | |
175 | subtitles = self.getSubtitles() | |
176 | self.assertEqual(md5(subtitles['fr']), '66a63f7f42c97a50f8c0e90bc7797bb5') | |
177 | ||
178 | def test_allsubtitles(self): | |
179 | self.DL.params['writesubtitles'] = True | |
180 | self.DL.params['allsubtitles'] = True | |
181 | subtitles = self.getSubtitles() | |
182 | self.assertTrue(len(subtitles.keys()) >= 28) | |
183 | ||
184 | def test_list_subtitles(self): | |
185 | self.DL.params['listsubtitles'] = True | |
186 | info_dict = self.getInfoDict() | |
187 | self.assertEqual(info_dict, None) | |
188 | ||
189 | def test_multiple_langs(self): | |
190 | self.DL.params['writesubtitles'] = True | |
191 | langs = ['es', 'fr', 'de'] | |
192 | self.DL.params['subtitleslangs'] = langs | |
193 | subtitles = self.getSubtitles() | |
194 | for lang in langs: | |
195 | self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang) | |
196 | ||
197 | ||
198 | class TestBlipTVSubtitles(BaseTestSubtitles): | |
199 | url = 'http://blip.tv/a/a-6603250' | |
200 | IE = BlipTVIE | |
201 | ||
202 | def test_list_subtitles(self): | |
203 | self.DL.params['listsubtitles'] = True | |
204 | info_dict = self.getInfoDict() | |
205 | self.assertEqual(info_dict, None) | |
206 | ||
207 | def test_allsubtitles(self): | |
208 | self.DL.params['writesubtitles'] = True | |
209 | self.DL.params['allsubtitles'] = True | |
210 | subtitles = self.getSubtitles() | |
211 | self.assertEqual(set(subtitles.keys()), set(['en'])) | |
212 | self.assertEqual(md5(subtitles['en']), '5b75c300af65fe4476dff79478bb93e4') | |
213 | ||
214 | ||
215 | class TestVimeoSubtitles(BaseTestSubtitles): | |
216 | url = 'http://vimeo.com/76979871' | |
217 | IE = VimeoIE | |
218 | ||
219 | def test_subtitles(self): | |
220 | self.DL.params['writesubtitles'] = True | |
221 | subtitles = self.getSubtitles() | |
222 | self.assertEqual(md5(subtitles['en']), '8062383cf4dec168fc40a088aa6d5888') | |
223 | ||
224 | def test_subtitles_lang(self): | |
225 | self.DL.params['writesubtitles'] = True | |
226 | self.DL.params['subtitleslangs'] = ['fr'] | |
227 | subtitles = self.getSubtitles() | |
228 | self.assertEqual(md5(subtitles['fr']), 'b6191146a6c5d3a452244d853fde6dc8') | |
229 | ||
230 | def test_allsubtitles(self): | |
231 | self.DL.params['writesubtitles'] = True | |
232 | self.DL.params['allsubtitles'] = True | |
233 | subtitles = self.getSubtitles() | |
234 | self.assertEqual(set(subtitles.keys()), set(['de', 'en', 'es', 'fr'])) | |
235 | ||
236 | def test_nosubtitles(self): | |
237 | self.DL.expect_warning('video doesn\'t have subtitles') | |
238 | self.url = 'http://vimeo.com/56015672' | |
239 | self.DL.params['writesubtitles'] = True | |
240 | self.DL.params['allsubtitles'] = True | |
241 | subtitles = self.getSubtitles() | |
242 | self.assertEqual(len(subtitles), 0) | |
243 | ||
244 | def test_multiple_langs(self): | |
245 | self.DL.params['writesubtitles'] = True | |
246 | langs = ['es', 'fr', 'de'] | |
247 | self.DL.params['subtitleslangs'] = langs | |
248 | subtitles = self.getSubtitles() | |
249 | for lang in langs: | |
250 | self.assertTrue(subtitles.get(lang) is not None, 'Subtitles for \'%s\' not extracted' % lang) | |
251 | ||
252 | ||
253 | class TestWallaSubtitles(BaseTestSubtitles): | |
254 | url = 'http://vod.walla.co.il/movie/2705958/the-yes-men' | |
255 | IE = WallaIE | |
256 | ||
257 | def test_list_subtitles(self): | |
258 | self.DL.expect_warning('Automatic Captions not supported by this server') | |
259 | self.DL.params['listsubtitles'] = True | |
260 | info_dict = self.getInfoDict() | |
261 | self.assertEqual(info_dict, None) | |
262 | ||
263 | def test_allsubtitles(self): | |
264 | self.DL.expect_warning('Automatic Captions not supported by this server') | |
265 | self.DL.params['writesubtitles'] = True | |
266 | self.DL.params['allsubtitles'] = True | |
267 | subtitles = self.getSubtitles() | |
268 | self.assertEqual(set(subtitles.keys()), set(['heb'])) | |
269 | self.assertEqual(md5(subtitles['heb']), 'e758c5d7cb982f6bef14f377ec7a3920') | |
270 | ||
271 | def test_nosubtitles(self): | |
272 | self.DL.expect_warning('video doesn\'t have subtitles') | |
273 | self.url = 'http://vod.walla.co.il/movie/2642630/one-direction-all-for-one' | |
274 | self.DL.params['writesubtitles'] = True | |
275 | self.DL.params['allsubtitles'] = True | |
276 | subtitles = self.getSubtitles() | |
277 | self.assertEqual(len(subtitles), 0) | |
278 | ||
279 | ||
280 | class TestCeskaTelevizeSubtitles(BaseTestSubtitles): | |
281 | url = 'http://www.ceskatelevize.cz/ivysilani/10600540290-u6-uzasny-svet-techniky' | |
282 | IE = CeskaTelevizeIE | |
283 | ||
284 | def test_list_subtitles(self): | |
285 | self.DL.expect_warning('Automatic Captions not supported by this server') | |
286 | self.DL.params['listsubtitles'] = True | |
287 | info_dict = self.getInfoDict() | |
288 | self.assertEqual(info_dict, None) | |
289 | ||
290 | def test_allsubtitles(self): | |
291 | self.DL.expect_warning('Automatic Captions not supported by this server') | |
292 | self.DL.params['writesubtitles'] = True | |
293 | self.DL.params['allsubtitles'] = True | |
294 | subtitles = self.getSubtitles() | |
295 | self.assertEqual(set(subtitles.keys()), set(['cs'])) | |
296 | self.assertTrue(len(subtitles['cs']) > 20000) | |
297 | ||
298 | def test_nosubtitles(self): | |
299 | self.DL.expect_warning('video doesn\'t have subtitles') | |
300 | self.url = 'http://www.ceskatelevize.cz/ivysilani/ivysilani/10441294653-hyde-park-civilizace/214411058091220' | |
301 | self.DL.params['writesubtitles'] = True | |
302 | self.DL.params['allsubtitles'] = True | |
303 | subtitles = self.getSubtitles() | |
304 | self.assertEqual(len(subtitles), 0) | |
305 | ||
306 | ||
307 | if __name__ == '__main__': | |
308 | unittest.main() |