9 # Allow direct execution
11 sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
13 from youtube_dl.InfoExtractors import YoutubeIE
14 from youtube_dl.utils import *
16 PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
17 with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
18 parameters = json.load(pf)
20 # General configuration (from __init__, not very elegant...)
21 jar = compat_cookiejar.CookieJar()
22 cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
23 proxy_handler = compat_urllib_request.ProxyHandler()
24 opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
25 compat_urllib_request.install_opener(opener)
27 class FakeDownloader(object):
30 self.params = parameters
31 def to_screen(self, s):
35 def download(self, x):
38 md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest()
40 class TestYoutubeSubtitles(unittest.TestCase):
43 DL.params['allsubtitles'] = False
44 DL.params['writesubtitles'] = False
45 DL.params['subtitlesformat'] = 'srt'
46 DL.params['listsubtitles'] = False
47 def test_youtube_no_subtitles(self):
49 DL.params['writesubtitles'] = False
51 info_dict = IE.extract('QRS8MkLhQmM')
52 subtitles = info_dict[0]['subtitles']
53 self.assertEqual(subtitles, None)
54 def test_youtube_subtitles(self):
56 DL.params['writesubtitles'] = True
58 info_dict = IE.extract('QRS8MkLhQmM')
59 sub = info_dict[0]['subtitles'][0]
60 self.assertEqual(md5(sub[2]), '4cd9278a35ba2305f47354ee13472260')
61 def test_youtube_subtitles_it(self):
63 DL.params['writesubtitles'] = True
64 DL.params['subtitleslang'] = 'it'
66 info_dict = IE.extract('QRS8MkLhQmM')
67 sub = info_dict[0]['subtitles'][0]
68 self.assertEqual(md5(sub[2]), '164a51f16f260476a05b50fe4c2f161d')
69 def test_youtube_onlysubtitles(self):
71 DL.params['writesubtitles'] = True
72 DL.params['onlysubtitles'] = True
74 info_dict = IE.extract('QRS8MkLhQmM')
75 sub = info_dict[0]['subtitles'][0]
76 self.assertEqual(md5(sub[2]), '4cd9278a35ba2305f47354ee13472260')
77 def test_youtube_allsubtitles(self):
79 DL.params['allsubtitles'] = True
81 info_dict = IE.extract('QRS8MkLhQmM')
82 subtitles = info_dict[0]['subtitles']
83 self.assertEqual(len(subtitles), 13)
84 def test_youtube_subtitles_format(self):
86 DL.params['writesubtitles'] = True
87 DL.params['subtitlesformat'] = 'sbv'
89 info_dict = IE.extract('QRS8MkLhQmM')
90 sub = info_dict[0]['subtitles'][0]
91 self.assertEqual(md5(sub[2]), '13aeaa0c245a8bed9a451cb643e3ad8b')
92 def test_youtube_list_subtitles(self):
94 DL.params['listsubtitles'] = True
96 info_dict = IE.extract('QRS8MkLhQmM')
97 self.assertEqual(info_dict, None)
99 if __name__ == '__main__':