Merge remote-tracking branch 'origin/master'
[youtube-dl] / test / test_YoutubeDL.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
10 from youtube_dl import YoutubeDL
11
12
13 class YDL(FakeYDL):
14     def __init__(self, *args, **kwargs):
15         super(YDL, self).__init__(*args, **kwargs)
16         self.downloaded_info_dicts = []
17         self.msgs = []
18
19     def process_info(self, info_dict):
20         self.downloaded_info_dicts.append(info_dict)
21
22     def to_screen(self, msg):
23         self.msgs.append(msg)
24
25
26 class TestFormatSelection(unittest.TestCase):
27     def test_prefer_free_formats(self):
28         # Same resolution => download webm
29         ydl = YDL()
30         ydl.params['prefer_free_formats'] = True
31         formats = [
32             {u'ext': u'webm', u'height': 460},
33             {u'ext': u'mp4',  u'height': 460},
34         ]
35         info_dict = {u'formats': formats, u'extractor': u'test'}
36         ydl.process_ie_result(info_dict)
37         downloaded = ydl.downloaded_info_dicts[0]
38         self.assertEqual(downloaded[u'ext'], u'webm')
39
40         # Different resolution => download best quality (mp4)
41         ydl = YDL()
42         ydl.params['prefer_free_formats'] = True
43         formats = [
44             {u'ext': u'webm', u'height': 720},
45             {u'ext': u'mp4', u'height': 1080},
46         ]
47         info_dict[u'formats'] = formats
48         ydl.process_ie_result(info_dict)
49         downloaded = ydl.downloaded_info_dicts[0]
50         self.assertEqual(downloaded[u'ext'], u'mp4')
51
52         # No prefer_free_formats => keep original formats order
53         ydl = YDL()
54         ydl.params['prefer_free_formats'] = False
55         formats = [
56             {u'ext': u'webm', u'height': 720},
57             {u'ext': u'flv', u'height': 720},
58         ]
59         info_dict[u'formats'] = formats
60         ydl.process_ie_result(info_dict)
61         downloaded = ydl.downloaded_info_dicts[0]
62         self.assertEqual(downloaded[u'ext'], u'flv')
63
64     def test_format_limit(self):
65         formats = [
66             {u'format_id': u'meh', u'url': u'http://example.com/meh'},
67             {u'format_id': u'good', u'url': u'http://example.com/good'},
68             {u'format_id': u'great', u'url': u'http://example.com/great'},
69             {u'format_id': u'excellent', u'url': u'http://example.com/exc'},
70         ]
71         info_dict = {
72             u'formats': formats, u'extractor': u'test', 'id': 'testvid'}
73
74         ydl = YDL()
75         ydl.process_ie_result(info_dict)
76         downloaded = ydl.downloaded_info_dicts[0]
77         self.assertEqual(downloaded[u'format_id'], u'excellent')
78
79         ydl = YDL({'format_limit': 'good'})
80         assert ydl.params['format_limit'] == 'good'
81         ydl.process_ie_result(info_dict.copy())
82         downloaded = ydl.downloaded_info_dicts[0]
83         self.assertEqual(downloaded[u'format_id'], u'good')
84
85         ydl = YDL({'format_limit': 'great', 'format': 'all'})
86         ydl.process_ie_result(info_dict.copy())
87         self.assertEqual(ydl.downloaded_info_dicts[0][u'format_id'], u'meh')
88         self.assertEqual(ydl.downloaded_info_dicts[1][u'format_id'], u'good')
89         self.assertEqual(ydl.downloaded_info_dicts[2][u'format_id'], u'great')
90         self.assertTrue('3' in ydl.msgs[0])
91
92         ydl = YDL()
93         ydl.params['format_limit'] = 'excellent'
94         ydl.process_ie_result(info_dict.copy())
95         downloaded = ydl.downloaded_info_dicts[0]
96         self.assertEqual(downloaded[u'format_id'], u'excellent')
97
98     def test_format_selection(self):
99         formats = [
100             {u'format_id': u'35', u'ext': u'mp4'},
101             {u'format_id': u'45', u'ext': u'webm'},
102             {u'format_id': u'47', u'ext': u'webm'},
103             {u'format_id': u'2', u'ext': u'flv'},
104         ]
105         info_dict = {u'formats': formats, u'extractor': u'test'}
106
107         ydl = YDL({'format': u'20/47'})
108         ydl.process_ie_result(info_dict.copy())
109         downloaded = ydl.downloaded_info_dicts[0]
110         self.assertEqual(downloaded['format_id'], u'47')
111
112         ydl = YDL({'format': u'20/71/worst'})
113         ydl.process_ie_result(info_dict.copy())
114         downloaded = ydl.downloaded_info_dicts[0]
115         self.assertEqual(downloaded['format_id'], u'35')
116
117         ydl = YDL()
118         ydl.process_ie_result(info_dict.copy())
119         downloaded = ydl.downloaded_info_dicts[0]
120         self.assertEqual(downloaded['format_id'], u'2')
121
122         ydl = YDL({'format': u'webm/mp4'})
123         ydl.process_ie_result(info_dict.copy())
124         downloaded = ydl.downloaded_info_dicts[0]
125         self.assertEqual(downloaded['format_id'], u'47')
126
127         ydl = YDL({'format': u'3gp/40/mp4'})
128         ydl.process_ie_result(info_dict.copy())
129         downloaded = ydl.downloaded_info_dicts[0]
130         self.assertEqual(downloaded['format_id'], u'35')
131
132     def test_add_extra_info(self):
133         test_dict = {
134             'extractor': 'Foo',
135         }
136         extra_info = {
137             'extractor': 'Bar',
138             'playlist': 'funny videos',
139         }
140         YDL.add_extra_info(test_dict, extra_info)
141         self.assertEqual(test_dict['extractor'], 'Foo')
142         self.assertEqual(test_dict['playlist'], 'funny videos')
143
144     def test_prepare_filename(self):
145         info = {
146             u'id': u'1234',
147             u'ext': u'mp4',
148             u'width': None,
149         }
150         def fname(templ):
151             ydl = YoutubeDL({'outtmpl': templ})
152             return ydl.prepare_filename(info)
153         self.assertEqual(fname(u'%(id)s.%(ext)s'), u'1234.mp4')
154         self.assertEqual(fname(u'%(id)s-%(width)s.%(ext)s'), u'1234-NA.mp4')
155         # Replace missing fields with 'NA'
156         self.assertEqual(fname(u'%(uploader_date)s-%(id)s.%(ext)s'), u'NA-1234.mp4')
157
158
159 if __name__ == '__main__':
160     unittest.main()