]>
Commit | Line | Data |
---|---|---|
5e5ddcfb FV |
1 | #!/usr/bin/env python |
2 | ||
3 | import sys | |
4 | import unittest | |
5e5ddcfb FV |
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) | |
5e5ddcfb FV |
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): | |
ae608b80 IM |
41 | def setUp(self): |
42 | DL = FakeDownloader() | |
43 | DL.params['allsubtitles'] = False | |
44 | DL.params['writesubtitles'] = False | |
9e62bc44 | 45 | DL.params['subtitlesformat'] = 'srt' |
ae608b80 IM |
46 | def test_youtube_no_subtitles(self): |
47 | DL = FakeDownloader() | |
48 | DL.params['writesubtitles'] = False | |
49 | IE = YoutubeIE(DL) | |
50 | info_dict = IE.extract('QRS8MkLhQmM') | |
51 | subtitles = info_dict[0]['subtitles'] | |
52 | self.assertEqual(subtitles, None) | |
5e5ddcfb FV |
53 | def test_youtube_subtitles(self): |
54 | DL = FakeDownloader() | |
55 | DL.params['writesubtitles'] = True | |
56 | IE = YoutubeIE(DL) | |
57 | info_dict = IE.extract('QRS8MkLhQmM') | |
ae608b80 IM |
58 | sub = info_dict[0]['subtitles'][0] |
59 | self.assertEqual(md5(sub[2]), '4cd9278a35ba2305f47354ee13472260') | |
5e5ddcfb FV |
60 | def test_youtube_subtitles_it(self): |
61 | DL = FakeDownloader() | |
62 | DL.params['writesubtitles'] = True | |
63 | DL.params['subtitleslang'] = 'it' | |
64 | IE = YoutubeIE(DL) | |
65 | info_dict = IE.extract('QRS8MkLhQmM') | |
ae608b80 IM |
66 | sub = info_dict[0]['subtitles'][0] |
67 | self.assertEqual(md5(sub[2]), '164a51f16f260476a05b50fe4c2f161d') | |
cdb130b0 IM |
68 | def test_youtube_onlysubtitles(self): |
69 | DL = FakeDownloader() | |
ae608b80 | 70 | DL.params['writesubtitles'] = True |
cdb130b0 IM |
71 | DL.params['onlysubtitles'] = True |
72 | IE = YoutubeIE(DL) | |
73 | info_dict = IE.extract('QRS8MkLhQmM') | |
ae608b80 IM |
74 | sub = info_dict[0]['subtitles'][0] |
75 | self.assertEqual(md5(sub[2]), '4cd9278a35ba2305f47354ee13472260') | |
76 | def test_youtube_allsubtitles(self): | |
77 | DL = FakeDownloader() | |
78 | DL.params['allsubtitles'] = True | |
79 | IE = YoutubeIE(DL) | |
80 | info_dict = IE.extract('QRS8MkLhQmM') | |
81 | subtitles = info_dict[0]['subtitles'] | |
82 | self.assertEqual(len(subtitles), 12) | |
9e62bc44 IM |
83 | def test_youtube_subtitles_format(self): |
84 | DL = FakeDownloader() | |
85 | DL.params['writesubtitles'] = True | |
86 | DL.params['subtitlesformat'] = 'sbv' | |
87 | IE = YoutubeIE(DL) | |
88 | info_dict = IE.extract('QRS8MkLhQmM') | |
89 | sub = info_dict[0]['subtitles'][0] | |
90 | self.assertEqual(md5(sub[2]), '13aeaa0c245a8bed9a451cb643e3ad8b') | |
cdb130b0 | 91 | |
5e5ddcfb FV |
92 | if __name__ == '__main__': |
93 | unittest.main() |