]>
Raphaƫl G. Git Repositories - youtubedl/blob - test/test_youtube_subtitles.py
9 # Allow direct execution
11 sys
.path
.append(os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
13 from youtube_dl
.extractor
import YoutubeIE
14 from youtube_dl
.utils
import *
15 from helper
import FakeYDL
17 md5
= lambda s
: hashlib
.md5(s
.encode('utf-8')).hexdigest()
19 class TestYoutubeSubtitles(unittest
.TestCase
):
22 self
.url
= 'QRS8MkLhQmM'
23 def getInfoDict(self
):
24 IE
= YoutubeIE(self
.DL
)
25 info_dict
= IE
.extract(self
.url
)
27 def getSubtitles(self
):
28 info_dict
= self
.getInfoDict()
29 return info_dict
[0]['subtitles']
30 def test_youtube_no_writesubtitles(self
):
31 self
.DL
.params
['writesubtitles'] = False
32 subtitles
= self
.getSubtitles()
33 self
.assertEqual(subtitles
, None)
34 def test_youtube_subtitles(self
):
35 self
.DL
.params
['writesubtitles'] = True
36 subtitles
= self
.getSubtitles()
37 self
.assertEqual(md5(subtitles
['en']), '4cd9278a35ba2305f47354ee13472260')
38 def test_youtube_subtitles_lang(self
):
39 self
.DL
.params
['writesubtitles'] = True
40 self
.DL
.params
['subtitleslangs'] = ['it']
41 subtitles
= self
.getSubtitles()
42 self
.assertEqual(md5(subtitles
['it']), '164a51f16f260476a05b50fe4c2f161d')
43 def test_youtube_allsubtitles(self
):
44 self
.DL
.params
['writesubtitles'] = True
45 self
.DL
.params
['allsubtitles'] = True
46 subtitles
= self
.getSubtitles()
47 self
.assertEqual(len(subtitles
.keys()), 13)
48 def test_youtube_subtitles_sbv_format(self
):
49 self
.DL
.params
['writesubtitles'] = True
50 self
.DL
.params
['subtitlesformat'] = 'sbv'
51 subtitles
= self
.getSubtitles()
52 self
.assertEqual(md5(subtitles
['en']), '13aeaa0c245a8bed9a451cb643e3ad8b')
53 def test_youtube_subtitles_vtt_format(self
):
54 self
.DL
.params
['writesubtitles'] = True
55 self
.DL
.params
['subtitlesformat'] = 'vtt'
56 subtitles
= self
.getSubtitles()
57 self
.assertEqual(md5(subtitles
['en']), '356cdc577fde0c6783b9b822e7206ff7')
58 def test_youtube_list_subtitles(self
):
59 self
.DL
.params
['listsubtitles'] = True
60 info_dict
= self
.getInfoDict()
61 self
.assertEqual(info_dict
, None)
62 def test_youtube_automatic_captions(self
):
63 self
.url
= '8YoUxe5ncPo'
64 self
.DL
.params
['writeautomaticsub'] = True
65 self
.DL
.params
['subtitleslangs'] = ['it']
66 subtitles
= self
.getSubtitles()
67 self
.assertTrue(subtitles
['it'] is not None)
68 def test_youtube_nosubtitles(self
):
69 self
.url
= 'sAjKT8FhjI8'
70 self
.DL
.params
['writesubtitles'] = True
71 self
.DL
.params
['allsubtitles'] = True
72 subtitles
= self
.getSubtitles()
73 self
.assertEqual(len(subtitles
), 0)
74 def test_youtube_multiple_langs(self
):
75 self
.url
= 'QRS8MkLhQmM'
76 self
.DL
.params
['writesubtitles'] = True
77 langs
= ['it', 'fr', 'de']
78 self
.DL
.params
['subtitleslangs'] = langs
79 subtitles
= self
.getSubtitles()
81 self
.assertTrue(subtitles
.get(lang
) is not None, u
'Subtitles for \'%s\' not extracted' % lang
)
83 if __name__
== '__main__':