test_youtube_lists.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python3
  2. # Allow direct execution
  3. import os
  4. import sys
  5. import unittest
  6. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  7. from test.helper import FakeYDL, is_download_test
  8. from yt_dlp.extractor import YoutubeIE, YoutubeTabIE
  9. from yt_dlp.utils import ExtractorError
  10. @is_download_test
  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_noplaylist(self):
  16. dl = FakeYDL()
  17. dl.params['noplaylist'] = True
  18. ie = YoutubeTabIE(dl)
  19. result = ie.extract('https://www.youtube.com/watch?v=OmJ-4B-mS-Y&list=PLydZ2Hrp_gPRJViZjLFKaBMgCQOYEEkyp&index=2')
  20. self.assertEqual(result['_type'], 'url')
  21. self.assertEqual(result['ie_key'], YoutubeIE.ie_key())
  22. self.assertEqual(YoutubeIE.extract_id(result['url']), 'OmJ-4B-mS-Y')
  23. def test_youtube_mix(self):
  24. dl = FakeYDL()
  25. ie = YoutubeTabIE(dl)
  26. result = ie.extract('https://www.youtube.com/watch?v=tyITL_exICo&list=RDCLAK5uy_kLWIr9gv1XLlPbaDS965-Db4TrBoUTxQ8')
  27. entries = list(result['entries'])
  28. self.assertTrue(len(entries) >= 50)
  29. original_video = entries[0]
  30. self.assertEqual(original_video['id'], 'tyITL_exICo')
  31. def test_youtube_flat_playlist_extraction(self):
  32. dl = FakeYDL()
  33. dl.params['extract_flat'] = True
  34. ie = YoutubeTabIE(dl)
  35. result = ie.extract('https://www.youtube.com/playlist?list=PL4lCao7KL_QFVb7Iudeipvc2BCavECqzc')
  36. self.assertIsPlaylist(result)
  37. entries = list(result['entries'])
  38. self.assertTrue(len(entries) == 1)
  39. video = entries[0]
  40. self.assertEqual(video['_type'], 'url')
  41. self.assertEqual(video['ie_key'], 'Youtube')
  42. self.assertEqual(video['id'], 'BaW_jenozKc')
  43. self.assertEqual(video['url'], 'https://www.youtube.com/watch?v=BaW_jenozKc')
  44. self.assertEqual(video['title'], 'youtube-dl test video "\'/\\ä↭𝕐')
  45. self.assertEqual(video['duration'], 10)
  46. self.assertEqual(video['uploader'], 'Philipp Hagemeister')
  47. def test_youtube_channel_no_uploads(self):
  48. dl = FakeYDL()
  49. dl.params['extract_flat'] = True
  50. ie = YoutubeTabIE(dl)
  51. # no uploads
  52. with self.assertRaisesRegex(ExtractorError, r'no uploads'):
  53. ie.extract('https://www.youtube.com/channel/UC2yXPzFejc422buOIzn_0CA')
  54. # no uploads and no UCID given
  55. with self.assertRaisesRegex(ExtractorError, r'no uploads'):
  56. ie.extract('https://www.youtube.com/news')
  57. if __name__ == '__main__':
  58. unittest.main()