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.

109 lines
4.3 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.InfoExtractors 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.assertEqual(result['title'], 'Team Fortress 2')
  48. self.assertTrue(len(result['entries']) > 40)
  49. def test_youtube_playlist_long(self):
  50. dl = FakeDownloader()
  51. ie = YoutubePlaylistIE(dl)
  52. result = ie.extract('https://www.youtube.com/playlist?list=UUBABnxM4Ar9ten8Mdjj1j0Q')[0]
  53. self.assertIsPlaylist(result)
  54. self.assertTrue(len(result['entries']) >= 799)
  55. def test_youtube_playlist_with_deleted(self):
  56. #651
  57. dl = FakeDownloader()
  58. ie = YoutubePlaylistIE(dl)
  59. result = ie.extract('https://www.youtube.com/playlist?list=PLwP_SiAcdui0KVebT0mU9Apz359a4ubsC')[0]
  60. ytie_results = [YoutubeIE()._extract_id(url['url']) for url in result['entries']]
  61. self.assertFalse('pElCt5oNDuI' in ytie_results)
  62. self.assertFalse('KdPEApIVdWM' in ytie_results)
  63. def test_youtube_playlist_empty(self):
  64. dl = FakeDownloader()
  65. ie = YoutubePlaylistIE(dl)
  66. result = ie.extract('https://www.youtube.com/playlist?list=PLtPgu7CB4gbZDA7i_euNxn75ISqxwZPYx')[0]
  67. self.assertIsPlaylist(result)
  68. self.assertEqual(len(result['entries']), 0)
  69. def test_youtube_course(self):
  70. dl = FakeDownloader()
  71. ie = YoutubePlaylistIE(dl)
  72. # TODO find a > 100 (paginating?) videos course
  73. result = ie.extract('https://www.youtube.com/course?list=ECUl4u3cNGP61MdtwGTqZA0MreSaDybji8')[0]
  74. entries = result['entries']
  75. self.assertEqual(YoutubeIE()._extract_id(entries[0]['url']), 'j9WZyLZCBzs')
  76. self.assertEqual(len(entries), 25)
  77. self.assertEqual(YoutubeIE()._extract_id(entries[-1]['url']), 'rYefUsYuEp0')
  78. def test_youtube_channel(self):
  79. dl = FakeDownloader()
  80. ie = YoutubeChannelIE(dl)
  81. #test paginated channel
  82. result = ie.extract('https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w')[0]
  83. self.assertTrue(len(result['entries']) > 90)
  84. #test autogenerated channel
  85. result = ie.extract('https://www.youtube.com/channel/HCtnHdj3df7iM/videos')[0]
  86. self.assertTrue(len(result['entries']) >= 18)
  87. def test_youtube_user(self):
  88. dl = FakeDownloader()
  89. ie = YoutubeUserIE(dl)
  90. result = ie.extract('https://www.youtube.com/user/TheLinuxFoundation')[0]
  91. self.assertTrue(len(result['entries']) >= 320)
  92. if __name__ == '__main__':
  93. unittest.main()