]>
Raphaƫl G. Git Repositories - youtubedl/blob - test/test_youtube_subtitles.py
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 *
15 from youtube_dl
import FileDownloader
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
)
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
)
28 class FakeDownloader(FileDownloader
):
31 self
.params
= parameters
32 def to_screen(self
, s
):
34 def trouble(self
, s
, tb
=None):
36 def download(self
, x
):
39 md5
= lambda s
: hashlib
.md5(s
.encode('utf-8')).hexdigest()
41 class TestYoutubeSubtitles(unittest
.TestCase
):
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
):
50 DL
.params
['writesubtitles'] = False
52 info_dict
= IE
.extract('QRS8MkLhQmM')
53 subtitles
= info_dict
[0]['subtitles']
54 self
.assertEqual(subtitles
, None)
55 def test_youtube_subtitles(self
):
57 DL
.params
['writesubtitles'] = True
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
):
64 DL
.params
['writesubtitles'] = True
65 DL
.params
['subtitleslang'] = 'it'
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
):
72 DL
.params
['writesubtitles'] = True
73 DL
.params
['onlysubtitles'] = True
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
):
80 DL
.params
['allsubtitles'] = True
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
):
87 DL
.params
['writesubtitles'] = True
88 DL
.params
['subtitlesformat'] = 'sbv'
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
):
95 DL
.params
['listsubtitles'] = True
97 info_dict
= IE
.extract('QRS8MkLhQmM')
98 self
.assertEqual(info_dict
, None)
100 if __name__
== '__main__':