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.

88 lines
4.1 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
  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. if __name__ == '__main__':
  74. unittest.main()