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.

102 lines
4.0 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):
  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_course(self):
  64. dl = FakeDownloader()
  65. ie = YoutubePlaylistIE(dl)
  66. # TODO find a > 100 (paginating?) videos course
  67. result = ie.extract('https://www.youtube.com/course?list=ECUl4u3cNGP61MdtwGTqZA0MreSaDybji8')[0]
  68. entries = result['entries']
  69. self.assertEqual(YoutubeIE()._extract_id(entries[0]['url']), 'j9WZyLZCBzs')
  70. self.assertEqual(len(entries), 25)
  71. self.assertEqual(YoutubeIE()._extract_id(entries[-1]['url']), 'rYefUsYuEp0')
  72. def test_youtube_channel(self):
  73. dl = FakeDownloader()
  74. ie = YoutubeChannelIE(dl)
  75. #test paginated channel
  76. result = ie.extract('https://www.youtube.com/channel/UCKfVa3S1e4PHvxWcwyMMg8w')[0]
  77. self.assertTrue(len(result['entries']) > 90)
  78. #test autogenerated channel
  79. result = ie.extract('https://www.youtube.com/channel/HCtnHdj3df7iM/videos')[0]
  80. self.assertTrue(len(result['entries']) > 20)
  81. def test_youtube_user(self):
  82. dl = FakeDownloader()
  83. ie = YoutubeUserIE(dl)
  84. result = ie.extract('https://www.youtube.com/user/TheLinuxFoundation')[0]
  85. self.assertTrue(len(result['entries']) >= 320)
  86. if __name__ == '__main__':
  87. unittest.main()