Add Jukebox IE
[youtube-dl] / 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 youtube_dl import YoutubeDL
16
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)
20
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)
27
28 class FakeYDL(YoutubeDL):
29     def __init__(self):
30         self.result = []
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):
35         print(s)
36     def trouble(self, s, tb=None):
37         raise Exception(s)
38     def download(self, x):
39         self.result.append(x)
40
41 md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest()
42
43 class TestYoutubeSubtitles(unittest.TestCase):
44     def setUp(self):
45         DL = FakeYDL()
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):
51         DL = FakeYDL()
52         DL.params['writesubtitles'] = False
53         IE = YoutubeIE(DL)
54         info_dict = IE.extract('QRS8MkLhQmM')
55         subtitles = info_dict[0]['subtitles']
56         self.assertEqual(subtitles, None)
57     def test_youtube_subtitles(self):
58         DL = FakeYDL()
59         DL.params['writesubtitles'] = True
60         IE = YoutubeIE(DL)
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):
65         DL = FakeYDL()
66         DL.params['writesubtitles'] = True
67         DL.params['subtitleslang'] = 'it'
68         IE = YoutubeIE(DL)
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):
73         DL = FakeYDL()
74         DL.params['writesubtitles'] = True
75         DL.params['onlysubtitles'] = True
76         IE = YoutubeIE(DL)
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):
81         DL = FakeYDL()
82         DL.params['allsubtitles'] = True
83         IE = YoutubeIE(DL)
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):
88         DL = FakeYDL()
89         DL.params['writesubtitles'] = True
90         DL.params['subtitlesformat'] = 'sbv'
91         IE = YoutubeIE(DL)
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):
96         DL = FakeYDL()
97         DL.params['listsubtitles'] = True
98         IE = YoutubeIE(DL)
99         info_dict = IE.extract('QRS8MkLhQmM')
100         self.assertEqual(info_dict, None)
101     def test_youtube_automatic_captions(self):
102         DL = FakeYDL()
103         DL.params['writesubtitles'] = True
104         DL.params['subtitleslang'] = 'it'
105         IE = YoutubeIE(DL)
106         info_dict = IE.extract('8YoUxe5ncPo')
107         sub = info_dict[0]['subtitles'][0]
108         self.assertTrue(sub[2] is not None)
109
110 if __name__ == '__main__':
111     unittest.main()