Start moving to ytdl-org
[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             <meta property=og-test3 content='Ill-formatted opengraph'/>
65             '''
66         self.assertEqual(ie._og_search_title(html), 'Foo')
67         self.assertEqual(ie._og_search_description(html), 'Some video\'s description ')
68         self.assertEqual(ie._og_search_thumbnail(html), 'http://domain.com/pic.jpg?key1=val1&key2=val2')
69         self.assertEqual(ie._og_search_video_url(html, default=None), None)
70         self.assertEqual(ie._og_search_property('foobar', html), 'Foo')
71         self.assertEqual(ie._og_search_property('test1', html), 'foo > < bar')
72         self.assertEqual(ie._og_search_property('test2', html), 'foo >//< bar')
73         self.assertEqual(ie._og_search_property('test3', html), 'Ill-formatted opengraph')
74         self.assertEqual(ie._og_search_property(('test0', 'test1'), html), 'foo > < bar')
75         self.assertRaises(RegexNotFoundError, ie._og_search_property, 'test0', html, None, fatal=True)
76         self.assertRaises(RegexNotFoundError, ie._og_search_property, ('test0', 'test00'), html, None, fatal=True)
77
78     def test_html_search_meta(self):
79         ie = self.ie
80         html = '''
81             <meta name="a" content="1" />
82             <meta name='b' content='2'>
83             <meta name="c" content='3'>
84             <meta name=d content='4'>
85             <meta property="e" content='5' >
86             <meta content="6" name="f">
87         '''
88
89         self.assertEqual(ie._html_search_meta('a', html), '1')
90         self.assertEqual(ie._html_search_meta('b', html), '2')
91         self.assertEqual(ie._html_search_meta('c', html), '3')
92         self.assertEqual(ie._html_search_meta('d', html), '4')
93         self.assertEqual(ie._html_search_meta('e', html), '5')
94         self.assertEqual(ie._html_search_meta('f', html), '6')
95         self.assertEqual(ie._html_search_meta(('a', 'b', 'c'), html), '1')
96         self.assertEqual(ie._html_search_meta(('c', 'b', 'a'), html), '3')
97         self.assertEqual(ie._html_search_meta(('z', 'x', 'c'), html), '3')
98         self.assertRaises(RegexNotFoundError, ie._html_search_meta, 'z', html, None, fatal=True)
99         self.assertRaises(RegexNotFoundError, ie._html_search_meta, ('z', 'x'), html, None, fatal=True)
100
101     def test_download_json(self):
102         uri = encode_data_uri(b'{"foo": "blah"}', 'application/json')
103         self.assertEqual(self.ie._download_json(uri, None), {'foo': 'blah'})
104         uri = encode_data_uri(b'callback({"foo": "blah"})', 'application/javascript')
105         self.assertEqual(self.ie._download_json(uri, None, transform_source=strip_jsonp), {'foo': 'blah'})
106         uri = encode_data_uri(b'{"foo": invalid}', 'application/json')
107         self.assertRaises(ExtractorError, self.ie._download_json, uri, None)
108         self.assertEqual(self.ie._download_json(uri, None, fatal=False), None)
109
110     def test_extract_jwplayer_data_realworld(self):
111         # from http://www.suffolk.edu/sjc/
112         expect_dict(
113             self,
114             self.ie._extract_jwplayer_data(r'''
115                 <script type='text/javascript'>
116                     jwplayer('my-video').setup({
117                         file: 'rtmp://192.138.214.154/live/sjclive',
118                         fallback: 'true',
119                         width: '95%',
120                       aspectratio: '16:9',
121                       primary: 'flash',
122                       mediaid:'XEgvuql4'
123                     });
124                 </script>
125                 ''', None, require_title=False),
126             {
127                 'id': 'XEgvuql4',
128                 'formats': [{
129                     'url': 'rtmp://192.138.214.154/live/sjclive',
130                     'ext': 'flv'
131                 }]
132             })
133
134         # from https://www.pornoxo.com/videos/7564/striptease-from-sexy-secretary/
135         expect_dict(
136             self,
137             self.ie._extract_jwplayer_data(r'''
138 <script type="text/javascript">
139     jwplayer("mediaplayer").setup({
140         'videoid': "7564",
141         'width': "100%",
142         'aspectratio': "16:9",
143         'stretching': "exactfit",
144         'autostart': 'false',
145         'flashplayer': "https://t04.vipstreamservice.com/jwplayer/v5.10/player.swf",
146         '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",
147         'image': "https://t03.vipstreamservice.com/thumbs/pxo-full/2009-12/14/a4b2157147afe5efa93ce1978e0265289c193874e02597.flv-full-13.jpg",
148         '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",
149         'logo.hide': true,
150         'skin': "https://t04.vipstreamservice.com/jwplayer/skin/modieus-blk.zip",
151         'plugins': "https://t04.vipstreamservice.com/jwplayer/dock/dockableskinnableplugin.swf",
152         'dockableskinnableplugin.piclink': "/index.php?key=ajax-videothumbsn&vid=7564&data=2009-12--14--4b2157147afe5efa93ce1978e0265289c193874e02597.flv--17370",
153         'controlbar': 'bottom',
154         'modes': [
155             {type: 'flash', src: 'https://t04.vipstreamservice.com/jwplayer/v5.10/player.swf'}
156         ],
157         'provider': 'http'
158     });
159     //noinspection JSAnnotator
160     invideo.setup({
161         adsUrl: "/banner-iframe/?zoneId=32",
162         adsUrl2: "",
163         autostart: false
164     });
165 </script>
166             ''', 'dummy', require_title=False),
167             {
168                 'thumbnail': 'https://t03.vipstreamservice.com/thumbs/pxo-full/2009-12/14/a4b2157147afe5efa93ce1978e0265289c193874e02597.flv-full-13.jpg',
169                 'formats': [{
170                     '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',
171                     'ext': 'flv'
172                 }]
173             })
174
175         # from http://www.indiedb.com/games/king-machine/videos
176         expect_dict(
177             self,
178             self.ie._extract_jwplayer_data(r'''
179 <script>
180 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) {
181             videoAnalytics("play");
182 }).once("complete", function(event) {
183     videoAnalytics("completed");
184 });
185 </script>
186                 ''', 'dummy'),
187             {
188                 'title': 'king machine trailer 1',
189                 'thumbnail': 'http://media.indiedb.com/cache/images/games/1/50/49678/thumb_620x2000/king-machine-trailer.mp4.jpg',
190                 'formats': [{
191                     'url': 'http://cdn.dbolical.com/cache/videos/games/1/50/49678/encode_mp4/king-machine-trailer.mp4',
192                     'height': 360,
193                     'ext': 'mp4'
194                 }, {
195                     'url': 'http://cdn.dbolical.com/cache/videos/games/1/50/49678/encode720p_mp4/king-machine-trailer.mp4',
196                     'height': 720,
197                     'ext': 'mp4'
198                 }]
199             })
200
201     def test_parse_m3u8_formats(self):
202         _TEST_CASES = [
203             (
204                 # https://github.com/ytdl-org/youtube-dl/issues/11507
205                 # http://pluzz.francetv.fr/videos/le_ministere.html
206                 'pluzz_francetv_11507',
207                 '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',
208                 [{
209                     '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',
210                     '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',
211                     'ext': 'mp4',
212                     'format_id': '180',
213                     'protocol': 'm3u8',
214                     'acodec': 'mp4a.40.2',
215                     'vcodec': 'avc1.66.30',
216                     'tbr': 180,
217                     'width': 256,
218                     'height': 144,
219                 }, {
220                     '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',
221                     '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',
222                     'ext': 'mp4',
223                     'format_id': '303',
224                     'protocol': 'm3u8',
225                     'acodec': 'mp4a.40.2',
226                     'vcodec': 'avc1.66.30',
227                     'tbr': 303,
228                     'width': 320,
229                     'height': 180,
230                 }, {
231                     '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',
232                     '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',
233                     'ext': 'mp4',
234                     'format_id': '575',
235                     'protocol': 'm3u8',
236                     'acodec': 'mp4a.40.2',
237                     'vcodec': 'avc1.66.30',
238                     'tbr': 575,
239                     'width': 512,
240                     'height': 288,
241                 }, {
242                     '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',
243                     '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',
244                     'ext': 'mp4',
245                     'format_id': '831',
246                     'protocol': 'm3u8',
247                     'acodec': 'mp4a.40.2',
248                     'vcodec': 'avc1.77.30',
249                     'tbr': 831,
250                     'width': 704,
251                     'height': 396,
252                 }, {
253                     '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',
254                     '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',
255                     'ext': 'mp4',
256                     'protocol': 'm3u8',
257                     'format_id': '1467',
258                     'acodec': 'mp4a.40.2',
259                     'vcodec': 'avc1.77.30',
260                     'tbr': 1467,
261                     'width': 1024,
262                     'height': 576,
263                 }]
264             ),
265             (
266                 # https://github.com/ytdl-org/youtube-dl/issues/11995
267                 # http://teamcoco.com/video/clueless-gamer-super-bowl-for-honor
268                 'teamcoco_11995',
269                 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8',
270                 [{
271                     'url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/hls/CONAN_020217_Highlight_show-audio-160k_v4.m3u8',
272                     'manifest_url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8',
273                     'ext': 'mp4',
274                     'format_id': 'audio-0-Default',
275                     'protocol': 'm3u8',
276                     'vcodec': 'none',
277                 }, {
278                     'url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/hls/CONAN_020217_Highlight_show-audio-64k_v4.m3u8',
279                     'manifest_url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8',
280                     'ext': 'mp4',
281                     'format_id': 'audio-1-Default',
282                     'protocol': 'm3u8',
283                     'vcodec': 'none',
284                 }, {
285                     'url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/hls/CONAN_020217_Highlight_show-audio-64k_v4.m3u8',
286                     'manifest_url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8',
287                     'ext': 'mp4',
288                     'format_id': '71',
289                     'protocol': 'm3u8',
290                     'acodec': 'mp4a.40.5',
291                     'vcodec': 'none',
292                     'tbr': 71,
293                 }, {
294                     'url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/hls/CONAN_020217_Highlight_show-400k_v4.m3u8',
295                     'manifest_url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8',
296                     'ext': 'mp4',
297                     'format_id': '413',
298                     'protocol': 'm3u8',
299                     'acodec': 'none',
300                     'vcodec': 'avc1.42001e',
301                     'tbr': 413,
302                     'width': 400,
303                     'height': 224,
304                 }, {
305                     'url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/hls/CONAN_020217_Highlight_show-400k_v4.m3u8',
306                     'manifest_url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8',
307                     'ext': 'mp4',
308                     'format_id': '522',
309                     'protocol': 'm3u8',
310                     'acodec': 'none',
311                     'vcodec': 'avc1.42001e',
312                     'tbr': 522,
313                     'width': 400,
314                     'height': 224,
315                 }, {
316                     'url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/hls/CONAN_020217_Highlight_show-1m_v4.m3u8',
317                     'manifest_url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8',
318                     'ext': 'mp4',
319                     'format_id': '1205',
320                     'protocol': 'm3u8',
321                     'acodec': 'none',
322                     'vcodec': 'avc1.4d001e',
323                     'tbr': 1205,
324                     'width': 640,
325                     'height': 360,
326                 }, {
327                     'url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/hls/CONAN_020217_Highlight_show-2m_v4.m3u8',
328                     'manifest_url': 'http://ak.storage-w.teamcococdn.com/cdn/2017-02/98599/ed8f/main.m3u8',
329                     'ext': 'mp4',
330                     'format_id': '2374',
331                     'protocol': 'm3u8',
332                     'acodec': 'none',
333                     'vcodec': 'avc1.4d001f',
334                     'tbr': 2374,
335                     'width': 1024,
336                     'height': 576,
337                 }]
338             ),
339             (
340                 # https://github.com/ytdl-org/youtube-dl/issues/12211
341                 # http://video.toggle.sg/en/series/whoopie-s-world/ep3/478601
342                 'toggle_mobile_12211',
343                 'http://cdnapi.kaltura.com/p/2082311/sp/208231100/playManifest/protocol/http/entryId/0_89q6e8ku/format/applehttp/tags/mobile_sd/f/a.m3u8',
344                 [{
345                     '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',
346                     'manifest_url': 'http://cdnapi.kaltura.com/p/2082311/sp/208231100/playManifest/protocol/http/entryId/0_89q6e8ku/format/applehttp/tags/mobile_sd/f/a.m3u8',
347                     'ext': 'mp4',
348                     'format_id': 'audio-English',
349                     'protocol': 'm3u8',
350                     'language': 'eng',
351                     'vcodec': 'none',
352                 }, {
353                     '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',
354                     'manifest_url': 'http://cdnapi.kaltura.com/p/2082311/sp/208231100/playManifest/protocol/http/entryId/0_89q6e8ku/format/applehttp/tags/mobile_sd/f/a.m3u8',
355                     'ext': 'mp4',
356                     'format_id': 'audio-Undefined',
357                     'protocol': 'm3u8',
358                     'language': 'und',
359                     'vcodec': 'none',
360                 }, {
361                     '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',
362                     'manifest_url': 'http://cdnapi.kaltura.com/p/2082311/sp/208231100/playManifest/protocol/http/entryId/0_89q6e8ku/format/applehttp/tags/mobile_sd/f/a.m3u8',
363                     'ext': 'mp4',
364                     'format_id': '155',
365                     'protocol': 'm3u8',
366                     'tbr': 155.648,
367                     'width': 320,
368                     'height': 180,
369                 }, {
370                     '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',
371                     'manifest_url': 'http://cdnapi.kaltura.com/p/2082311/sp/208231100/playManifest/protocol/http/entryId/0_89q6e8ku/format/applehttp/tags/mobile_sd/f/a.m3u8',
372                     'ext': 'mp4',
373                     'format_id': '502',
374                     'protocol': 'm3u8',
375                     'tbr': 502.784,
376                     'width': 480,
377                     'height': 270,
378                 }, {
379                     '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',
380                     'manifest_url': 'http://cdnapi.kaltura.com/p/2082311/sp/208231100/playManifest/protocol/http/entryId/0_89q6e8ku/format/applehttp/tags/mobile_sd/f/a.m3u8',
381                     'ext': 'mp4',
382                     'format_id': '827',
383                     'protocol': 'm3u8',
384                     'tbr': 827.392,
385                     'width': 640,
386                     'height': 360,
387                 }, {
388                     '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',
389                     'manifest_url': 'http://cdnapi.kaltura.com/p/2082311/sp/208231100/playManifest/protocol/http/entryId/0_89q6e8ku/format/applehttp/tags/mobile_sd/f/a.m3u8',
390                     'ext': 'mp4',
391                     'format_id': '1396',
392                     'protocol': 'm3u8',
393                     'tbr': 1396.736,
394                     'width': 854,
395                     'height': 480,
396                 }]
397             ),
398             (
399                 # http://www.twitch.tv/riotgames/v/6528877
400                 'twitch_vod',
401                 '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',
402                 [{
403                     'url': 'https://vod.edgecast.hls.ttvnw.net/e5da31ab49_riotgames_15001215120_261543898/audio_only/index-muted-HM49I092CC.m3u8',
404                     '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',
405                     'ext': 'mp4',
406                     'format_id': 'Audio Only',
407                     'protocol': 'm3u8',
408                     'acodec': 'mp4a.40.2',
409                     'vcodec': 'none',
410                     'tbr': 182.725,
411                 }, {
412                     'url': 'https://vod.edgecast.hls.ttvnw.net/e5da31ab49_riotgames_15001215120_261543898/mobile/index-muted-HM49I092CC.m3u8',
413                     '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',
414                     'ext': 'mp4',
415                     'format_id': 'Mobile',
416                     'protocol': 'm3u8',
417                     'acodec': 'mp4a.40.2',
418                     'vcodec': 'avc1.42C00D',
419                     'tbr': 280.474,
420                     'width': 400,
421                     'height': 226,
422                 }, {
423                     'url': 'https://vod.edgecast.hls.ttvnw.net/e5da31ab49_riotgames_15001215120_261543898/low/index-muted-HM49I092CC.m3u8',
424                     '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',
425                     'ext': 'mp4',
426                     'format_id': 'Low',
427                     'protocol': 'm3u8',
428                     'acodec': 'mp4a.40.2',
429                     'vcodec': 'avc1.42C01E',
430                     'tbr': 628.347,
431                     'width': 640,
432                     'height': 360,
433                 }, {
434                     'url': 'https://vod.edgecast.hls.ttvnw.net/e5da31ab49_riotgames_15001215120_261543898/medium/index-muted-HM49I092CC.m3u8',
435                     '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',
436                     'ext': 'mp4',
437                     'format_id': 'Medium',
438                     'protocol': 'm3u8',
439                     'acodec': 'mp4a.40.2',
440                     'vcodec': 'avc1.42C01E',
441                     'tbr': 893.387,
442                     'width': 852,
443                     'height': 480,
444                 }, {
445                     'url': 'https://vod.edgecast.hls.ttvnw.net/e5da31ab49_riotgames_15001215120_261543898/high/index-muted-HM49I092CC.m3u8',
446                     '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',
447                     'ext': 'mp4',
448                     'format_id': 'High',
449                     'protocol': 'm3u8',
450                     'acodec': 'mp4a.40.2',
451                     'vcodec': 'avc1.42C01F',
452                     'tbr': 1603.789,
453                     'width': 1280,
454                     'height': 720,
455                 }, {
456                     'url': 'https://vod.edgecast.hls.ttvnw.net/e5da31ab49_riotgames_15001215120_261543898/chunked/index-muted-HM49I092CC.m3u8',
457                     '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',
458                     'ext': 'mp4',
459                     'format_id': 'Source',
460                     'protocol': 'm3u8',
461                     'acodec': 'mp4a.40.2',
462                     'vcodec': 'avc1.100.31',
463                     'tbr': 3214.134,
464                     'width': 1280,
465                     'height': 720,
466                 }]
467             ),
468             (
469                 # http://www.vidio.com/watch/165683-dj_ambred-booyah-live-2015
470                 # EXT-X-STREAM-INF tag with NAME attribute that is not defined
471                 # in HLS specification
472                 'vidio',
473                 'https://www.vidio.com/videos/165683/playlist.m3u8',
474                 [{
475                     'url': 'https://cdn1-a.production.vidio.static6.com/uploads/165683/dj_ambred-4383-b300.mp4.m3u8',
476                     'manifest_url': 'https://www.vidio.com/videos/165683/playlist.m3u8',
477                     'ext': 'mp4',
478                     'format_id': '270p 3G',
479                     'protocol': 'm3u8',
480                     'tbr': 300,
481                     'width': 480,
482                     'height': 270,
483                 }, {
484                     'url': 'https://cdn1-a.production.vidio.static6.com/uploads/165683/dj_ambred-4383-b600.mp4.m3u8',
485                     'manifest_url': 'https://www.vidio.com/videos/165683/playlist.m3u8',
486                     'ext': 'mp4',
487                     'format_id': '360p SD',
488                     'protocol': 'm3u8',
489                     'tbr': 600,
490                     'width': 640,
491                     'height': 360,
492                 }, {
493                     'url': 'https://cdn1-a.production.vidio.static6.com/uploads/165683/dj_ambred-4383-b1200.mp4.m3u8',
494                     'manifest_url': 'https://www.vidio.com/videos/165683/playlist.m3u8',
495                     'ext': 'mp4',
496                     'format_id': '720p HD',
497                     'protocol': 'm3u8',
498                     'tbr': 1200,
499                     'width': 1280,
500                     'height': 720,
501                 }]
502             ),
503             (
504                 # https://github.com/ytdl-org/youtube-dl/issues/18923
505                 # https://www.ted.com/talks/boris_hesser_a_grassroots_healthcare_revolution_in_africa
506                 'ted_18923',
507                 'http://hls.ted.com/talks/31241.m3u8',
508                 [{
509                     'url': 'http://hls.ted.com/videos/BorisHesser_2018S/audio/600k.m3u8?nobumpers=true&uniqueId=76011e2b',
510                     'format_id': '600k-Audio',
511                     'vcodec': 'none',
512                 }, {
513                     'url': 'http://hls.ted.com/videos/BorisHesser_2018S/audio/600k.m3u8?nobumpers=true&uniqueId=76011e2b',
514                     'format_id': '68',
515                     'vcodec': 'none',
516                 }, {
517                     'url': 'http://hls.ted.com/videos/BorisHesser_2018S/video/64k.m3u8?nobumpers=true&uniqueId=76011e2b',
518                     'format_id': '163',
519                     'acodec': 'none',
520                     'width': 320,
521                     'height': 180,
522                 }, {
523                     'url': 'http://hls.ted.com/videos/BorisHesser_2018S/video/180k.m3u8?nobumpers=true&uniqueId=76011e2b',
524                     'format_id': '481',
525                     'acodec': 'none',
526                     'width': 512,
527                     'height': 288,
528                 }, {
529                     'url': 'http://hls.ted.com/videos/BorisHesser_2018S/video/320k.m3u8?nobumpers=true&uniqueId=76011e2b',
530                     'format_id': '769',
531                     'acodec': 'none',
532                     'width': 512,
533                     'height': 288,
534                 }, {
535                     'url': 'http://hls.ted.com/videos/BorisHesser_2018S/video/450k.m3u8?nobumpers=true&uniqueId=76011e2b',
536                     'format_id': '984',
537                     'acodec': 'none',
538                     'width': 512,
539                     'height': 288,
540                 }, {
541                     'url': 'http://hls.ted.com/videos/BorisHesser_2018S/video/600k.m3u8?nobumpers=true&uniqueId=76011e2b',
542                     'format_id': '1255',
543                     'acodec': 'none',
544                     'width': 640,
545                     'height': 360,
546                 }, {
547                     'url': 'http://hls.ted.com/videos/BorisHesser_2018S/video/950k.m3u8?nobumpers=true&uniqueId=76011e2b',
548                     'format_id': '1693',
549                     'acodec': 'none',
550                     'width': 853,
551                     'height': 480,
552                 }, {
553                     'url': 'http://hls.ted.com/videos/BorisHesser_2018S/video/1500k.m3u8?nobumpers=true&uniqueId=76011e2b',
554                     'format_id': '2462',
555                     'acodec': 'none',
556                     'width': 1280,
557                     'height': 720,
558                 }]
559             ),
560         ]
561
562         for m3u8_file, m3u8_url, expected_formats in _TEST_CASES:
563             with io.open('./test/testdata/m3u8/%s.m3u8' % m3u8_file,
564                          mode='r', encoding='utf-8') as f:
565                 formats = self.ie._parse_m3u8_formats(
566                     f.read(), m3u8_url, ext='mp4')
567                 self.ie._sort_formats(formats)
568                 expect_value(self, formats, expected_formats, None)
569
570     def test_parse_mpd_formats(self):
571         _TEST_CASES = [
572             (
573                 # https://github.com/ytdl-org/youtube-dl/issues/13919
574                 # Also tests duplicate representation ids, see
575                 # https://github.com/ytdl-org/youtube-dl/issues/15111
576                 'float_duration',
577                 'http://unknown/manifest.mpd',
578                 [{
579                     'manifest_url': 'http://unknown/manifest.mpd',
580                     'ext': 'm4a',
581                     'format_id': '318597',
582                     'format_note': 'DASH audio',
583                     'protocol': 'http_dash_segments',
584                     'acodec': 'mp4a.40.2',
585                     'vcodec': 'none',
586                     'tbr': 61.587,
587                 }, {
588                     'manifest_url': 'http://unknown/manifest.mpd',
589                     'ext': 'mp4',
590                     'format_id': '318597',
591                     'format_note': 'DASH video',
592                     'protocol': 'http_dash_segments',
593                     'acodec': 'none',
594                     'vcodec': 'avc1.42001f',
595                     'tbr': 318.597,
596                     'width': 340,
597                     'height': 192,
598                 }, {
599                     'manifest_url': 'http://unknown/manifest.mpd',
600                     'ext': 'mp4',
601                     'format_id': '638590',
602                     'format_note': 'DASH video',
603                     'protocol': 'http_dash_segments',
604                     'acodec': 'none',
605                     'vcodec': 'avc1.42001f',
606                     'tbr': 638.59,
607                     'width': 512,
608                     'height': 288,
609                 }, {
610                     'manifest_url': 'http://unknown/manifest.mpd',
611                     'ext': 'mp4',
612                     'format_id': '1022565',
613                     'format_note': 'DASH video',
614                     'protocol': 'http_dash_segments',
615                     'acodec': 'none',
616                     'vcodec': 'avc1.4d001f',
617                     'tbr': 1022.565,
618                     'width': 688,
619                     'height': 384,
620                 }, {
621                     'manifest_url': 'http://unknown/manifest.mpd',
622                     'ext': 'mp4',
623                     'format_id': '2046506',
624                     'format_note': 'DASH video',
625                     'protocol': 'http_dash_segments',
626                     'acodec': 'none',
627                     'vcodec': 'avc1.4d001f',
628                     'tbr': 2046.506,
629                     'width': 1024,
630                     'height': 576,
631                 }, {
632                     'manifest_url': 'http://unknown/manifest.mpd',
633                     'ext': 'mp4',
634                     'format_id': '3998017',
635                     'format_note': 'DASH video',
636                     'protocol': 'http_dash_segments',
637                     'acodec': 'none',
638                     'vcodec': 'avc1.640029',
639                     'tbr': 3998.017,
640                     'width': 1280,
641                     'height': 720,
642                 }, {
643                     'manifest_url': 'http://unknown/manifest.mpd',
644                     'ext': 'mp4',
645                     'format_id': '5997485',
646                     'format_note': 'DASH video',
647                     'protocol': 'http_dash_segments',
648                     'acodec': 'none',
649                     'vcodec': 'avc1.640032',
650                     'tbr': 5997.485,
651                     'width': 1920,
652                     'height': 1080,
653                 }]
654             ), (
655                 # https://github.com/ytdl-org/youtube-dl/pull/14844
656                 'urls_only',
657                 'http://unknown/manifest.mpd',
658                 [{
659                     'manifest_url': 'http://unknown/manifest.mpd',
660                     'ext': 'mp4',
661                     'format_id': 'h264_aac_144p_m4s',
662                     'format_note': 'DASH video',
663                     'protocol': 'http_dash_segments',
664                     'acodec': 'mp4a.40.2',
665                     'vcodec': 'avc3.42c01e',
666                     'tbr': 200,
667                     'width': 256,
668                     'height': 144,
669                 }, {
670                     'manifest_url': 'http://unknown/manifest.mpd',
671                     'ext': 'mp4',
672                     'format_id': 'h264_aac_240p_m4s',
673                     'format_note': 'DASH video',
674                     'protocol': 'http_dash_segments',
675                     'acodec': 'mp4a.40.2',
676                     'vcodec': 'avc3.42c01e',
677                     'tbr': 400,
678                     'width': 424,
679                     'height': 240,
680                 }, {
681                     'manifest_url': 'http://unknown/manifest.mpd',
682                     'ext': 'mp4',
683                     'format_id': 'h264_aac_360p_m4s',
684                     'format_note': 'DASH video',
685                     'protocol': 'http_dash_segments',
686                     'acodec': 'mp4a.40.2',
687                     'vcodec': 'avc3.42c01e',
688                     'tbr': 800,
689                     'width': 640,
690                     'height': 360,
691                 }, {
692                     'manifest_url': 'http://unknown/manifest.mpd',
693                     'ext': 'mp4',
694                     'format_id': 'h264_aac_480p_m4s',
695                     'format_note': 'DASH video',
696                     'protocol': 'http_dash_segments',
697                     'acodec': 'mp4a.40.2',
698                     'vcodec': 'avc3.42c01e',
699                     'tbr': 1200,
700                     'width': 856,
701                     'height': 480,
702                 }, {
703                     'manifest_url': 'http://unknown/manifest.mpd',
704                     'ext': 'mp4',
705                     'format_id': 'h264_aac_576p_m4s',
706                     'format_note': 'DASH video',
707                     'protocol': 'http_dash_segments',
708                     'acodec': 'mp4a.40.2',
709                     'vcodec': 'avc3.42c01e',
710                     'tbr': 1600,
711                     'width': 1024,
712                     'height': 576,
713                 }, {
714                     'manifest_url': 'http://unknown/manifest.mpd',
715                     'ext': 'mp4',
716                     'format_id': 'h264_aac_720p_m4s',
717                     'format_note': 'DASH video',
718                     'protocol': 'http_dash_segments',
719                     'acodec': 'mp4a.40.2',
720                     'vcodec': 'avc3.42c01e',
721                     'tbr': 2400,
722                     'width': 1280,
723                     'height': 720,
724                 }, {
725                     'manifest_url': 'http://unknown/manifest.mpd',
726                     'ext': 'mp4',
727                     'format_id': 'h264_aac_1080p_m4s',
728                     'format_note': 'DASH video',
729                     'protocol': 'http_dash_segments',
730                     'acodec': 'mp4a.40.2',
731                     'vcodec': 'avc3.42c01e',
732                     'tbr': 4400,
733                     'width': 1920,
734                     'height': 1080,
735                 }]
736             )
737         ]
738
739         for mpd_file, mpd_url, expected_formats in _TEST_CASES:
740             with io.open('./test/testdata/mpd/%s.mpd' % mpd_file,
741                          mode='r', encoding='utf-8') as f:
742                 formats = self.ie._parse_mpd_formats(
743                     compat_etree_fromstring(f.read().encode('utf-8')),
744                     mpd_url=mpd_url)
745                 self.ie._sort_formats(formats)
746                 expect_value(self, formats, expected_formats, None)
747
748     def test_parse_f4m_formats(self):
749         _TEST_CASES = [
750             (
751                 # https://github.com/ytdl-org/youtube-dl/issues/14660
752                 'custom_base_url',
753                 'http://api.new.livestream.com/accounts/6115179/events/6764928/videos/144884262.f4m',
754                 [{
755                     'manifest_url': 'http://api.new.livestream.com/accounts/6115179/events/6764928/videos/144884262.f4m',
756                     'ext': 'flv',
757                     'format_id': '2148',
758                     'protocol': 'f4m',
759                     'tbr': 2148,
760                     'width': 1280,
761                     'height': 720,
762                 }]
763             ),
764         ]
765
766         for f4m_file, f4m_url, expected_formats in _TEST_CASES:
767             with io.open('./test/testdata/f4m/%s.f4m' % f4m_file,
768                          mode='r', encoding='utf-8') as f:
769                 formats = self.ie._parse_f4m_formats(
770                     compat_etree_fromstring(f.read().encode('utf-8')),
771                     f4m_url, None)
772                 self.ie._sort_formats(formats)
773                 expect_value(self, formats, expected_formats, None)
774
775     def test_parse_xspf(self):
776         _TEST_CASES = [
777             (
778                 'foo_xspf',
779                 'https://example.org/src/foo_xspf.xspf',
780                 [{
781                     'id': 'foo_xspf',
782                     'title': 'Pandemonium',
783                     'description': 'Visit http://bigbrother404.bandcamp.com',
784                     'duration': 202.416,
785                     'formats': [{
786                         'manifest_url': 'https://example.org/src/foo_xspf.xspf',
787                         'url': 'https://example.org/src/cd1/track%201.mp3',
788                     }],
789                 }, {
790                     'id': 'foo_xspf',
791                     'title': 'Final Cartridge (Nichico Twelve Remix)',
792                     'description': 'Visit http://bigbrother404.bandcamp.com',
793                     'duration': 255.857,
794                     'formats': [{
795                         'manifest_url': 'https://example.org/src/foo_xspf.xspf',
796                         'url': 'https://example.org/%E3%83%88%E3%83%A9%E3%83%83%E3%82%AF%E3%80%80%EF%BC%92.mp3',
797                     }],
798                 }, {
799                     'id': 'foo_xspf',
800                     'title': 'Rebuilding Nightingale',
801                     'description': 'Visit http://bigbrother404.bandcamp.com',
802                     'duration': 287.915,
803                     'formats': [{
804                         'manifest_url': 'https://example.org/src/foo_xspf.xspf',
805                         'url': 'https://example.org/src/track3.mp3',
806                     }, {
807                         'manifest_url': 'https://example.org/src/foo_xspf.xspf',
808                         'url': 'https://example.com/track3.mp3',
809                     }]
810                 }]
811             ),
812         ]
813
814         for xspf_file, xspf_url, expected_entries in _TEST_CASES:
815             with io.open('./test/testdata/xspf/%s.xspf' % xspf_file,
816                          mode='r', encoding='utf-8') as f:
817                 entries = self.ie._parse_xspf(
818                     compat_etree_fromstring(f.read().encode('utf-8')),
819                     xspf_file, xspf_url=xspf_url, xspf_base_url=xspf_url)
820                 expect_value(self, entries, expected_entries, None)
821                 for i in range(len(entries)):
822                     expect_dict(self, entries[i], expected_entries[i])
823
824     def test_response_with_expected_status_returns_content(self):
825         # Checks for mitigations against the effects of
826         # <https://bugs.python.org/issue15002> that affect Python 3.4.1+, which
827         # manifest as `_download_webpage`, `_download_xml`, `_download_json`,
828         # or the underlying `_download_webpage_handle` returning no content
829         # when a response matches `expected_status`.
830
831         httpd = compat_http_server.HTTPServer(
832             ('127.0.0.1', 0), InfoExtractorTestRequestHandler)
833         port = http_server_port(httpd)
834         server_thread = threading.Thread(target=httpd.serve_forever)
835         server_thread.daemon = True
836         server_thread.start()
837
838         (content, urlh) = self.ie._download_webpage_handle(
839             'http://127.0.0.1:%d/teapot' % port, None,
840             expected_status=TEAPOT_RESPONSE_STATUS)
841         self.assertEqual(content, TEAPOT_RESPONSE_BODY)
842
843
844 if __name__ == '__main__':
845     unittest.main()