]> Raphaƫl G. Git Repositories - youtubedl/blob - test/test_youtube_subtitles.py
Imported Upstream version 2013.05.14
[youtubedl] / 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 from youtube_dl import FileDownloader
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 FakeDownloader(FileDownloader):
29 def __init__(self):
30 self.result = []
31 self.params = parameters
32 def to_screen(self, s):
33 print(s)
34 def trouble(self, s, tb=None):
35 raise Exception(s)
36 def download(self, x):
37 self.result.append(x)
38
39 md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest()
40
41 class TestYoutubeSubtitles(unittest.TestCase):
42 def setUp(self):
43 DL = FakeDownloader()
44 DL.params['allsubtitles'] = False
45 DL.params['writesubtitles'] = False
46 DL.params['subtitlesformat'] = 'srt'
47 DL.params['listsubtitles'] = False
48 def test_youtube_no_subtitles(self):
49 DL = FakeDownloader()
50 DL.params['writesubtitles'] = False
51 IE = YoutubeIE(DL)
52 info_dict = IE.extract('QRS8MkLhQmM')
53 subtitles = info_dict[0]['subtitles']
54 self.assertEqual(subtitles, None)
55 def test_youtube_subtitles(self):
56 DL = FakeDownloader()
57 DL.params['writesubtitles'] = True
58 IE = YoutubeIE(DL)
59 info_dict = IE.extract('QRS8MkLhQmM')
60 sub = info_dict[0]['subtitles'][0]
61 self.assertEqual(md5(sub[2]), '4cd9278a35ba2305f47354ee13472260')
62 def test_youtube_subtitles_it(self):
63 DL = FakeDownloader()
64 DL.params['writesubtitles'] = True
65 DL.params['subtitleslang'] = 'it'
66 IE = YoutubeIE(DL)
67 info_dict = IE.extract('QRS8MkLhQmM')
68 sub = info_dict[0]['subtitles'][0]
69 self.assertEqual(md5(sub[2]), '164a51f16f260476a05b50fe4c2f161d')
70 def test_youtube_onlysubtitles(self):
71 DL = FakeDownloader()
72 DL.params['writesubtitles'] = True
73 DL.params['onlysubtitles'] = True
74 IE = YoutubeIE(DL)
75 info_dict = IE.extract('QRS8MkLhQmM')
76 sub = info_dict[0]['subtitles'][0]
77 self.assertEqual(md5(sub[2]), '4cd9278a35ba2305f47354ee13472260')
78 def test_youtube_allsubtitles(self):
79 DL = FakeDownloader()
80 DL.params['allsubtitles'] = True
81 IE = YoutubeIE(DL)
82 info_dict = IE.extract('QRS8MkLhQmM')
83 subtitles = info_dict[0]['subtitles']
84 self.assertEqual(len(subtitles), 13)
85 def test_youtube_subtitles_format(self):
86 DL = FakeDownloader()
87 DL.params['writesubtitles'] = True
88 DL.params['subtitlesformat'] = 'sbv'
89 IE = YoutubeIE(DL)
90 info_dict = IE.extract('QRS8MkLhQmM')
91 sub = info_dict[0]['subtitles'][0]
92 self.assertEqual(md5(sub[2]), '13aeaa0c245a8bed9a451cb643e3ad8b')
93 def test_youtube_list_subtitles(self):
94 DL = FakeDownloader()
95 DL.params['listsubtitles'] = True
96 IE = YoutubeIE(DL)
97 info_dict = IE.extract('QRS8MkLhQmM')
98 self.assertEqual(info_dict, None)
99
100 if __name__ == '__main__':
101 unittest.main()