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.

114 lines
4.5 KiB

12 years ago
12 years ago
12 years ago
  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
  9. from youtube_dl.utils import *
  10. from youtube_dl.FileDownloader import FileDownloader
  11. PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
  12. with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
  13. parameters = json.load(pf)
  14. # General configuration (from __init__, not very elegant...)
  15. jar = compat_cookiejar.CookieJar()
  16. cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
  17. proxy_handler = compat_urllib_request.ProxyHandler()
  18. opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
  19. compat_urllib_request.install_opener(opener)
  20. class FakeDownloader(FileDownloader):
  21. def __init__(self):
  22. self.result = []
  23. self.params = parameters
  24. def to_screen(self, s):
  25. print(s)
  26. def trouble(self, s, tb=None):
  27. raise Exception(s)
  28. def extract_info(self, url):
  29. self.result.append(url)
  30. return url
  31. class TestYoutubeLists(unittest.TestCase):
  32. def assertIsPlaylist(self,info):
  33. """Make sure the info has '_type' set to 'playlist'"""
  34. self.assertEqual(info['_type'], 'playlist')
  35. def test_youtube_playlist(self):
  36. dl = FakeDownloader()
  37. ie = YoutubePlaylistIE(dl)
  38. result = ie.extract('https://www.youtube.com/playlist?list=PLwiyx1dc3P2JR9N8gQaQN_BCvlSlap7re')[0]
  39. self.assertIsPlaylist(result)
  40. self.assertEqual(result['title'], 'ytdl test PL')
  41. ytie_results = [YoutubeIE()._extract_id(url['url']) for url in result['entries']]
  42. self.assertEqual(ytie_results, [ 'bV9L5Ht9LgY', 'FXxLjLQi3Fg', 'tU3Bgo5qJZE'])
  43. def test_issue_673(self):
  44. dl = FakeDownloader()
  45. ie = YoutubePlaylistIE(dl)
  46. result = ie.extract('PLBB231211A4F62143')[0]
  47. self.assertTrue(len(result['entries']) > 25)
  48. def test_youtube_playlist_long(self):
  49. dl = FakeDownloader()
  50. ie = YoutubePlaylistIE(dl)
  51. result = ie.extract('https://www.youtube.com/playlist?list=UUBABnxM4Ar9ten8Mdjj1j0Q')[0]
  52. self.assertIsPlaylist(result)
  53. self.assertTrue(len(result['entries']) >= 799)
  54. def test_youtube_playlist_with_deleted(self):
  55. #651
  56. dl = FakeDownloader()
  57. ie = YoutubePlaylistIE(dl)
  58. result = ie.extract('https://www.youtube.com/playlist?list=PLwP_SiAcdui0KVebT0mU9Apz359a4ubsC')[0]
  59. ytie_results = [YoutubeIE()._extract_id(url['url']) for url in result['entries']]
  60. self.assertFalse('pElCt5oNDuI' in ytie_results)
  61. self.assertFalse('KdPEApIVdWM' in ytie_results)
  62. def test_youtube_playlist_empty(self):
  63. dl = FakeDownloader()
  64. ie = YoutubePlaylistIE(dl)
  65. result = ie.extract('https://www.youtube.com/playlist?list=PLtPgu7CB4gbZDA7i_euNxn75ISqxwZPYx')[0]
  66. self.assertIsPlaylist(result)
  67. self.assertEqual(len(result['entries']), 0)
  68. def test_youtube_course(self):
  69. dl = FakeDownloader()
  70. ie = YoutubePlaylistIE(dl)
  71. # TODO find a > 100 (paginating?) videos course
  72. result = ie.extract('https://www.youtube.com/course?list=ECUl4u3cNGP61MdtwGTqZA0MreSaDybji8')[0]
  73. entries = result['entries']
  74. self.assertEqual(YoutubeIE()._extract_id(entries[0]['url']), 'j9WZyLZCBzs')
  75. self.assertEqual(len(entries), 25)
  76. self.assertEqual(YoutubeIE()._extract_id(entries[-1]['url']), 'rYefUsYuEp0')
  77. def test_youtube_channel(self):
  78. dl = FakeDownloader()
  79. ie = YoutubeChannelIE(dl)
  80. #test paginated channel
  81. result = ie.extract('https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w')[0]
  82. self.assertTrue(len(result['entries']) > 90)
  83. #test autogenerated channel
  84. result = ie.extract('https://www.youtube.com/channel/HCtnHdj3df7iM/videos')[0]
  85. self.assertTrue(len(result['entries']) >= 18)
  86. def test_youtube_user(self):
  87. dl = FakeDownloader()
  88. ie = YoutubeUserIE(dl)
  89. result = ie.extract('https://www.youtube.com/user/TheLinuxFoundation')[0]
  90. self.assertTrue(len(result['entries']) >= 320)
  91. def test_youtube_safe_search(self):
  92. dl = FakeDownloader()
  93. ie = YoutubePlaylistIE(dl)
  94. result = ie.extract('PLtPgu7CB4gbY9oDN3drwC3cMbJggS7dKl')[0]
  95. self.assertEqual(len(result['entries']), 2)
  96. if __name__ == '__main__':
  97. unittest.main()