test_youtube_lists.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #!/usr/bin/env python
  2. import sys
  3. import unittest
  4. import json
  5. # Allow direct execution
  6. import os
  7. sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  8. from youtube_dl.extractor import YoutubeUserIE, YoutubePlaylistIE, YoutubeIE, YoutubeChannelIE, YoutubeShowIE
  9. from youtube_dl.utils import *
  10. from helper import FakeYDL
  11. class TestYoutubeLists(unittest.TestCase):
  12. def assertIsPlaylist(self,info):
  13. """Make sure the info has '_type' set to 'playlist'"""
  14. self.assertEqual(info['_type'], 'playlist')
  15. def test_youtube_playlist(self):
  16. dl = FakeYDL()
  17. ie = YoutubePlaylistIE(dl)
  18. result = ie.extract('https://www.youtube.com/playlist?list=PLwiyx1dc3P2JR9N8gQaQN_BCvlSlap7re')[0]
  19. self.assertIsPlaylist(result)
  20. self.assertEqual(result['title'], 'ytdl test PL')
  21. ytie_results = [YoutubeIE()._extract_id(url['url']) for url in result['entries']]
  22. self.assertEqual(ytie_results, [ 'bV9L5Ht9LgY', 'FXxLjLQi3Fg', 'tU3Bgo5qJZE'])
  23. def test_youtube_playlist_noplaylist(self):
  24. dl = FakeYDL()
  25. dl.params['noplaylist'] = True
  26. ie = YoutubePlaylistIE(dl)
  27. result = ie.extract('https://www.youtube.com/watch?v=FXxLjLQi3Fg&list=PLwiyx1dc3P2JR9N8gQaQN_BCvlSlap7re')
  28. self.assertEqual(result['_type'], 'url')
  29. def test_issue_673(self):
  30. dl = FakeYDL()
  31. ie = YoutubePlaylistIE(dl)
  32. result = ie.extract('PLBB231211A4F62143')[0]
  33. self.assertTrue(len(result['entries']) > 25)
  34. def test_youtube_playlist_long(self):
  35. dl = FakeYDL()
  36. ie = YoutubePlaylistIE(dl)
  37. result = ie.extract('https://www.youtube.com/playlist?list=UUBABnxM4Ar9ten8Mdjj1j0Q')[0]
  38. self.assertIsPlaylist(result)
  39. self.assertTrue(len(result['entries']) >= 799)
  40. def test_youtube_playlist_with_deleted(self):
  41. #651
  42. dl = FakeYDL()
  43. ie = YoutubePlaylistIE(dl)
  44. result = ie.extract('https://www.youtube.com/playlist?list=PLwP_SiAcdui0KVebT0mU9Apz359a4ubsC')[0]
  45. ytie_results = [YoutubeIE()._extract_id(url['url']) for url in result['entries']]
  46. self.assertFalse('pElCt5oNDuI' in ytie_results)
  47. self.assertFalse('KdPEApIVdWM' in ytie_results)
  48. def test_youtube_playlist_empty(self):
  49. dl = FakeYDL()
  50. ie = YoutubePlaylistIE(dl)
  51. result = ie.extract('https://www.youtube.com/playlist?list=PLtPgu7CB4gbZDA7i_euNxn75ISqxwZPYx')[0]
  52. self.assertIsPlaylist(result)
  53. self.assertEqual(len(result['entries']), 0)
  54. def test_youtube_course(self):
  55. dl = FakeYDL()
  56. ie = YoutubePlaylistIE(dl)
  57. # TODO find a > 100 (paginating?) videos course
  58. result = ie.extract('https://www.youtube.com/course?list=ECUl4u3cNGP61MdtwGTqZA0MreSaDybji8')[0]
  59. entries = result['entries']
  60. self.assertEqual(YoutubeIE()._extract_id(entries[0]['url']), 'j9WZyLZCBzs')
  61. self.assertEqual(len(entries), 25)
  62. self.assertEqual(YoutubeIE()._extract_id(entries[-1]['url']), 'rYefUsYuEp0')
  63. def test_youtube_channel(self):
  64. dl = FakeYDL()
  65. ie = YoutubeChannelIE(dl)
  66. #test paginated channel
  67. result = ie.extract('https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w')[0]
  68. self.assertTrue(len(result['entries']) > 90)
  69. #test autogenerated channel
  70. result = ie.extract('https://www.youtube.com/channel/HCtnHdj3df7iM/videos')[0]
  71. self.assertTrue(len(result['entries']) >= 18)
  72. def test_youtube_user(self):
  73. dl = FakeYDL()
  74. ie = YoutubeUserIE(dl)
  75. result = ie.extract('https://www.youtube.com/user/TheLinuxFoundation')[0]
  76. self.assertTrue(len(result['entries']) >= 320)
  77. def test_youtube_safe_search(self):
  78. dl = FakeYDL()
  79. ie = YoutubePlaylistIE(dl)
  80. result = ie.extract('PLtPgu7CB4gbY9oDN3drwC3cMbJggS7dKl')[0]
  81. self.assertEqual(len(result['entries']), 2)
  82. def test_youtube_show(self):
  83. dl = FakeYDL()
  84. ie = YoutubeShowIE(dl)
  85. result = ie.extract('http://www.youtube.com/show/airdisasters')
  86. self.assertTrue(len(result) >= 4)
  87. if __name__ == '__main__':
  88. unittest.main()