You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

180 lines
9.8 KiB

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