]> git.bitcoin.ninja Git - youtube-dl/blob - test/test_youtube_subtitles.py
b4909091b9216b2b426ca599882b21e2abc6820e
[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.InfoExtractors import YoutubeIE
14 from youtube_dl.utils import *
15
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)
19
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)
26
27 class FakeDownloader(object):
28     def __init__(self):
29         self.result = []
30         self.params = parameters
31     def to_screen(self, s):
32         print(s)
33     def trouble(self, s):
34         raise Exception(s)
35     def download(self, x):
36         self.result.append(x)
37
38 md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest()
39
40 class TestYoutubeSubtitles(unittest.TestCase):
41     def setUp(self):
42         DL = FakeDownloader()
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):
48         DL = FakeDownloader()
49         DL.params['writesubtitles'] = False
50         IE = YoutubeIE(DL)
51         info_dict = IE.extract('QRS8MkLhQmM')
52         subtitles = info_dict[0]['subtitles']
53         self.assertEqual(subtitles, None)
54     def test_youtube_subtitles(self):
55         DL = FakeDownloader()
56         DL.params['writesubtitles'] = True
57         IE = YoutubeIE(DL)
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):
62         DL = FakeDownloader()
63         DL.params['writesubtitles'] = True
64         DL.params['subtitleslang'] = 'it'
65         IE = YoutubeIE(DL)
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):
70         DL = FakeDownloader()
71         DL.params['writesubtitles'] = True
72         DL.params['onlysubtitles'] = True
73         IE = YoutubeIE(DL)
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):
78         DL = FakeDownloader()
79         DL.params['allsubtitles'] = True
80         IE = YoutubeIE(DL)
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):
85         DL = FakeDownloader()
86         DL.params['writesubtitles'] = True
87         DL.params['subtitlesformat'] = 'sbv'
88         IE = YoutubeIE(DL)
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):
93         DL = FakeDownloader()
94         DL.params['listsubtitles'] = True
95         IE = YoutubeIE(DL)
96         info_dict = IE.extract('QRS8MkLhQmM')
97         self.assertEqual(info_dict, None)
98
99 if __name__ == '__main__':
100     unittest.main()