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_subtitles(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_it(self):
39 self.DL.params['writesubtitles'] = True
40 self.DL.params['subtitleslang'] = 'it'
41 subtitles = self.getSubtitles()
42 self.assertEqual(md5(subtitles['it']), '164a51f16f260476a05b50fe4c2f161d')
43 def test_youtube_allsubtitles(self):
44 self.DL.params['allsubtitles'] = True
45 subtitles = self.getSubtitles()
46 self.assertEqual(len(subtitles.keys()), 13)
47 def test_youtube_subtitles_sbv_format(self):
48 self.DL.params['writesubtitles'] = True
49 self.DL.params['subtitlesformat'] = 'sbv'
50 subtitles = self.getSubtitles()
51 self.assertEqual(md5(subtitles['en']), '13aeaa0c245a8bed9a451cb643e3ad8b')
52 def test_youtube_subtitles_vtt_format(self):
53 self.DL.params['writesubtitles'] = True
54 self.DL.params['subtitlesformat'] = 'vtt'
55 subtitles = self.getSubtitles()
56 self.assertEqual(md5(subtitles['en']), '356cdc577fde0c6783b9b822e7206ff7')
57 def test_youtube_list_subtitles(self):
58 self.DL.params['listsubtitles'] = True
59 info_dict = self.getInfoDict()
60 self.assertEqual(info_dict, None)
61 def test_youtube_automatic_captions(self):
62 self.url = '8YoUxe5ncPo'
63 self.DL.params['writeautomaticsub'] = True
64 self.DL.params['subtitleslang'] = 'it'
65 subtitles = self.getSubtitles()
66 self.assertTrue(subtitles['it'] is not None)
68 if __name__ == '__main__':