Merge branch 'master' of https://github.com/rg3/youtube-dl
[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 test_youtube_subtitles(self):
42         DL = FakeDownloader()
43         DL.params['writesubtitles'] = True
44         IE = YoutubeIE(DL)
45         info_dict = IE.extract('QRS8MkLhQmM')
46         self.assertEqual(md5(info_dict[0]['subtitles']), 'c3228550d59116f3c29fba370b55d033')
47
48     def test_youtube_subtitles_it(self):
49         DL = FakeDownloader()
50         DL.params['writesubtitles'] = True
51         DL.params['subtitleslang'] = 'it'
52         IE = YoutubeIE(DL)
53         info_dict = IE.extract('QRS8MkLhQmM')
54         self.assertEqual(md5(info_dict[0]['subtitles']), '132a88a0daf8e1520f393eb58f1f646a')
55
56 if __name__ == '__main__':
57     unittest.main()