test_youtube_lists.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.InfoExtractors import YoutubeUserIE, YoutubePlaylistIE, YoutubeIE
  9. from youtube_dl.utils import *
  10. PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
  11. with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
  12. parameters = json.load(pf)
  13. # General configuration (from __init__, not very elegant...)
  14. jar = compat_cookiejar.CookieJar()
  15. cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
  16. proxy_handler = compat_urllib_request.ProxyHandler()
  17. opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
  18. compat_urllib_request.install_opener(opener)
  19. class FakeDownloader(object):
  20. def __init__(self):
  21. self.result = []
  22. self.params = parameters
  23. def to_screen(self, s):
  24. print(s)
  25. def trouble(self, s):
  26. raise Exception(s)
  27. def download(self, x):
  28. self.result.append(x)
  29. class TestYoutubeLists(unittest.TestCase):
  30. def test_youtube_playlist(self):
  31. DL = FakeDownloader()
  32. IE = YoutubePlaylistIE(DL)
  33. IE.extract('https://www.youtube.com/playlist?list=PLwiyx1dc3P2JR9N8gQaQN_BCvlSlap7re')
  34. self.assertEqual(map(lambda x: YoutubeIE()._extract_id(x[0]), DL.result),
  35. [ 'bV9L5Ht9LgY', 'FXxLjLQi3Fg', 'tU3Bgo5qJZE' ])
  36. #661
  37. DL = FakeDownloader()
  38. IE = YoutubePlaylistIE(DL)
  39. IE.extract('PLMCmkNmxw6Z9eduM7BZjSEh7HiU543Ig0')
  40. self.assertTrue(len(DL.result) > 20)
  41. #673
  42. DL = FakeDownloader()
  43. IE = YoutubePlaylistIE(DL)
  44. IE.extract('PLBB231211A4F62143')
  45. self.assertTrue(len(DL.result) > 40)
  46. def test_youtube_playlist_long(self):
  47. DL = FakeDownloader()
  48. IE = YoutubePlaylistIE(DL)
  49. IE.extract('https://www.youtube.com/playlist?list=UUBABnxM4Ar9ten8Mdjj1j0Q')
  50. self.assertTrue(len(DL.result) >= 799)
  51. def test_youtube_playlist_with_deleted(self):
  52. #651
  53. DL = FakeDownloader()
  54. IE = YoutubePlaylistIE(DL)
  55. IE.extract('https://www.youtube.com/playlist?list=PLwP_SiAcdui0KVebT0mU9Apz359a4ubsC')
  56. self.assertFalse('pElCt5oNDuI' in map(lambda x: YoutubeIE()._extract_id(x[0]), DL.result))
  57. self.assertFalse('KdPEApIVdWM' in map(lambda x: YoutubeIE()._extract_id(x[0]), DL.result))
  58. def test_youtube_course(self):
  59. DL = FakeDownloader()
  60. IE = YoutubePlaylistIE(DL)
  61. # TODO find a > 100 (paginating?) videos course
  62. IE.extract('https://www.youtube.com/course?list=ECUl4u3cNGP61MdtwGTqZA0MreSaDybji8')
  63. self.assertEqual(YoutubeIE()._extract_id(DL.result[0][0]), 'j9WZyLZCBzs')
  64. self.assertEqual(len(DL.result), 25)
  65. self.assertEqual(YoutubeIE()._extract_id(DL.result[-1][0]), 'rYefUsYuEp0')
  66. def test_youtube_channel(self):
  67. # I give up, please find a channel that does paginate and test this like test_youtube_playlist_long
  68. pass # TODO
  69. def test_youtube_user(self):
  70. DL = FakeDownloader()
  71. IE = YoutubeUserIE(DL)
  72. IE.extract('https://www.youtube.com/user/TheLinuxFoundation')
  73. self.assertTrue(len(DL.result) >= 320)
  74. if __name__ == '__main__':
  75. unittest.main()