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 youtube_dl import YoutubeDL
17 PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
18 with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
19 parameters = json.load(pf)
21 # General configuration (from __init__, not very elegant...)
22 jar = compat_cookiejar.CookieJar()
23 cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
24 proxy_handler = compat_urllib_request.ProxyHandler()
25 opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
26 compat_urllib_request.install_opener(opener)
28 class FakeYDL(YoutubeDL):
31 # Different instances of the downloader can't share the same dictionary
32 # some test set the "sublang" parameter, which would break the md5 checks.
33 self.params = dict(parameters)
34 def to_screen(self, s):
36 def trouble(self, s, tb=None):
38 def download(self, x):
41 md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest()
43 class TestYoutubeSubtitles(unittest.TestCase):
46 DL.params['allsubtitles'] = False
47 DL.params['writesubtitles'] = False
48 DL.params['subtitlesformat'] = 'srt'
49 DL.params['listsubtitles'] = False
50 def test_youtube_no_subtitles(self):
52 DL.params['writesubtitles'] = False
54 info_dict = IE.extract('QRS8MkLhQmM')
55 subtitles = info_dict[0]['subtitles']
56 self.assertEqual(subtitles, None)
57 def test_youtube_subtitles(self):
59 DL.params['writesubtitles'] = True
61 info_dict = IE.extract('QRS8MkLhQmM')
62 sub = info_dict[0]['subtitles'][0]
63 self.assertEqual(md5(sub[2]), '4cd9278a35ba2305f47354ee13472260')
64 def test_youtube_subtitles_it(self):
66 DL.params['writesubtitles'] = True
67 DL.params['subtitleslang'] = 'it'
69 info_dict = IE.extract('QRS8MkLhQmM')
70 sub = info_dict[0]['subtitles'][0]
71 self.assertEqual(md5(sub[2]), '164a51f16f260476a05b50fe4c2f161d')
72 def test_youtube_onlysubtitles(self):
74 DL.params['writesubtitles'] = True
75 DL.params['onlysubtitles'] = True
77 info_dict = IE.extract('QRS8MkLhQmM')
78 sub = info_dict[0]['subtitles'][0]
79 self.assertEqual(md5(sub[2]), '4cd9278a35ba2305f47354ee13472260')
80 def test_youtube_allsubtitles(self):
82 DL.params['allsubtitles'] = True
84 info_dict = IE.extract('QRS8MkLhQmM')
85 subtitles = info_dict[0]['subtitles']
86 self.assertEqual(len(subtitles), 13)
87 def test_youtube_subtitles_format(self):
89 DL.params['writesubtitles'] = True
90 DL.params['subtitlesformat'] = 'sbv'
92 info_dict = IE.extract('QRS8MkLhQmM')
93 sub = info_dict[0]['subtitles'][0]
94 self.assertEqual(md5(sub[2]), '13aeaa0c245a8bed9a451cb643e3ad8b')
95 def test_youtube_list_subtitles(self):
97 DL.params['listsubtitles'] = True
99 info_dict = IE.extract('QRS8MkLhQmM')
100 self.assertEqual(info_dict, None)
101 def test_youtube_automatic_captions(self):
103 DL.params['writesubtitles'] = True
104 DL.params['subtitleslang'] = 'it'
106 info_dict = IE.extract('8YoUxe5ncPo')
107 sub = info_dict[0]['subtitles'][0]
108 self.assertTrue(sub[2] is not None)
110 if __name__ == '__main__':