]>
Raphaƫl G. Git Repositories - youtubedl/blob - test/test_subtitles.py
3 # Allow direct execution
7 sys
.path
.insert(0, os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
9 from test
.helper
import FakeYDL
, global_setup
, md5
13 from youtube_dl
.extractor
import (
20 class BaseTestSubtitles(unittest
.TestCase
):
25 self
.ie
= self
.IE(self
.DL
)
27 def getInfoDict(self
):
28 info_dict
= self
.ie
.extract(self
.url
)
31 def getSubtitles(self
):
32 info_dict
= self
.getInfoDict()
33 return info_dict
['subtitles']
36 class TestYoutubeSubtitles(BaseTestSubtitles
):
40 def getSubtitles(self
):
41 info_dict
= self
.getInfoDict()
42 return info_dict
[0]['subtitles']
44 def test_youtube_no_writesubtitles(self
):
45 self
.DL
.params
['writesubtitles'] = False
46 subtitles
= self
.getSubtitles()
47 self
.assertEqual(subtitles
, None)
49 def test_youtube_subtitles(self
):
50 self
.DL
.params
['writesubtitles'] = True
51 subtitles
= self
.getSubtitles()
52 self
.assertEqual(md5(subtitles
['en']), '4cd9278a35ba2305f47354ee13472260')
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')
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)
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')
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')
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)
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)
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)
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()
106 self
.assertTrue(subtitles
.get(lang
) is not None, u
'Subtitles for \'%s\' not extracted' % lang
)
109 class TestDailymotionSubtitles(BaseTestSubtitles
):
110 url
= 'http://www.dailymotion.com/video/xczg00'
113 def test_no_writesubtitles(self
):
114 subtitles
= self
.getSubtitles()
115 self
.assertEqual(subtitles
, None)
117 def test_subtitles(self
):
118 self
.DL
.params
['writesubtitles'] = True
119 subtitles
= self
.getSubtitles()
120 self
.assertEqual(md5(subtitles
['en']), '976553874490cba125086bbfea3ff76f')
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')
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)
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)
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)
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)
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()
161 self
.assertTrue(subtitles
.get(lang
) is not None, u
'Subtitles for \'%s\' not extracted' % lang
)
164 class TestTedSubtitles(BaseTestSubtitles
):
165 url
= 'http://www.ted.com/talks/dan_dennett_on_our_consciousness.html'
168 def test_no_writesubtitles(self
):
169 subtitles
= self
.getSubtitles()
170 self
.assertEqual(subtitles
, None)
172 def test_subtitles(self
):
173 self
.DL
.params
['writesubtitles'] = True
174 subtitles
= self
.getSubtitles()
175 self
.assertEqual(md5(subtitles
['en']), '2154f31ff9b9f89a0aa671537559c21d')
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')
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)
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)
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)
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()
208 self
.assertTrue(subtitles
.get(lang
) is not None, u
'Subtitles for \'%s\' not extracted' % lang
)
210 if __name__
== '__main__':