[YoutubeDL] Add negation support for string comparisons in format selection expressio...
[youtube-dl] / test / test_InfoExtractor.py
1 #!/usr/bin/env python
2
3 from __future__ import unicode_literals
4
5 # Allow direct execution
6 import io
7 import os
8 import sys
9 import unittest
10 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
11
12 from test.helper import FakeYDL, expect_dict, expect_value, http_server_port
13 from youtube_dl.compat import compat_etree_fromstring, compat_http_server
14 from youtube_dl.extractor.common import InfoExtractor
15 from youtube_dl.extractor import YoutubeIE, get_info_extractor
16 from youtube_dl.utils import encode_data_uri, strip_jsonp, ExtractorError, RegexNotFoundError
17 import threading
18
19
20 TEAPOT_RESPONSE_STATUS = 418
21 TEAPOT_RESPONSE_BODY = "<h1>418 I'm a teapot</h1>"
22
23
24 class InfoExtractorTestRequestHandler(compat_http_server.BaseHTTPRequestHandler):
25     def log_message(self, format, *args):
26         pass
27
28     def do_GET(self):
29         if self.path == '/teapot':
30             self.send_response(TEAPOT_RESPONSE_STATUS)
31             self.send_header('Content-Type', 'text/html; charset=utf-8')
32             self.end_headers()
33             self.wfile.write(TEAPOT_RESPONSE_BODY.encode())
34         else:
35             assert False
36
37
38 class TestIE(InfoExtractor):
39     pass
40
41
42 class TestInfoExtractor(unittest.TestCase):
43     def setUp(self):
44         self.ie = TestIE(FakeYDL())
45
46     def test_ie_key(self):
47         self.assertEqual(get_info_extractor(YoutubeIE.ie_key()), YoutubeIE)
48
49     def test_html_search_regex(self):
50         html = '<p id="foo">Watch this <a href="http://www.youtube.com/watch?v=BaW_jenozKc">video</a></p>'
51         search = lambda re, *args: self.ie._html_search_regex(re, html, *args)
52         self.assertEqual(search(r'<p id="foo">(.+?)</p>', 'foo'), 'Watch this video')
53
54     def test_opengraph(self):
55         ie = self.ie
56         html = '''
57             <meta name="og:title" content='Foo'/>
58             <meta content="Some video's description " name="og:description"/>
59             <meta property='og:image' content='http://domain.com/pic.jpg?key1=val1&amp;key2=val2'/>
60             <meta content='application/x-shockwave-flash' property='og:video:type'>
61             <meta content='Foo' property=og:foobar>
62             <meta name="og:test1" content='foo > < bar'/>
63             <meta name="og:test2" content="foo >//< bar"/>
64             '''
65         self.assertEqual(ie._og_search_title(html), 'Foo')
66         self.assertEqual(ie._og_search_description(html), 'Some video\'s description ')
67         self.assertEqual(ie._og_search_thumbnail(html), 'http://domain.com/pic.jpg?key1=val1&key2=val2')
68         self.assertEqual(ie._og_search_video_url(html, default=None), None)
69         self.assertEqual(ie._og_search_property('foobar', html), 'Foo')
70         self.assertEqual(ie._og_search_property('test1', html), 'foo > < bar')
71         self.assertEqual(ie._og_search_property('test2', html), 'foo >//< bar')
72         self.assertEqual(ie._og_search_property(('test0', 'test1'), html), 'foo > < bar')
73         self.assertRaises(RegexNotFoundError, ie._og_search_property, 'test0', html, None, fatal=True)
74         self.assertRaises(RegexNotFoundError, ie._og_search_property, ('test0', 'test00'), html, None, fatal=True)
75
76     def test_html_search_meta(self):
77         ie = self.ie
78         html = '''
79             <meta name="a" content="1" />
80             <meta name='b' content='2'>
81             <meta name="c" content='3'>
82             <meta name=d content='4'>
83             <meta property="e" content='5' >
84             <meta content="6" name="f">
85         '''
86
87         self.assertEqual(ie._html_search_meta('a', html), '1')
88         self.assertEqual(ie._html_search_meta('b', html), '2')
89         self.assertEqual(ie._html_search_meta('c', html), '3')
90         self.assertEqual(ie._html_search_meta('d', html), '4')
91         self.assertEqual(ie._html_search_meta('e', html), '5')
92         self.assertEqual(ie._html_search_meta('f', html), '6')
93         self.assertEqual(ie._html_search_meta(('a', 'b', 'c'), html), '1')
94         self.assertEqual(ie._html_search_meta(('c', 'b', 'a'), html), '3')
95         self.assertEqual(ie._html_search_meta(('z', 'x', 'c'), html), '3')
96         self.assertRaises(RegexNotFoundError, ie._html_search_meta, 'z', html, None, fatal=True)
97         self.assertRaises(RegexNotFoundError, ie._html_search_meta, ('z', 'x'), html, None, fatal=True)
98
99     def test_download_json(self):
100         uri = encode_data_uri(b'{"foo": "blah"}', 'application/json')
101         self.assertEqual(self.ie._download_json(uri, None), {'foo': 'blah'})
102         uri = encode_data_uri(b'callback({"foo": "blah"})', 'application/javascript')
103         self.assertEqual(self.ie._download_json(uri, None, transform_source=strip_jsonp), {'foo': 'blah'})
104         uri = encode_data_uri(b'{"foo": invalid}', 'application/json')
105         self.assertRaises(ExtractorError, self.ie._download_json, uri, None)
106         self.assertEqual(self.ie._download_json(uri, None, fatal=False), None)
107
108     def test_extract_jwplayer_data_realworld(self):
109         # from http://www.suffolk.edu/sjc/
110         expect_dict(
111             self,
112             self.ie._extract_jwplayer_data(r'''
113                 <script type='text/javascript'>
114                     jwplayer('my-video').setup({
115                         file: 'rtmp://192.138.214.154/live/sjclive',
116                         fallback: 'true',
117                         width: '95%',
118                       aspectratio: '16:9',
119                       primary: 'flash',
120                       mediaid:'XEgvuql4'
121                     });
122                 </script>
123                 ''', None, require_title=False),
124             {
125                 'id': 'XEgvuql4',
126                 'formats': [{
127                     'url': 'rtmp://192.138.214.154/live/sjclive',
128                     'ext': 'flv'
129                 }]
130             })
131
132         # from https://www.pornoxo.com/videos/7564/striptease-from-sexy-secretary/
133         expect_dict(
134             self,
135             self.ie._extract_jwplayer_data(r'''
136 <script type="text/javascript">
137     jwplayer("mediaplayer").setup({
138         'videoid': "7564",
139         'width': "100%",
140         'aspectratio': "16:9",
141         'stretching': "exactfit",
142         'autostart': 'false',
143         'flashplayer': "https://t04.vipstreamservice.com/jwplayer/v5.10/player.swf",
144         'file': "https://cdn.pornoxo.com/key=MF+oEbaxqTKb50P-w9G3nA,end=1489689259,ip=104.199.146.27/ip=104.199.146.27/speed=6573765/buffer=3.0/2009-12/4b2157147afe5efa93ce1978e0265289c193874e02597.flv",
145         'image': "https://t03.vipstreamservice.com/thumbs/pxo-full/2009-12/14/a4b2157147afe5efa93ce1978e0265289c193874e02597.flv-full-13.jpg",
146         'filefallback': "https://cdn.pornoxo.com/key=9ZPsTR5EvPLQrBaak2MUGA,end=1489689259,ip=104.199.146.27/ip=104.199.146.27/speed=6573765/buffer=3.0/2009-12/m_4b2157147afe5efa93ce1978e0265289c193874e02597.mp4",
147         'logo.hide': true,
148         'skin': "https://t04.vipstreamservice.com/jwplayer/skin/modieus-blk.zip",
149         'plugins': "https://t04.vipstreamservice.com/jwplayer/dock/dockableskinnableplugin.swf",
150         'dockableskinnableplugin.piclink': "/index.php?key=ajax-videothumbsn&vid=7564&data=2009-12--14--4b2157147afe5efa93ce1978e0265289c193874e02597.flv--17370",
151         'controlbar': 'bottom',
152         'modes': [
153             {type: 'flash', src: 'https://t04.vipstreamservice.com/jwplayer/v5.10/player.swf'}
154         ],
155         'provider': 'http'
156     });
157     //noinspection JSAnnotator
158     invideo.setup({
159         adsUrl: "/banner-iframe/?zoneId=32",
160         adsUrl2: "",
161         autostart: false
162     });
163 </script>
164             ''', 'dummy', require_title=False),
165             {
166                 'thumbnail': 'https://t03.vipstreamservice.com/thumbs/pxo-full/2009-12/14/a4b2157147afe5efa93ce1978e0265289c193874e02597.flv-full-13.jpg',
167                 'formats': [{
168                     'url': 'https://cdn.pornoxo.com/key=MF+oEbaxqTKb50P-w9G3nA,end=1489689259,ip=104.199.146.27/ip=104.199.146.27/speed=6573765/buffer=3.0/2009-12/4b2157147afe5efa93ce1978e0265289c193874e02597.flv',
169                     'ext': 'flv'
170                 }]
171             })
172
173         # from http://www.indiedb.com/games/king-machine/videos
174         expect_dict(
175             self,
176             self.ie._extract_jwplayer_data(r'''
177 <script>
178 jwplayer("mediaplayer").setup({"abouttext":"Visit Indie DB","aboutlink":"http:\/\/www.indiedb.com\/","displaytitle":false,"autostart":false,"repeat":false,"title":"king machine trailer 1","sharing":{"link":"http:\/\/www.indiedb.com\/games\/king-machine\/videos\/king-machine-trailer-1","code":"<iframe width=\"560\" height=\"315\" src=\"http:\/\/www.indiedb.com\/media\/iframe\/1522983\" frameborder=\"0\" allowfullscreen><\/iframe><br><a href=\"http:\/\/www.indiedb.com\/games\/king-machine\/videos\/king-machine-trailer-1\">king machine trailer 1 - Indie DB<\/a>"},"related":{"file":"http:\/\/rss.indiedb.com\/media\/recommended\/1522983\/feed\/rss.xml","dimensions":"160x120","onclick":"link"},"sources":[{"file":"http:\/\/cdn.dbolical.com\/cache\/videos\/games\/1\/50\/49678\/encode_mp4\/king-machine-trailer.mp4","label":"360p SD","default":"true"},{"file":"http:\/\/cdn.dbolical.com\/cache\/videos\/games\/1\/50\/49678\/encode720p_mp4\/king-machine-trailer.mp4","label":"720p HD"}],"image":"http:\/\/media.indiedb.com\/cache\/images\/games\/1\/50\/49678\/thumb_620x2000\/king-machine-trailer.mp4.jpg","advertising":{"client":"vast","tag":"http:\/\/ads.intergi.com\/adrawdata\/3.0\/5205\/4251742\/0\/1013\/ADTECH;cors=yes;width=560;height=315;referring_url=http:\/\/www.indiedb.com\/games\/king-machine\/videos\/king-machine-trailer-1;content_url=http:\/\/www.indiedb.com\/games\/king-machine\/videos\/king-machine-trailer-1;media_id=1522983;title=king+machine+trailer+1;device=__DEVICE__;model=__MODEL__;os=Windows+OS;osversion=__OSVERSION__;ua=__UA__;ip=109.171.17.81;uniqueid=1522983;tags=__TAGS__;number=58cac25928151;time=1489683033"},"width":620,"height":349}).once("play", function(event) {
179             videoAnalytics("play");
180 }).once("complete", function(event) {
181     videoAnalytics("completed");
182 });
183 </script>
184                 ''', 'dummy'),
185             {
186                 'title': 'king machine trailer 1',
187                 'thumbnail': 'http://media.indiedb.com/cache/images/games/1/50/49678/thumb_620x2000/king-machine-trailer.mp4.jpg',
188                 'formats': [{
189                     'url': 'http://cdn.dbolical.com/cache/videos/games/1/50/49678/encode_mp4/king-machine-trailer.mp4',
190                     'height': 360,
191                     'ext': 'mp4'
192                 }, {
193                     'url': 'http://cdn.dbolical.com/cache/videos/games/1/50/49678/encode720p_mp4/king-machine-trailer.mp4',
194                     'height': 720,
195                     'ext': 'mp4'
196                 }]
197             })
198
199     def test_parse_m3u8_formats(self):
200         _TEST_CASES = [
201             (
202                 # https://github.com/rg3/youtube-dl/issues/11507
203                 # http://pluzz.francetv.fr/videos/le_ministere.html
204                 'pluzz_francetv_11507',
205                 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/master.m3u8?caption=2017%2F16%2F156589847-1492488987.m3u8%3Afra%3AFrancais&audiotrack=0%3Afra%3AFrancais',
206                 [{
207                     'url': 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/index_0_av.m3u8?null=0',
208                     'manifest_url': 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/master.m3u8?caption=2017%2F16%2F156589847-1492488987.m3u8%3Afra%3AFrancais&audiotrack=0%3Afra%3AFrancais',
209                     'ext': 'mp4',
210                     'format_id': '180',
211                     'protocol': 'm3u8',
212                     'acodec': 'mp4a.40.2',
213                     'vcodec': 'avc1.66.30',
214                     'tbr': 180,
215                     'width': 256,
216                     'height': 144,
217                 }, {
218                     'url': 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/index_1_av.m3u8?null=0',
219                     'manifest_url': 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/master.m3u8?caption=2017%2F16%2F156589847-1492488987.m3u8%3Afra%3AFrancais&audiotrack=0%3Afra%3AFrancais',
220                     'ext': 'mp4',
221                     'format_id': '303',
222                     'protocol': 'm3u8',
223                     'acodec': 'mp4a.40.2',
224                     'vcodec': 'avc1.66.30',
225                     'tbr': 303,
226                     'width': 320,
227                     'height': 180,
228                 }, {
229                     'url': 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/index_2_av.m3u8?null=0',
230                     'manifest_url': 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/master.m3u8?caption=2017%2F16%2F156589847-1492488987.m3u8%3Afra%3AFrancais&audiotrack=0%3Afra%3AFrancais',
231                     'ext': 'mp4',
232                     'format_id': '575',
233                     'protocol': 'm3u8',
234                     'acodec': 'mp4a.40.2',
235                     'vcodec': 'avc1.66.30',
236                     'tbr': 575,
237                     'width': 512,
238                     'height': 288,
239                 }, {
240                     'url': 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/index_3_av.m3u8?null=0',
241                     'manifest_url': 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/master.m3u8?caption=2017%2F16%2F156589847-1492488987.m3u8%3Afra%3AFrancais&audiotrack=0%3Afra%3AFrancais',
242                     'ext': 'mp4',
243                     'format_id': '831',
244                     'protocol': 'm3u8',
245                     'acodec': 'mp4a.40.2',
246                     'vcodec': 'avc1.77.30',
247                     'tbr': 831,
248                     'width': 704,
249                     'height': 396,
250                 }, {
251                     'url': 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/index_4_av.m3u8?null=0',
252                     'manifest_url': 'http://replayftv-vh.akamaihd.net/i/streaming-adaptatif_france-dom-tom/2017/S16/J2/156589847-58f59130c1f52-,standard1,standard2,standard3,standard4,standard5,.mp4.csmil/master.m3u8?caption=2017%2F16%2F156589847-1492488987.m3u8%3Afra%3AFrancais&audiotrack=0%3Afra%3AFrancais',
253                     'ext': 'mp4',
254                     'protocol': 'm3u8',
255                     'format_id': '1467',
256                     'acodec': 'mp4a.40.2',
257                     'vcodec': 'avc1.77.30',
258                     'tbr': 1467,
259                     'width': 1024,
260                     'height': 576,
261                 }]
262             ),
263             (
264                 # https://github.com/rg3/youtube-dl/issues/11995
265                 # http://teamcoco.com/video/clueless-gamer-super-bowl-for-honor
266                 'teamcoco_11995',
267                 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8',
268                 [{
269                     'url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/hls/CONAN_020217_Highlight_show-audio-160k_v4.m3u8',
270                     'manifest_url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8',
271                     'ext': 'mp4',
272                     'format_id': 'audio-0-Default',
273                     'protocol': 'm3u8',
274                     'vcodec': 'none',
275                 }, {
276                     'url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/hls/CONAN_020217_Highlight_show-audio-64k_v4.m3u8',
277                     'manifest_url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8',
278                     'ext': 'mp4',
279                     'format_id': 'audio-1-Default',
280                     'protocol': 'm3u8',
281                     'vcodec': 'none',
282                 }, {
283                     'url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/hls/CONAN_020217_Highlight_show-audio-64k_v4.m3u8',
284                     'manifest_url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8',
285                     'ext': 'mp4',
286                     'format_id': '71',
287                     'protocol': 'm3u8',
288                     'acodec': 'mp4a.40.5',
289                     'vcodec': 'none',
290                     'tbr': 71,
291                 }, {
292                     'url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/hls/CONAN_020217_Highlight_show-400k_v4.m3u8',
293                     'manifest_url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8',
294                     'ext': 'mp4',
295                     'format_id': '413',
296                     'protocol': 'm3u8',
297                     'acodec': 'none',
298                     'vcodec': 'avc1.42001e',
299                     'tbr': 413,
300                     'width': 400,
301                     'height': 224,
302                 }, {
303                     'url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/hls/CONAN_020217_Highlight_show-400k_v4.m3u8',
304                     'manifest_url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8',
305                     'ext': 'mp4',
306                     'format_id': '522',
307                     'protocol': 'm3u8',
308                     'acodec': 'none',
309                     'vcodec': 'avc1.42001e',
310                     'tbr': 522,
311                     'width': 400,
312                     'height': 224,
313                 }, {
314                     'url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/hls/CONAN_020217_Highlight_show-1m_v4.m3u8',
315                     'manifest_url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8',
316                     'ext': 'mp4',
317                     'format_id': '1205',
318                     'protocol': 'm3u8',
319                     'acodec': 'none',
320                     'vcodec': 'avc1.4d001e',
321                     'tbr': 1205,
322                     'width': 640,
323                     'height': 360,
324                 }, {
325                     'url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/hls/CONAN_020217_Highlight_show-2m_v4.m3u8',
326                     'manifest_url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8',
327                     'ext': 'mp4',
328                     'format_id': '2374',
329                     'protocol': 'm3u8',
330                     'acodec': 'none',
331                     'vcodec': 'avc1.4d001f',
332                     'tbr': 2374,
333                     'width': 1024,
334                     'height': 576,
335                 }]
336             ),
337             (
338                 # https://github.com/rg3/youtube-dl/issues/12211
339                 # http://video.toggle.sg/en/series/whoopie-s-world/ep3/478601
340                 'toggle_mobile_12211',
341                 'http://cdnapi.kaltura.com/p/2082311/sp/208231100/playManifest/protocol/http/entryId/0_89q6e8ku/format/applehttp/tags/mobile_sd/f/a.m3u8',
342                 [{
343                     'url': 'http://k.toggle.sg/fhls/p/2082311/sp/208231100/serveFlavor/entryId/0_89q6e8ku/v/2/pv/1/flavorId/0_sa2ntrdg/name/a.mp4/index.m3u8',
344                     'manifest_url': 'http://cdnapi.kaltura.com/p/2082311/sp/208231100/playManifest/protocol/http/entryId/0_89q6e8ku/format/applehttp/tags/mobile_sd/f/a.m3u8',
345                     'ext': 'mp4',
346                     'format_id': 'audio-English',
347                     'protocol': 'm3u8',
348                     'language': 'eng',
349                     'vcodec': 'none',
350                 }, {
351                     'url': 'http://k.toggle.sg/fhls/p/2082311/sp/208231100/serveFlavor/entryId/0_89q6e8ku/v/2/pv/1/flavorId/0_r7y0nitg/name/a.mp4/index.m3u8',
352                     'manifest_url': 'http://cdnapi.kaltura.com/p/2082311/sp/208231100/playManifest/protocol/http/entryId/0_89q6e8ku/format/applehttp/tags/mobile_sd/f/a.m3u8',
353                     'ext': 'mp4',
354                     'format_id': 'audio-Undefined',
355                     'protocol': 'm3u8',
356                     'language': 'und',
357                     'vcodec': 'none',
358                 }, {
359                     'url': 'http://k.toggle.sg/fhls/p/2082311/sp/208231100/serveFlavor/entryId/0_89q6e8ku/v/2/pv/1/flavorId/0_qlk9hlzr/name/a.mp4/index.m3u8',
360                     'manifest_url': 'http://cdnapi.kaltura.com/p/2082311/sp/208231100/playManifest/protocol/http/entryId/0_89q6e8ku/format/applehttp/tags/mobile_sd/f/a.m3u8',
361                     'ext': 'mp4',
362                     'format_id': '155',
363                     'protocol': 'm3u8',
364                     'tbr': 155.648,
365                     'width': 320,
366                     'height': 180,
367                 }, {
368                     'url': 'http://k.toggle.sg/fhls/p/2082311/sp/208231100/serveFlavor/entryId/0_89q6e8ku/v/2/pv/1/flavorId/0_oefackmi/name/a.mp4/index.m3u8',
369                     'manifest_url': 'http://cdnapi.kaltura.com/p/2082311/sp/208231100/playManifest/protocol/http/entryId/0_89q6e8ku/format/applehttp/tags/mobile_sd/f/a.m3u8',
370                     'ext': 'mp4',
371                     'format_id': '502',
372                     'protocol': 'm3u8',
373                     'tbr': 502.784,
374                     'width': 480,
375                     'height': 270,
376                 }, {
377                     'url': 'http://k.toggle.sg/fhls/p/2082311/sp/208231100/serveFlavor/entryId/0_89q6e8ku/v/12/pv/1/flavorId/0_vyg9pj7k/name/a.mp4/index.m3u8',
378                     'manifest_url': 'http://cdnapi.kaltura.com/p/2082311/sp/208231100/playManifest/protocol/http/entryId/0_89q6e8ku/format/applehttp/tags/mobile_sd/f/a.m3u8',
379                     'ext': 'mp4',
380                     'format_id': '827',
381                     'protocol': 'm3u8',
382                     'tbr': 827.392,
383                     'width': 640,
384                     'height': 360,
385                 }, {
386                     'url': 'http://k.toggle.sg/fhls/p/2082311/sp/208231100/serveFlavor/entryId/0_89q6e8ku/v/12/pv/1/flavorId/0_50n4psvx/name/a.mp4/index.m3u8',
387                     'manifest_url': 'http://cdnapi.kaltura.com/p/2082311/sp/208231100/playManifest/protocol/http/entryId/0_89q6e8ku/format/applehttp/tags/mobile_sd/f/a.m3u8',
388                     'ext': 'mp4',
389                     'format_id': '1396',
390                     'protocol': 'm3u8',
391                     'tbr': 1396.736,
392                     'width': 854,
393                     'height': 480,
394                 }]
395             ),
396             (
397                 # http://www.twitch.tv/riotgames/v/6528877
398                 'twitch_vod',
399                 'https://usher.ttvnw.net/vod/6528877?allow_source=true&allow_audio_only=true&allow_spectre=true&player=twitchweb&nauth=%7B%22user_id%22%3Anull%2C%22vod_id%22%3A6528877%2C%22expires%22%3A1492887874%2C%22chansub%22%3A%7B%22restricted_bitrates%22%3A%5B%5D%7D%2C%22privileged%22%3Afalse%2C%22https_required%22%3Afalse%7D&nauthsig=3e29296a6824a0f48f9e731383f77a614fc79bee',
400                 [{
401                     'url': 'https://vod.edgecast.hls.ttvnw.net/e5da31ab49_riotgames_15001215120_261543898/audio_only/index-muted-HM49I092CC.m3u8',
402                     'manifest_url': 'https://usher.ttvnw.net/vod/6528877?allow_source=true&allow_audio_only=true&allow_spectre=true&player=twitchweb&nauth=%7B%22user_id%22%3Anull%2C%22vod_id%22%3A6528877%2C%22expires%22%3A1492887874%2C%22chansub%22%3A%7B%22restricted_bitrates%22%3A%5B%5D%7D%2C%22privileged%22%3Afalse%2C%22https_required%22%3Afalse%7D&nauthsig=3e29296a6824a0f48f9e731383f77a614fc79bee',
403                     'ext': 'mp4',
404                     'format_id': 'Audio Only',
405                     'protocol': 'm3u8',
406                     'acodec': 'mp4a.40.2',
407                     'vcodec': 'none',
408                     'tbr': 182.725,
409                 }, {
410                     'url': 'https://vod.edgecast.hls.ttvnw.net/e5da31ab49_riotgames_15001215120_261543898/mobile/index-muted-HM49I092CC.m3u8',
411                     'manifest_url': 'https://usher.ttvnw.net/vod/6528877?allow_source=true&allow_audio_only=true&allow_spectre=true&player=twitchweb&nauth=%7B%22user_id%22%3Anull%2C%22vod_id%22%3A6528877%2C%22expires%22%3A1492887874%2C%22chansub%22%3A%7B%22restricted_bitrates%22%3A%5B%5D%7D%2C%22privileged%22%3Afalse%2C%22https_required%22%3Afalse%7D&nauthsig=3e29296a6824a0f48f9e731383f77a614fc79bee',
412                     'ext': 'mp4',
413                     'format_id': 'Mobile',
414                     'protocol': 'm3u8',
415                     'acodec': 'mp4a.40.2',
416                     'vcodec': 'avc1.42C00D',
417                     'tbr': 280.474,
418                     'width': 400,
419                     'height': 226,
420                 }, {
421                     'url': 'https://vod.edgecast.hls.ttvnw.net/e5da31ab49_riotgames_15001215120_261543898/low/index-muted-HM49I092CC.m3u8',
422                     'manifest_url': 'https://usher.ttvnw.net/vod/6528877?allow_source=true&allow_audio_only=true&allow_spectre=true&player=twitchweb&nauth=%7B%22user_id%22%3Anull%2C%22vod_id%22%3A6528877%2C%22expires%22%3A1492887874%2C%22chansub%22%3A%7B%22restricted_bitrates%22%3A%5B%5D%7D%2C%22privileged%22%3Afalse%2C%22https_required%22%3Afalse%7D&nauthsig=3e29296a6824a0f48f9e731383f77a614fc79bee',
423                     'ext': 'mp4',
424                     'format_id': 'Low',
425                     'protocol': 'm3u8',
426                     'acodec': 'mp4a.40.2',
427                     'vcodec': 'avc1.42C01E',
428                     'tbr': 628.347,
429                     'width': 640,
430                     'height': 360,
431                 }, {
432                     'url': 'https://vod.edgecast.hls.ttvnw.net/e5da31ab49_riotgames_15001215120_261543898/medium/index-muted-HM49I092CC.m3u8',
433                     'manifest_url': 'https://usher.ttvnw.net/vod/6528877?allow_source=true&allow_audio_only=true&allow_spectre=true&player=twitchweb&nauth=%7B%22user_id%22%3Anull%2C%22vod_id%22%3A6528877%2C%22expires%22%3A1492887874%2C%22chansub%22%3A%7B%22restricted_bitrates%22%3A%5B%5D%7D%2C%22privileged%22%3Afalse%2C%22https_required%22%3Afalse%7D&nauthsig=3e29296a6824a0f48f9e731383f77a614fc79bee',
434                     'ext': 'mp4',
435                     'format_id': 'Medium',
436                     'protocol': 'm3u8',
437                     'acodec': 'mp4a.40.2',
438                     'vcodec': 'avc1.42C01E',
439                     'tbr': 893.387,
440                     'width': 852,
441                     'height': 480,
442                 }, {
443                     'url': 'https://vod.edgecast.hls.ttvnw.net/e5da31ab49_riotgames_15001215120_261543898/high/index-muted-HM49I092CC.m3u8',
444                     'manifest_url': 'https://usher.ttvnw.net/vod/6528877?allow_source=true&allow_audio_only=true&allow_spectre=true&player=twitchweb&nauth=%7B%22user_id%22%3Anull%2C%22vod_id%22%3A6528877%2C%22expires%22%3A1492887874%2C%22chansub%22%3A%7B%22restricted_bitrates%22%3A%5B%5D%7D%2C%22privileged%22%3Afalse%2C%22https_required%22%3Afalse%7D&nauthsig=3e29296a6824a0f48f9e731383f77a614fc79bee',
445                     'ext': 'mp4',
446                     'format_id': 'High',
447                     'protocol': 'm3u8',
448                     'acodec': 'mp4a.40.2',
449                     'vcodec': 'avc1.42C01F',
450                     'tbr': 1603.789,
451                     'width': 1280,
452                     'height': 720,
453                 }, {
454                     'url': 'https://vod.edgecast.hls.ttvnw.net/e5da31ab49_riotgames_15001215120_261543898/chunked/index-muted-HM49I092CC.m3u8',
455                     'manifest_url': 'https://usher.ttvnw.net/vod/6528877?allow_source=true&allow_audio_only=true&allow_spectre=true&player=twitchweb&nauth=%7B%22user_id%22%3Anull%2C%22vod_id%22%3A6528877%2C%22expires%22%3A1492887874%2C%22chansub%22%3A%7B%22restricted_bitrates%22%3A%5B%5D%7D%2C%22privileged%22%3Afalse%2C%22https_required%22%3Afalse%7D&nauthsig=3e29296a6824a0f48f9e731383f77a614fc79bee',
456                     'ext': 'mp4',
457                     'format_id': 'Source',
458                     'protocol': 'm3u8',
459                     'acodec': 'mp4a.40.2',
460                     'vcodec': 'avc1.100.31',
461                     'tbr': 3214.134,
462                     'width': 1280,
463                     'height': 720,
464                 }]
465             ),
466             (
467                 # http://www.vidio.com/watch/165683-dj_ambred-booyah-live-2015
468                 # EXT-X-STREAM-INF tag with NAME attribute that is not defined
469                 # in HLS specification
470                 'vidio',
471                 'https://www.vidio.com/videos/165683/playlist.m3u8',
472                 [{
473                     'url': 'https://cdn1-a.production.vidio.static6.com/uploads/165683/dj_ambred-4383-b300.mp4.m3u8',
474                     'manifest_url': 'https://www.vidio.com/videos/165683/playlist.m3u8',
475                     'ext': 'mp4',
476                     'format_id': '270p 3G',
477                     'protocol': 'm3u8',
478                     'tbr': 300,
479                     'width': 480,
480                     'height': 270,
481                 }, {
482                     'url': 'https://cdn1-a.production.vidio.static6.com/uploads/165683/dj_ambred-4383-b600.mp4.m3u8',
483                     'manifest_url': 'https://www.vidio.com/videos/165683/playlist.m3u8',
484                     'ext': 'mp4',
485                     'format_id': '360p SD',
486                     'protocol': 'm3u8',
487                     'tbr': 600,
488                     'width': 640,
489                     'height': 360,
490                 }, {
491                     'url': 'https://cdn1-a.production.vidio.static6.com/uploads/165683/dj_ambred-4383-b1200.mp4.m3u8',
492                     'manifest_url': 'https://www.vidio.com/videos/165683/playlist.m3u8',
493                     'ext': 'mp4',
494                     'format_id': '720p HD',
495                     'protocol': 'm3u8',
496                     'tbr': 1200,
497                     'width': 1280,
498                     'height': 720,
499                 }]
500             )
501         ]
502
503         for m3u8_file, m3u8_url, expected_formats in _TEST_CASES:
504             with io.open('./test/testdata/m3u8/%s.m3u8' % m3u8_file,
505                          mode='r', encoding='utf-8') as f:
506                 formats = self.ie._parse_m3u8_formats(
507                     f.read(), m3u8_url, ext='mp4')
508                 self.ie._sort_formats(formats)
509                 expect_value(self, formats, expected_formats, None)
510
511     def test_parse_mpd_formats(self):
512         _TEST_CASES = [
513             (
514                 # https://github.com/rg3/youtube-dl/issues/13919
515                 # Also tests duplicate representation ids, see
516                 # https://github.com/rg3/youtube-dl/issues/15111
517                 'float_duration',
518                 'http://unknown/manifest.mpd',
519                 [{
520                     'manifest_url': 'http://unknown/manifest.mpd',
521                     'ext': 'm4a',
522                     'format_id': '318597',
523                     'format_note': 'DASH audio',
524                     'protocol': 'http_dash_segments',
525                     'acodec': 'mp4a.40.2',
526                     'vcodec': 'none',
527                     'tbr': 61.587,
528                 }, {
529                     'manifest_url': 'http://unknown/manifest.mpd',
530                     'ext': 'mp4',
531                     'format_id': '318597',
532                     'format_note': 'DASH video',
533                     'protocol': 'http_dash_segments',
534                     'acodec': 'none',
535                     'vcodec': 'avc1.42001f',
536                     'tbr': 318.597,
537                     'width': 340,
538                     'height': 192,
539                 }, {
540                     'manifest_url': 'http://unknown/manifest.mpd',
541                     'ext': 'mp4',
542                     'format_id': '638590',
543                     'format_note': 'DASH video',
544                     'protocol': 'http_dash_segments',
545                     'acodec': 'none',
546                     'vcodec': 'avc1.42001f',
547                     'tbr': 638.59,
548                     'width': 512,
549                     'height': 288,
550                 }, {
551                     'manifest_url': 'http://unknown/manifest.mpd',
552                     'ext': 'mp4',
553                     'format_id': '1022565',
554                     'format_note': 'DASH video',
555                     'protocol': 'http_dash_segments',
556                     'acodec': 'none',
557                     'vcodec': 'avc1.4d001f',
558                     'tbr': 1022.565,
559                     'width': 688,
560                     'height': 384,
561                 }, {
562                     'manifest_url': 'http://unknown/manifest.mpd',
563                     'ext': 'mp4',
564                     'format_id': '2046506',
565                     'format_note': 'DASH video',
566                     'protocol': 'http_dash_segments',
567                     'acodec': 'none',
568                     'vcodec': 'avc1.4d001f',
569                     'tbr': 2046.506,
570                     'width': 1024,
571                     'height': 576,
572                 }, {
573                     'manifest_url': 'http://unknown/manifest.mpd',
574                     'ext': 'mp4',
575                     'format_id': '3998017',
576                     'format_note': 'DASH video',
577                     'protocol': 'http_dash_segments',
578                     'acodec': 'none',
579                     'vcodec': 'avc1.640029',
580                     'tbr': 3998.017,
581                     'width': 1280,
582                     'height': 720,
583                 }, {
584                     'manifest_url': 'http://unknown/manifest.mpd',
585                     'ext': 'mp4',
586                     'format_id': '5997485',
587                     'format_note': 'DASH video',
588                     'protocol': 'http_dash_segments',
589                     'acodec': 'none',
590                     'vcodec': 'avc1.640032',
591                     'tbr': 5997.485,
592                     'width': 1920,
593                     'height': 1080,
594                 }]
595             ), (
596                 # https://github.com/rg3/youtube-dl/pull/14844
597                 'urls_only',
598                 'http://unknown/manifest.mpd',
599                 [{
600                     'manifest_url': 'http://unknown/manifest.mpd',
601                     'ext': 'mp4',
602                     'format_id': 'h264_aac_144p_m4s',
603                     'format_note': 'DASH video',
604                     'protocol': 'http_dash_segments',
605                     'acodec': 'mp4a.40.2',
606                     'vcodec': 'avc3.42c01e',
607                     'tbr': 200,
608                     'width': 256,
609                     'height': 144,
610                 }, {
611                     'manifest_url': 'http://unknown/manifest.mpd',
612                     'ext': 'mp4',
613                     'format_id': 'h264_aac_240p_m4s',
614                     'format_note': 'DASH video',
615                     'protocol': 'http_dash_segments',
616                     'acodec': 'mp4a.40.2',
617                     'vcodec': 'avc3.42c01e',
618                     'tbr': 400,
619                     'width': 424,
620                     'height': 240,
621                 }, {
622                     'manifest_url': 'http://unknown/manifest.mpd',
623                     'ext': 'mp4',
624                     'format_id': 'h264_aac_360p_m4s',
625                     'format_note': 'DASH video',
626                     'protocol': 'http_dash_segments',
627                     'acodec': 'mp4a.40.2',
628                     'vcodec': 'avc3.42c01e',
629                     'tbr': 800,
630                     'width': 640,
631                     'height': 360,
632                 }, {
633                     'manifest_url': 'http://unknown/manifest.mpd',
634                     'ext': 'mp4',
635                     'format_id': 'h264_aac_480p_m4s',
636                     'format_note': 'DASH video',
637                     'protocol': 'http_dash_segments',
638                     'acodec': 'mp4a.40.2',
639                     'vcodec': 'avc3.42c01e',
640                     'tbr': 1200,
641                     'width': 856,
642                     'height': 480,
643                 }, {
644                     'manifest_url': 'http://unknown/manifest.mpd',
645                     'ext': 'mp4',
646                     'format_id': 'h264_aac_576p_m4s',
647                     'format_note': 'DASH video',
648                     'protocol': 'http_dash_segments',
649                     'acodec': 'mp4a.40.2',
650                     'vcodec': 'avc3.42c01e',
651                     'tbr': 1600,
652                     'width': 1024,
653                     'height': 576,
654                 }, {
655                     'manifest_url': 'http://unknown/manifest.mpd',
656                     'ext': 'mp4',
657                     'format_id': 'h264_aac_720p_m4s',
658                     'format_note': 'DASH video',
659                     'protocol': 'http_dash_segments',
660                     'acodec': 'mp4a.40.2',
661                     'vcodec': 'avc3.42c01e',
662                     'tbr': 2400,
663                     'width': 1280,
664                     'height': 720,
665                 }, {
666                     'manifest_url': 'http://unknown/manifest.mpd',
667                     'ext': 'mp4',
668                     'format_id': 'h264_aac_1080p_m4s',
669                     'format_note': 'DASH video',
670                     'protocol': 'http_dash_segments',
671                     'acodec': 'mp4a.40.2',
672                     'vcodec': 'avc3.42c01e',
673                     'tbr': 4400,
674                     'width': 1920,
675                     'height': 1080,
676                 }]
677             )
678         ]
679
680         for mpd_file, mpd_url, expected_formats in _TEST_CASES:
681             with io.open('./test/testdata/mpd/%s.mpd' % mpd_file,
682                          mode='r', encoding='utf-8') as f:
683                 formats = self.ie._parse_mpd_formats(
684                     compat_etree_fromstring(f.read().encode('utf-8')),
685                     mpd_url=mpd_url)
686                 self.ie._sort_formats(formats)
687                 expect_value(self, formats, expected_formats, None)
688
689     def test_parse_f4m_formats(self):
690         _TEST_CASES = [
691             (
692                 # https://github.com/rg3/youtube-dl/issues/14660
693                 'custom_base_url',
694                 'http://api.new.livestream.com/accounts/6115179/events/6764928/videos/144884262.f4m',
695                 [{
696                     'manifest_url': 'http://api.new.livestream.com/accounts/6115179/events/6764928/videos/144884262.f4m',
697                     'ext': 'flv',
698                     'format_id': '2148',
699                     'protocol': 'f4m',
700                     'tbr': 2148,
701                     'width': 1280,
702                     'height': 720,
703                 }]
704             ),
705         ]
706
707         for f4m_file, f4m_url, expected_formats in _TEST_CASES:
708             with io.open('./test/testdata/f4m/%s.f4m' % f4m_file,
709                          mode='r', encoding='utf-8') as f:
710                 formats = self.ie._parse_f4m_formats(
711                     compat_etree_fromstring(f.read().encode('utf-8')),
712                     f4m_url, None)
713                 self.ie._sort_formats(formats)
714                 expect_value(self, formats, expected_formats, None)
715
716     def test_parse_xspf(self):
717         _TEST_CASES = [
718             (
719                 'foo_xspf',
720                 'https://example.org/src/foo_xspf.xspf',
721                 [{
722                     'id': 'foo_xspf',
723                     'title': 'Pandemonium',
724                     'description': 'Visit http://bigbrother404.bandcamp.com',
725                     'duration': 202.416,
726                     'formats': [{
727                         'manifest_url': 'https://example.org/src/foo_xspf.xspf',
728                         'url': 'https://example.org/src/cd1/track%201.mp3',
729                     }],
730                 }, {
731                     'id': 'foo_xspf',
732                     'title': 'Final Cartridge (Nichico Twelve Remix)',
733                     'description': 'Visit http://bigbrother404.bandcamp.com',
734                     'duration': 255.857,
735                     'formats': [{
736                         'manifest_url': 'https://example.org/src/foo_xspf.xspf',
737                         'url': 'https://example.org/%E3%83%88%E3%83%A9%E3%83%83%E3%82%AF%E3%80%80%EF%BC%92.mp3',
738                     }],
739                 }, {
740                     'id': 'foo_xspf',
741                     'title': 'Rebuilding Nightingale',
742                     'description': 'Visit http://bigbrother404.bandcamp.com',
743                     'duration': 287.915,
744                     'formats': [{
745                         'manifest_url': 'https://example.org/src/foo_xspf.xspf',
746                         'url': 'https://example.org/src/track3.mp3',
747                     }, {
748                         'manifest_url': 'https://example.org/src/foo_xspf.xspf',
749                         'url': 'https://example.com/track3.mp3',
750                     }]
751                 }]
752             ),
753         ]
754
755         for xspf_file, xspf_url, expected_entries in _TEST_CASES:
756             with io.open('./test/testdata/xspf/%s.xspf' % xspf_file,
757                          mode='r', encoding='utf-8') as f:
758                 entries = self.ie._parse_xspf(
759                     compat_etree_fromstring(f.read().encode('utf-8')),
760                     xspf_file, xspf_url=xspf_url, xspf_base_url=xspf_url)
761                 expect_value(self, entries, expected_entries, None)
762                 for i in range(len(entries)):
763                     expect_dict(self, entries[i], expected_entries[i])
764
765     def test_response_with_expected_status_returns_content(self):
766         # Checks for mitigations against the effects of
767         # <https://bugs.python.org/issue15002> that affect Python 3.4.1+, which
768         # manifest as `_download_webpage`, `_download_xml`, `_download_json`,
769         # or the underlying `_download_webpage_handle` returning no content
770         # when a response matches `expected_status`.
771
772         httpd = compat_http_server.HTTPServer(
773             ('127.0.0.1', 0), InfoExtractorTestRequestHandler)
774         port = http_server_port(httpd)
775         server_thread = threading.Thread(target=httpd.serve_forever)
776         server_thread.daemon = True
777         server_thread.start()
778
779         (content, urlh) = self.ie._download_webpage_handle(
780             'http://127.0.0.1:%d/teapot' % port, None,
781             expected_status=TEAPOT_RESPONSE_STATUS)
782         self.assertEqual(content, TEAPOT_RESPONSE_BODY)
783
784
785 if __name__ == '__main__':
786     unittest.main()