]> Raphaƫl G. Git Repositories - youtubedl/blob - test/test_youtube_subtitles.py
641206277bbeec22d11339bb3b143df5adff834e
[youtubedl] / test / test_youtube_subtitles.py
1 #!/usr/bin/env python
2
3 import sys
4 import unittest
5 import json
6 import io
7 import hashlib
8
9 # Allow direct execution
10 import os
11 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
12
13 from youtube_dl.extractor import YoutubeIE
14 from youtube_dl.utils import *
15 from helper import FakeYDL
16
17 md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest()
18
19 class TestYoutubeSubtitles(unittest.TestCase):
20 def setUp(self):
21 DL = FakeYDL()
22 DL.params['allsubtitles'] = False
23 DL.params['writesubtitles'] = False
24 DL.params['subtitlesformat'] = 'srt'
25 DL.params['listsubtitles'] = False
26 def test_youtube_no_subtitles(self):
27 DL = FakeYDL()
28 DL.params['writesubtitles'] = False
29 IE = YoutubeIE(DL)
30 info_dict = IE.extract('QRS8MkLhQmM')
31 subtitles = info_dict[0]['subtitles']
32 self.assertEqual(subtitles, None)
33 def test_youtube_subtitles(self):
34 DL = FakeYDL()
35 DL.params['writesubtitles'] = True
36 IE = YoutubeIE(DL)
37 info_dict = IE.extract('QRS8MkLhQmM')
38 sub = info_dict[0]['subtitles']['en']
39 self.assertEqual(md5(sub), '4cd9278a35ba2305f47354ee13472260')
40 def test_youtube_subtitles_it(self):
41 DL = FakeYDL()
42 DL.params['writesubtitles'] = True
43 DL.params['subtitleslangs'] = ['it']
44 IE = YoutubeIE(DL)
45 info_dict = IE.extract('QRS8MkLhQmM')
46 sub = info_dict[0]['subtitles']['it']
47 self.assertEqual(md5(sub), '164a51f16f260476a05b50fe4c2f161d')
48 def test_youtube_onlysubtitles(self):
49 DL = FakeYDL()
50 DL.params['writesubtitles'] = True
51 DL.params['onlysubtitles'] = True
52 IE = YoutubeIE(DL)
53 info_dict = IE.extract('QRS8MkLhQmM')
54 sub = info_dict[0]['subtitles']['en']
55 self.assertEqual(md5(sub), '4cd9278a35ba2305f47354ee13472260')
56 def test_youtube_allsubtitles(self):
57 DL = FakeYDL()
58 DL.params['allsubtitles'] = True
59 IE = YoutubeIE(DL)
60 info_dict = IE.extract('QRS8MkLhQmM')
61 subtitles = info_dict[0]['subtitles']
62 self.assertEqual(len(subtitles.keys()), 13)
63 def test_youtube_subtitles_sbv_format(self):
64 DL = FakeYDL()
65 DL.params['writesubtitles'] = True
66 DL.params['subtitlesformat'] = 'sbv'
67 IE = YoutubeIE(DL)
68 info_dict = IE.extract('QRS8MkLhQmM')
69 sub = info_dict[0]['subtitles']['en']
70 self.assertEqual(md5(sub), '13aeaa0c245a8bed9a451cb643e3ad8b')
71 def test_youtube_subtitles_vtt_format(self):
72 DL = FakeYDL()
73 DL.params['writesubtitles'] = True
74 DL.params['subtitlesformat'] = 'vtt'
75 IE = YoutubeIE(DL)
76 info_dict = IE.extract('QRS8MkLhQmM')
77 sub = info_dict[0]['subtitles']['en']
78 self.assertEqual(md5(sub), '356cdc577fde0c6783b9b822e7206ff7')
79 def test_youtube_list_subtitles(self):
80 DL = FakeYDL()
81 DL.params['listsubtitles'] = True
82 IE = YoutubeIE(DL)
83 info_dict = IE.extract('QRS8MkLhQmM')
84 self.assertEqual(info_dict, None)
85 def test_youtube_automatic_captions(self):
86 DL = FakeYDL()
87 DL.params['writeautomaticsub'] = True
88 DL.params['subtitleslangs'] = ['it']
89 IE = YoutubeIE(DL)
90 info_dict = IE.extract('8YoUxe5ncPo')
91 sub = info_dict[0]['subtitles']['it']
92 self.assertTrue(sub is not None)
93 def test_youtube_multiple_langs(self):
94 DL = FakeYDL()
95 DL.params['writesubtitles'] = True
96 langs = ['it', 'fr', 'de']
97 DL.params['subtitleslangs'] = langs
98 IE = YoutubeIE(DL)
99 subtitles = IE.extract('QRS8MkLhQmM')[0]['subtitles']
100 for lang in langs:
101 self.assertTrue(subtitles.get(lang) is not None, u'Subtitles for \'%s\' not extracted' % lang)
102
103 if __name__ == '__main__':
104 unittest.main()