]> git.bitcoin.ninja Git - youtube-dl/blob - test/test_utils.py
[pbs] Extract coveplayerid (Closes #3522)
[youtube-dl] / test / test_utils.py
1 #!/usr/bin/env python
2 # coding: utf-8
3
4 # Allow direct execution
5 import os
6 import sys
7 import unittest
8 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9
10
11 # Various small unit tests
12 import io
13 import json
14 import xml.etree.ElementTree
15
16 #from youtube_dl.utils import htmlentity_transform
17 from youtube_dl.utils import (
18     DateRange,
19     encodeFilename,
20     find_xpath_attr,
21     fix_xml_ampersands,
22     get_meta_content,
23     orderedSet,
24     PagedList,
25     parse_duration,
26     read_batch_urls,
27     sanitize_filename,
28     shell_quote,
29     smuggle_url,
30     str_to_int,
31     struct_unpack,
32     timeconvert,
33     unescapeHTML,
34     unified_strdate,
35     unsmuggle_url,
36     url_basename,
37     urlencode_postdata,
38     xpath_with_ns,
39     parse_iso8601,
40     strip_jsonp,
41     uppercase_escape,
42 )
43
44 if sys.version_info < (3, 0):
45     _compat_str = lambda b: b.decode('unicode-escape')
46 else:
47     _compat_str = lambda s: s
48
49
50 class TestUtil(unittest.TestCase):
51     def test_timeconvert(self):
52         self.assertTrue(timeconvert('') is None)
53         self.assertTrue(timeconvert('bougrg') is None)
54
55     def test_sanitize_filename(self):
56         self.assertEqual(sanitize_filename('abc'), 'abc')
57         self.assertEqual(sanitize_filename('abc_d-e'), 'abc_d-e')
58
59         self.assertEqual(sanitize_filename('123'), '123')
60
61         self.assertEqual('abc_de', sanitize_filename('abc/de'))
62         self.assertFalse('/' in sanitize_filename('abc/de///'))
63
64         self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de'))
65         self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|'))
66         self.assertEqual('yes no', sanitize_filename('yes? no'))
67         self.assertEqual('this - that', sanitize_filename('this: that'))
68
69         self.assertEqual(sanitize_filename('AT&T'), 'AT&T')
70         aumlaut = _compat_str('\xe4')
71         self.assertEqual(sanitize_filename(aumlaut), aumlaut)
72         tests = _compat_str('\u043a\u0438\u0440\u0438\u043b\u043b\u0438\u0446\u0430')
73         self.assertEqual(sanitize_filename(tests), tests)
74
75         forbidden = '"\0\\/'
76         for fc in forbidden:
77             for fbc in forbidden:
78                 self.assertTrue(fbc not in sanitize_filename(fc))
79
80     def test_sanitize_filename_restricted(self):
81         self.assertEqual(sanitize_filename('abc', restricted=True), 'abc')
82         self.assertEqual(sanitize_filename('abc_d-e', restricted=True), 'abc_d-e')
83
84         self.assertEqual(sanitize_filename('123', restricted=True), '123')
85
86         self.assertEqual('abc_de', sanitize_filename('abc/de', restricted=True))
87         self.assertFalse('/' in sanitize_filename('abc/de///', restricted=True))
88
89         self.assertEqual('abc_de', sanitize_filename('abc/<>\\*|de', restricted=True))
90         self.assertEqual('xxx', sanitize_filename('xxx/<>\\*|', restricted=True))
91         self.assertEqual('yes_no', sanitize_filename('yes? no', restricted=True))
92         self.assertEqual('this_-_that', sanitize_filename('this: that', restricted=True))
93
94         tests = _compat_str('a\xe4b\u4e2d\u56fd\u7684c')
95         self.assertEqual(sanitize_filename(tests, restricted=True), 'a_b_c')
96         self.assertTrue(sanitize_filename(_compat_str('\xf6'), restricted=True) != '')  # No empty filename
97
98         forbidden = '"\0\\/&!: \'\t\n()[]{}$;`^,#'
99         for fc in forbidden:
100             for fbc in forbidden:
101                 self.assertTrue(fbc not in sanitize_filename(fc, restricted=True))
102
103         # Handle a common case more neatly
104         self.assertEqual(sanitize_filename(_compat_str('\u5927\u58f0\u5e26 - Song'), restricted=True), 'Song')
105         self.assertEqual(sanitize_filename(_compat_str('\u603b\u7edf: Speech'), restricted=True), 'Speech')
106         # .. but make sure the file name is never empty
107         self.assertTrue(sanitize_filename('-', restricted=True) != '')
108         self.assertTrue(sanitize_filename(':', restricted=True) != '')
109
110     def test_sanitize_ids(self):
111         self.assertEqual(sanitize_filename('_n_cd26wFpw', is_id=True), '_n_cd26wFpw')
112         self.assertEqual(sanitize_filename('_BD_eEpuzXw', is_id=True), '_BD_eEpuzXw')
113         self.assertEqual(sanitize_filename('N0Y__7-UOdI', is_id=True), 'N0Y__7-UOdI')
114
115     def test_ordered_set(self):
116         self.assertEqual(orderedSet([1, 1, 2, 3, 4, 4, 5, 6, 7, 3, 5]), [1, 2, 3, 4, 5, 6, 7])
117         self.assertEqual(orderedSet([]), [])
118         self.assertEqual(orderedSet([1]), [1])
119         #keep the list ordered
120         self.assertEqual(orderedSet([135, 1, 1, 1]), [135, 1])
121
122     def test_unescape_html(self):
123         self.assertEqual(unescapeHTML(_compat_str('%20;')), _compat_str('%20;'))
124         
125     def test_daterange(self):
126         _20century = DateRange("19000101","20000101")
127         self.assertFalse("17890714" in _20century)
128         _ac = DateRange("00010101")
129         self.assertTrue("19690721" in _ac)
130         _firstmilenium = DateRange(end="10000101")
131         self.assertTrue("07110427" in _firstmilenium)
132
133     def test_unified_dates(self):
134         self.assertEqual(unified_strdate('December 21, 2010'), '20101221')
135         self.assertEqual(unified_strdate('8/7/2009'), '20090708')
136         self.assertEqual(unified_strdate('Dec 14, 2012'), '20121214')
137         self.assertEqual(unified_strdate('2012/10/11 01:56:38 +0000'), '20121011')
138         self.assertEqual(unified_strdate('1968-12-10'), '19681210')
139
140     def test_find_xpath_attr(self):
141         testxml = u'''<root>
142             <node/>
143             <node x="a"/>
144             <node x="a" y="c" />
145             <node x="b" y="d" />
146         </root>'''
147         doc = xml.etree.ElementTree.fromstring(testxml)
148
149         self.assertEqual(find_xpath_attr(doc, './/fourohfour', 'n', 'v'), None)
150         self.assertEqual(find_xpath_attr(doc, './/node', 'x', 'a'), doc[1])
151         self.assertEqual(find_xpath_attr(doc, './/node', 'y', 'c'), doc[2])
152
153     def test_meta_parser(self):
154         testhtml = u'''
155         <head>
156             <meta name="description" content="foo &amp; bar">
157             <meta content='Plato' name='author'/>
158         </head>
159         '''
160         get_meta = lambda name: get_meta_content(name, testhtml)
161         self.assertEqual(get_meta('description'), u'foo & bar')
162         self.assertEqual(get_meta('author'), 'Plato')
163
164     def test_xpath_with_ns(self):
165         testxml = u'''<root xmlns:media="http://example.com/">
166             <media:song>
167                 <media:author>The Author</media:author>
168                 <url>http://server.com/download.mp3</url>
169             </media:song>
170         </root>'''
171         doc = xml.etree.ElementTree.fromstring(testxml)
172         find = lambda p: doc.find(xpath_with_ns(p, {'media': 'http://example.com/'}))
173         self.assertTrue(find('media:song') is not None)
174         self.assertEqual(find('media:song/media:author').text, u'The Author')
175         self.assertEqual(find('media:song/url').text, u'http://server.com/download.mp3')
176
177     def test_smuggle_url(self):
178         data = {u"ö": u"ö", u"abc": [3]}
179         url = 'https://foo.bar/baz?x=y#a'
180         smug_url = smuggle_url(url, data)
181         unsmug_url, unsmug_data = unsmuggle_url(smug_url)
182         self.assertEqual(url, unsmug_url)
183         self.assertEqual(data, unsmug_data)
184
185         res_url, res_data = unsmuggle_url(url)
186         self.assertEqual(res_url, url)
187         self.assertEqual(res_data, None)
188
189     def test_shell_quote(self):
190         args = ['ffmpeg', '-i', encodeFilename(u'ñ€ß\'.mp4')]
191         self.assertEqual(shell_quote(args), u"""ffmpeg -i 'ñ€ß'"'"'.mp4'""")
192
193     def test_str_to_int(self):
194         self.assertEqual(str_to_int('123,456'), 123456)
195         self.assertEqual(str_to_int('123.456'), 123456)
196
197     def test_url_basename(self):
198         self.assertEqual(url_basename(u'http://foo.de/'), u'')
199         self.assertEqual(url_basename(u'http://foo.de/bar/baz'), u'baz')
200         self.assertEqual(url_basename(u'http://foo.de/bar/baz?x=y'), u'baz')
201         self.assertEqual(url_basename(u'http://foo.de/bar/baz#x=y'), u'baz')
202         self.assertEqual(url_basename(u'http://foo.de/bar/baz/'), u'baz')
203         self.assertEqual(
204             url_basename(u'http://media.w3.org/2010/05/sintel/trailer.mp4'),
205             u'trailer.mp4')
206
207     def test_parse_duration(self):
208         self.assertEqual(parse_duration(None), None)
209         self.assertEqual(parse_duration('1'), 1)
210         self.assertEqual(parse_duration('1337:12'), 80232)
211         self.assertEqual(parse_duration('9:12:43'), 33163)
212         self.assertEqual(parse_duration('12:00'), 720)
213         self.assertEqual(parse_duration('00:01:01'), 61)
214         self.assertEqual(parse_duration('x:y'), None)
215         self.assertEqual(parse_duration('3h11m53s'), 11513)
216         self.assertEqual(parse_duration('62m45s'), 3765)
217         self.assertEqual(parse_duration('6m59s'), 419)
218         self.assertEqual(parse_duration('49s'), 49)
219         self.assertEqual(parse_duration('0h0m0s'), 0)
220         self.assertEqual(parse_duration('0m0s'), 0)
221         self.assertEqual(parse_duration('0s'), 0)
222
223     def test_fix_xml_ampersands(self):
224         self.assertEqual(
225             fix_xml_ampersands('"&x=y&z=a'), '"&amp;x=y&amp;z=a')
226         self.assertEqual(
227             fix_xml_ampersands('"&amp;x=y&wrong;&z=a'),
228             '"&amp;x=y&amp;wrong;&amp;z=a')
229         self.assertEqual(
230             fix_xml_ampersands('&amp;&apos;&gt;&lt;&quot;'),
231             '&amp;&apos;&gt;&lt;&quot;')
232         self.assertEqual(
233             fix_xml_ampersands('&#1234;&#x1abC;'), '&#1234;&#x1abC;')
234         self.assertEqual(fix_xml_ampersands('&#&#'), '&amp;#&amp;#')
235
236     def test_paged_list(self):
237         def testPL(size, pagesize, sliceargs, expected):
238             def get_page(pagenum):
239                 firstid = pagenum * pagesize
240                 upto = min(size, pagenum * pagesize + pagesize)
241                 for i in range(firstid, upto):
242                     yield i
243
244             pl = PagedList(get_page, pagesize)
245             got = pl.getslice(*sliceargs)
246             self.assertEqual(got, expected)
247
248         testPL(5, 2, (), [0, 1, 2, 3, 4])
249         testPL(5, 2, (1,), [1, 2, 3, 4])
250         testPL(5, 2, (2,), [2, 3, 4])
251         testPL(5, 2, (4,), [4])
252         testPL(5, 2, (0, 3), [0, 1, 2])
253         testPL(5, 2, (1, 4), [1, 2, 3])
254         testPL(5, 2, (2, 99), [2, 3, 4])
255         testPL(5, 2, (20, 99), [])
256
257     def test_struct_unpack(self):
258         self.assertEqual(struct_unpack(u'!B', b'\x00'), (0,))
259
260     def test_read_batch_urls(self):
261         f = io.StringIO(u'''\xef\xbb\xbf foo
262             bar\r
263             baz
264             # More after this line\r
265             ; or after this
266             bam''')
267         self.assertEqual(read_batch_urls(f), [u'foo', u'bar', u'baz', u'bam'])
268
269     def test_urlencode_postdata(self):
270         data = urlencode_postdata({'username': 'foo@bar.com', 'password': '1234'})
271         self.assertTrue(isinstance(data, bytes))
272
273     def test_parse_iso8601(self):
274         self.assertEqual(parse_iso8601('2014-03-23T23:04:26+0100'), 1395612266)
275         self.assertEqual(parse_iso8601('2014-03-23T22:04:26+0000'), 1395612266)
276         self.assertEqual(parse_iso8601('2014-03-23T22:04:26Z'), 1395612266)
277
278     def test_strip_jsonp(self):
279         stripped = strip_jsonp('cb ([ {"id":"532cb",\n\n\n"x":\n3}\n]\n);')
280         d = json.loads(stripped)
281         self.assertEqual(d, [{"id": "532cb", "x": 3}])
282
283     def test_uppercase_escape(self):
284         self.assertEqual(uppercase_escape(u'aä'), u'aä')
285         self.assertEqual(uppercase_escape(u'\\U0001d550'), u'𝕐')
286
287 if __name__ == '__main__':
288     unittest.main()