]> Raphaƫl G. Git Repositories - youtubedl/blob - test/test_subtitles.py
Update changelog.
[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, global_setup, md5
10 global_setup()
11
12
13 from youtube_dl.extractor import (
14 YoutubeIE,
15 DailymotionIE,
16 TEDIE,
17 )
18
19
20 class BaseTestSubtitles(unittest.TestCase):
21 url = None
22 IE = None
23 def setUp(self):
24 self.DL = FakeYDL()
25 self.ie = self.IE(self.DL)
26
27 def getInfoDict(self):
28 info_dict = self.ie.extract(self.url)
29 return info_dict
30
31 def getSubtitles(self):
32 info_dict = self.getInfoDict()
33 return info_dict['subtitles']
34
35
36 class TestYoutubeSubtitles(BaseTestSubtitles):
37 url = 'QRS8MkLhQmM'
38 IE = YoutubeIE
39
40 def getSubtitles(self):
41 info_dict = self.getInfoDict()
42 return info_dict[0]['subtitles']
43
44 def test_youtube_no_writesubtitles(self):
45 self.DL.params['writesubtitles'] = False
46 subtitles = self.getSubtitles()
47 self.assertEqual(subtitles, None)
48
49 def test_youtube_subtitles(self):
50 self.DL.params['writesubtitles'] = True
51 subtitles = self.getSubtitles()
52 self.assertEqual(md5(subtitles['en']), '4cd9278a35ba2305f47354ee13472260')
53
54 def test_youtube_subtitles_lang(self):
55 self.DL.params['writesubtitles'] = True
56 self.DL.params['subtitleslangs'] = ['it']
57 subtitles = self.getSubtitles()
58 self.assertEqual(md5(subtitles['it']), '164a51f16f260476a05b50fe4c2f161d')
59
60 def test_youtube_allsubtitles(self):
61 self.DL.params['writesubtitles'] = True
62 self.DL.params['allsubtitles'] = True
63 subtitles = self.getSubtitles()
64 self.assertEqual(len(subtitles.keys()), 13)
65
66 def test_youtube_subtitles_sbv_format(self):
67 self.DL.params['writesubtitles'] = True
68 self.DL.params['subtitlesformat'] = 'sbv'
69 subtitles = self.getSubtitles()
70 self.assertEqual(md5(subtitles['en']), '13aeaa0c245a8bed9a451cb643e3ad8b')
71
72 def test_youtube_subtitles_vtt_format(self):
73 self.DL.params['writesubtitles'] = True
74 self.DL.params['subtitlesformat'] = 'vtt'
75 subtitles = self.getSubtitles()
76 self.assertEqual(md5(subtitles['en']), '356cdc577fde0c6783b9b822e7206ff7')
77
78 def test_youtube_list_subtitles(self):
79 self.DL.expect_warning(u'Video doesn\'t have automatic captions')
80 self.DL.params['listsubtitles'] = True
81 info_dict = self.getInfoDict()
82 self.assertEqual(info_dict, None)
83
84 def test_youtube_automatic_captions(self):
85 self.url = '8YoUxe5ncPo'
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
91 def test_youtube_nosubtitles(self):
92 self.DL.expect_warning(u'video doesn\'t have subtitles')
93 self.url = 'sAjKT8FhjI8'
94 self.DL.params['writesubtitles'] = True
95 self.DL.params['allsubtitles'] = True
96 subtitles = self.getSubtitles()
97 self.assertEqual(len(subtitles), 0)
98
99 def test_youtube_multiple_langs(self):
100 self.url = 'QRS8MkLhQmM'
101 self.DL.params['writesubtitles'] = True
102 langs = ['it', 'fr', 'de']
103 self.DL.params['subtitleslangs'] = langs
104 subtitles = self.getSubtitles()
105 for lang in langs:
106 self.assertTrue(subtitles.get(lang) is not None, u'Subtitles for \'%s\' not extracted' % lang)
107
108
109 class TestDailymotionSubtitles(BaseTestSubtitles):
110 url = 'http://www.dailymotion.com/video/xczg00'
111 IE = DailymotionIE
112
113 def test_no_writesubtitles(self):
114 subtitles = self.getSubtitles()
115 self.assertEqual(subtitles, None)
116
117 def test_subtitles(self):
118 self.DL.params['writesubtitles'] = True
119 subtitles = self.getSubtitles()
120 self.assertEqual(md5(subtitles['en']), '976553874490cba125086bbfea3ff76f')
121
122 def test_subtitles_lang(self):
123 self.DL.params['writesubtitles'] = True
124 self.DL.params['subtitleslangs'] = ['fr']
125 subtitles = self.getSubtitles()
126 self.assertEqual(md5(subtitles['fr']), '594564ec7d588942e384e920e5341792')
127
128 def test_allsubtitles(self):
129 self.DL.params['writesubtitles'] = True
130 self.DL.params['allsubtitles'] = True
131 subtitles = self.getSubtitles()
132 self.assertEqual(len(subtitles.keys()), 5)
133
134 def test_list_subtitles(self):
135 self.DL.expect_warning(u'Automatic Captions not supported by this server')
136 self.DL.params['listsubtitles'] = True
137 info_dict = self.getInfoDict()
138 self.assertEqual(info_dict, None)
139
140 def test_automatic_captions(self):
141 self.DL.expect_warning(u'Automatic Captions not supported by this server')
142 self.DL.params['writeautomaticsub'] = True
143 self.DL.params['subtitleslang'] = ['en']
144 subtitles = self.getSubtitles()
145 self.assertTrue(len(subtitles.keys()) == 0)
146
147 def test_nosubtitles(self):
148 self.DL.expect_warning(u'video doesn\'t have subtitles')
149 self.url = 'http://www.dailymotion.com/video/x12u166_le-zapping-tele-star-du-08-aout-2013_tv'
150 self.DL.params['writesubtitles'] = True
151 self.DL.params['allsubtitles'] = True
152 subtitles = self.getSubtitles()
153 self.assertEqual(len(subtitles), 0)
154
155 def test_multiple_langs(self):
156 self.DL.params['writesubtitles'] = True
157 langs = ['es', 'fr', 'de']
158 self.DL.params['subtitleslangs'] = langs
159 subtitles = self.getSubtitles()
160 for lang in langs:
161 self.assertTrue(subtitles.get(lang) is not None, u'Subtitles for \'%s\' not extracted' % lang)
162
163
164 class TestTedSubtitles(BaseTestSubtitles):
165 url = 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html'
166 IE = TEDIE
167
168 def test_no_writesubtitles(self):
169 subtitles = self.getSubtitles()
170 self.assertEqual(subtitles, None)
171
172 def test_subtitles(self):
173 self.DL.params['writesubtitles'] = True
174 subtitles = self.getSubtitles()
175 self.assertEqual(md5(subtitles['en']), '2154f31ff9b9f89a0aa671537559c21d')
176
177 def test_subtitles_lang(self):
178 self.DL.params['writesubtitles'] = True
179 self.DL.params['subtitleslangs'] = ['fr']
180 subtitles = self.getSubtitles()
181 self.assertEqual(md5(subtitles['fr']), '7616cbc6df20ec2c1204083c83871cf6')
182
183 def test_allsubtitles(self):
184 self.DL.params['writesubtitles'] = True
185 self.DL.params['allsubtitles'] = True
186 subtitles = self.getSubtitles()
187 self.assertEqual(len(subtitles.keys()), 28)
188
189 def test_list_subtitles(self):
190 self.DL.expect_warning(u'Automatic Captions not supported by this server')
191 self.DL.params['listsubtitles'] = True
192 info_dict = self.getInfoDict()
193 self.assertEqual(info_dict, None)
194
195 def test_automatic_captions(self):
196 self.DL.expect_warning(u'Automatic Captions not supported by this server')
197 self.DL.params['writeautomaticsub'] = True
198 self.DL.params['subtitleslang'] = ['en']
199 subtitles = self.getSubtitles()
200 self.assertTrue(len(subtitles.keys()) == 0)
201
202 def test_multiple_langs(self):
203 self.DL.params['writesubtitles'] = True
204 langs = ['es', 'fr', 'de']
205 self.DL.params['subtitleslangs'] = langs
206 subtitles = self.getSubtitles()
207 for lang in langs:
208 self.assertTrue(subtitles.get(lang) is not None, u'Subtitles for \'%s\' not extracted' % lang)
209
210 if __name__ == '__main__':
211 unittest.main()