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.

94 lines
3.6 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
  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. ytie_results = [YoutubeIE()._extract_id(url['url']) for url in result['entries']]
  41. self.assertEqual(ytie_results, [ 'bV9L5Ht9LgY', 'FXxLjLQi3Fg', 'tU3Bgo5qJZE'])
  42. def test_issue_673(self):
  43. dl = FakeDownloader()
  44. ie = YoutubePlaylistIE(dl)
  45. result = ie.extract('PLBB231211A4F62143')[0]
  46. self.assertTrue(len(result['entries']) > 40)
  47. def test_youtube_playlist_long(self):
  48. dl = FakeDownloader()
  49. ie = YoutubePlaylistIE(dl)
  50. result = ie.extract('https://www.youtube.com/playlist?list=UUBABnxM4Ar9ten8Mdjj1j0Q')[0]
  51. self.assertIsPlaylist(result)
  52. self.assertTrue(len(result['entries']) >= 799)
  53. def test_youtube_playlist_with_deleted(self):
  54. #651
  55. dl = FakeDownloader()
  56. ie = YoutubePlaylistIE(dl)
  57. result = ie.extract('https://www.youtube.com/playlist?list=PLwP_SiAcdui0KVebT0mU9Apz359a4ubsC')[0]
  58. ytie_results = [YoutubeIE()._extract_id(url['url']) for url in result['entries']]
  59. self.assertFalse('pElCt5oNDuI' in ytie_results)
  60. self.assertFalse('KdPEApIVdWM' in ytie_results)
  61. def test_youtube_course(self):
  62. dl = FakeDownloader()
  63. ie = YoutubePlaylistIE(dl)
  64. # TODO find a > 100 (paginating?) videos course
  65. result = ie.extract('https://www.youtube.com/course?list=ECUl4u3cNGP61MdtwGTqZA0MreSaDybji8')[0]
  66. entries = result['entries']
  67. self.assertEqual(YoutubeIE()._extract_id(entries[0]['url']), 'j9WZyLZCBzs')
  68. self.assertEqual(len(entries), 25)
  69. self.assertEqual(YoutubeIE()._extract_id(entries[-1]['url']), 'rYefUsYuEp0')
  70. def test_youtube_channel(self):
  71. # I give up, please find a channel that does paginate and test this like test_youtube_playlist_long
  72. pass # TODO
  73. def test_youtube_user(self):
  74. dl = FakeDownloader()
  75. ie = YoutubeUserIE(dl)
  76. result = ie.extract('https://www.youtube.com/user/TheLinuxFoundation')[0]
  77. self.assertTrue(len(result['entries']) >= 320)
  78. if __name__ == '__main__':
  79. unittest.main()