]> git.bitcoin.ninja Git - youtube-dl/blob - test/test_youtube_subtitles.py
Simplify tests
[youtube-dl] / test / test_youtube_subtitles.py
1 #!/usr/bin/env python
2
3 # Allow direct execution
4 import os
5 import sys
6 import unittest
7 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
8
9 from test.helper import FakeYDL, global_setup, md5
10 global_setup()
11
12
13 from youtube_dl.extractor import YoutubeIE
14
15
16 class TestYoutubeSubtitles(unittest.TestCase):
17     def setUp(self):
18         self.DL = FakeYDL()
19         self.url = 'QRS8MkLhQmM'
20
21     def getInfoDict(self):
22         IE = YoutubeIE(self.DL)
23         info_dict = IE.extract(self.url)
24         return info_dict
25
26     def getSubtitles(self):
27         info_dict = self.getInfoDict()
28         return info_dict[0]['subtitles']
29
30     def test_youtube_no_writesubtitles(self):
31         self.DL.params['writesubtitles'] = False
32         subtitles = self.getSubtitles()
33         self.assertEqual(subtitles, None)
34
35     def test_youtube_subtitles(self):
36         self.DL.params['writesubtitles'] = True
37         subtitles = self.getSubtitles()
38         self.assertEqual(md5(subtitles['en']), '4cd9278a35ba2305f47354ee13472260')
39
40     def test_youtube_subtitles_lang(self):
41         self.DL.params['writesubtitles'] = True
42         self.DL.params['subtitleslangs'] = ['it']
43         subtitles = self.getSubtitles()
44         self.assertEqual(md5(subtitles['it']), '164a51f16f260476a05b50fe4c2f161d')
45
46     def test_youtube_allsubtitles(self):
47         self.DL.params['writesubtitles'] = True
48         self.DL.params['allsubtitles'] = True
49         subtitles = self.getSubtitles()
50         self.assertEqual(len(subtitles.keys()), 13)
51
52     def test_youtube_subtitles_sbv_format(self):
53         self.DL.params['writesubtitles'] = True
54         self.DL.params['subtitlesformat'] = 'sbv'
55         subtitles = self.getSubtitles()
56         self.assertEqual(md5(subtitles['en']), '13aeaa0c245a8bed9a451cb643e3ad8b')
57
58     def test_youtube_subtitles_vtt_format(self):
59         self.DL.params['writesubtitles'] = True
60         self.DL.params['subtitlesformat'] = 'vtt'
61         subtitles = self.getSubtitles()
62         self.assertEqual(md5(subtitles['en']), '356cdc577fde0c6783b9b822e7206ff7')
63
64     def test_youtube_list_subtitles(self):
65         self.DL.expect_warning(u'Video doesn\'t have automatic captions')
66         self.DL.params['listsubtitles'] = True
67         info_dict = self.getInfoDict()
68         self.assertEqual(info_dict, None)
69
70     def test_youtube_automatic_captions(self):
71         self.url = '8YoUxe5ncPo'
72         self.DL.params['writeautomaticsub'] = True
73         self.DL.params['subtitleslangs'] = ['it']
74         subtitles = self.getSubtitles()
75         self.assertTrue(subtitles['it'] is not None)
76
77     def test_youtube_nosubtitles(self):
78         self.DL.expect_warning(u'video doesn\'t have subtitles')
79         self.url = 'sAjKT8FhjI8'
80         self.DL.params['writesubtitles'] = True
81         self.DL.params['allsubtitles'] = True
82         subtitles = self.getSubtitles()
83         self.assertEqual(len(subtitles), 0)
84
85     def test_youtube_multiple_langs(self):
86         self.url = 'QRS8MkLhQmM'
87         self.DL.params['writesubtitles'] = True
88         langs = ['it', 'fr', 'de']
89         self.DL.params['subtitleslangs'] = langs
90         subtitles = self.getSubtitles()
91         for lang in langs:
92             self.assertTrue(subtitles.get(lang) is not None, u'Subtitles for \'%s\' not extracted' % lang)
93
94 if __name__ == '__main__':
95     unittest.main()