]> Raphaƫl G. Git Repositories - youtubedl/blob - test/test_InfoExtractor.py
New upstream version 2017.03.26
[youtubedl] / test / test_InfoExtractor.py
1 #!/usr/bin/env python
2
3 from __future__ import unicode_literals
4
5 # Allow direct execution
6 import os
7 import sys
8 import unittest
9 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
10
11 from test.helper import FakeYDL, expect_dict
12 from youtube_dl.extractor.common import InfoExtractor
13 from youtube_dl.extractor import YoutubeIE, get_info_extractor
14 from youtube_dl.utils import encode_data_uri, strip_jsonp, ExtractorError, RegexNotFoundError
15
16
17 class TestIE(InfoExtractor):
18 pass
19
20
21 class TestInfoExtractor(unittest.TestCase):
22 def setUp(self):
23 self.ie = TestIE(FakeYDL())
24
25 def test_ie_key(self):
26 self.assertEqual(get_info_extractor(YoutubeIE.ie_key()), YoutubeIE)
27
28 def test_html_search_regex(self):
29 html = '<p id="foo">Watch this <a href="http://www.youtube.com/watch?v=BaW_jenozKc">video</a></p>'
30 search = lambda re, *args: self.ie._html_search_regex(re, html, *args)
31 self.assertEqual(search(r'<p id="foo">(.+?)</p>', 'foo'), 'Watch this video')
32
33 def test_opengraph(self):
34 ie = self.ie
35 html = '''
36 <meta name="og:title" content='Foo'/>
37 <meta content="Some video's description " name="og:description"/>
38 <meta property='og:image' content='http://domain.com/pic.jpg?key1=val1&amp;key2=val2'/>
39 <meta content='application/x-shockwave-flash' property='og:video:type'>
40 <meta content='Foo' property=og:foobar>
41 <meta name="og:test1" content='foo > < bar'/>
42 <meta name="og:test2" content="foo >//< bar"/>
43 '''
44 self.assertEqual(ie._og_search_title(html), 'Foo')
45 self.assertEqual(ie._og_search_description(html), 'Some video\'s description ')
46 self.assertEqual(ie._og_search_thumbnail(html), 'http://domain.com/pic.jpg?key1=val1&key2=val2')
47 self.assertEqual(ie._og_search_video_url(html, default=None), None)
48 self.assertEqual(ie._og_search_property('foobar', html), 'Foo')
49 self.assertEqual(ie._og_search_property('test1', html), 'foo > < bar')
50 self.assertEqual(ie._og_search_property('test2', html), 'foo >//< bar')
51 self.assertEqual(ie._og_search_property(('test0', 'test1'), html), 'foo > < bar')
52 self.assertRaises(RegexNotFoundError, ie._og_search_property, 'test0', html, None, fatal=True)
53 self.assertRaises(RegexNotFoundError, ie._og_search_property, ('test0', 'test00'), html, None, fatal=True)
54
55 def test_html_search_meta(self):
56 ie = self.ie
57 html = '''
58 <meta name="a" content="1" />
59 <meta name='b' content='2'>
60 <meta name="c" content='3'>
61 <meta name=d content='4'>
62 <meta property="e" content='5' >
63 <meta content="6" name="f">
64 '''
65
66 self.assertEqual(ie._html_search_meta('a', html), '1')
67 self.assertEqual(ie._html_search_meta('b', html), '2')
68 self.assertEqual(ie._html_search_meta('c', html), '3')
69 self.assertEqual(ie._html_search_meta('d', html), '4')
70 self.assertEqual(ie._html_search_meta('e', html), '5')
71 self.assertEqual(ie._html_search_meta('f', html), '6')
72 self.assertEqual(ie._html_search_meta(('a', 'b', 'c'), html), '1')
73 self.assertEqual(ie._html_search_meta(('c', 'b', 'a'), html), '3')
74 self.assertEqual(ie._html_search_meta(('z', 'x', 'c'), html), '3')
75 self.assertRaises(RegexNotFoundError, ie._html_search_meta, 'z', html, None, fatal=True)
76 self.assertRaises(RegexNotFoundError, ie._html_search_meta, ('z', 'x'), html, None, fatal=True)
77
78 def test_download_json(self):
79 uri = encode_data_uri(b'{"foo": "blah"}', 'application/json')
80 self.assertEqual(self.ie._download_json(uri, None), {'foo': 'blah'})
81 uri = encode_data_uri(b'callback({"foo": "blah"})', 'application/javascript')
82 self.assertEqual(self.ie._download_json(uri, None, transform_source=strip_jsonp), {'foo': 'blah'})
83 uri = encode_data_uri(b'{"foo": invalid}', 'application/json')
84 self.assertRaises(ExtractorError, self.ie._download_json, uri, None)
85 self.assertEqual(self.ie._download_json(uri, None, fatal=False), None)
86
87 def test_extract_jwplayer_data_realworld(self):
88 # from http://www.suffolk.edu/sjc/
89 expect_dict(
90 self,
91 self.ie._extract_jwplayer_data(r'''
92 <script type='text/javascript'>
93 jwplayer('my-video').setup({
94 file: 'rtmp://192.138.214.154/live/sjclive',
95 fallback: 'true',
96 width: '95%',
97 aspectratio: '16:9',
98 primary: 'flash',
99 mediaid:'XEgvuql4'
100 });
101 </script>
102 ''', None, require_title=False),
103 {
104 'id': 'XEgvuql4',
105 'formats': [{
106 'url': 'rtmp://192.138.214.154/live/sjclive',
107 'ext': 'flv'
108 }]
109 })
110
111 # from https://www.pornoxo.com/videos/7564/striptease-from-sexy-secretary/
112 expect_dict(
113 self,
114 self.ie._extract_jwplayer_data(r'''
115 <script type="text/javascript">
116 jwplayer("mediaplayer").setup({
117 'videoid': "7564",
118 'width': "100%",
119 'aspectratio': "16:9",
120 'stretching': "exactfit",
121 'autostart': 'false',
122 'flashplayer': "https://t04.vipstreamservice.com/jwplayer/v5.10/player.swf",
123 '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",
124 'image': "https://t03.vipstreamservice.com/thumbs/pxo-full/2009-12/14/a4b2157147afe5efa93ce1978e0265289c193874e02597.flv-full-13.jpg",
125 '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",
126 'logo.hide': true,
127 'skin': "https://t04.vipstreamservice.com/jwplayer/skin/modieus-blk.zip",
128 'plugins': "https://t04.vipstreamservice.com/jwplayer/dock/dockableskinnableplugin.swf",
129 'dockableskinnableplugin.piclink': "/index.php?key=ajax-videothumbsn&vid=7564&data=2009-12--14--4b2157147afe5efa93ce1978e0265289c193874e02597.flv--17370",
130 'controlbar': 'bottom',
131 'modes': [
132 {type: 'flash', src: 'https://t04.vipstreamservice.com/jwplayer/v5.10/player.swf'}
133 ],
134 'provider': 'http'
135 });
136 //noinspection JSAnnotator
137 invideo.setup({
138 adsUrl: "/banner-iframe/?zoneId=32",
139 adsUrl2: "",
140 autostart: false
141 });
142 </script>
143 ''', 'dummy', require_title=False),
144 {
145 'thumbnail': 'https://t03.vipstreamservice.com/thumbs/pxo-full/2009-12/14/a4b2157147afe5efa93ce1978e0265289c193874e02597.flv-full-13.jpg',
146 'formats': [{
147 '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',
148 'ext': 'flv'
149 }]
150 })
151
152 # from http://www.indiedb.com/games/king-machine/videos
153 expect_dict(
154 self,
155 self.ie._extract_jwplayer_data(r'''
156 <script>
157 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) {
158 videoAnalytics("play");
159 }).once("complete", function(event) {
160 videoAnalytics("completed");
161 });
162 </script>
163 ''', 'dummy'),
164 {
165 'title': 'king machine trailer 1',
166 'thumbnail': 'http://media.indiedb.com/cache/images/games/1/50/49678/thumb_620x2000/king-machine-trailer.mp4.jpg',
167 'formats': [{
168 'url': 'http://cdn.dbolical.com/cache/videos/games/1/50/49678/encode_mp4/king-machine-trailer.mp4',
169 'height': 360,
170 'ext': 'mp4'
171 }, {
172 'url': 'http://cdn.dbolical.com/cache/videos/games/1/50/49678/encode720p_mp4/king-machine-trailer.mp4',
173 'height': 720,
174 'ext': 'mp4'
175 }]
176 })
177
178
179 if __name__ == '__main__':
180 unittest.main()