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.

75 lines
2.7 KiB

12 years ago
12 years ago
12 years ago
  1. #!/usr/bin/env python
  2. import sys
  3. import unittest
  4. import socket
  5. import json
  6. # Allow direct execution
  7. import os
  8. sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  9. from youtube_dl.InfoExtractors import YoutubeUserIE,YoutubePlaylistIE
  10. from youtube_dl.utils import *
  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. socket.setdefaulttimeout(300) # 5 minutes should be enough (famous last words)
  21. class FakeDownloader(object):
  22. def __init__(self):
  23. self.result = []
  24. self.params = parameters
  25. def to_screen(self, s):
  26. print(s)
  27. def trouble(self, s):
  28. raise Exception(s)
  29. def download(self, x):
  30. self.result.append(x)
  31. class TestYoutubeLists(unittest.TestCase):
  32. def test_youtube_playlist(self):
  33. DL = FakeDownloader()
  34. IE = YoutubePlaylistIE(DL)
  35. IE.extract('https://www.youtube.com/playlist?list=PLwiyx1dc3P2JR9N8gQaQN_BCvlSlap7re')
  36. self.assertEqual(DL.result, [
  37. ['http://www.youtube.com/watch?v=bV9L5Ht9LgY'],
  38. ['http://www.youtube.com/watch?v=FXxLjLQi3Fg'],
  39. ['http://www.youtube.com/watch?v=tU3Bgo5qJZE']
  40. ])
  41. def test_youtube_playlist_long(self):
  42. DL = FakeDownloader()
  43. IE = YoutubePlaylistIE(DL)
  44. IE.extract('https://www.youtube.com/playlist?list=UUBABnxM4Ar9ten8Mdjj1j0Q')
  45. self.assertTrue(len(DL.result) >= 799)
  46. def test_youtube_course(self):
  47. DL = FakeDownloader()
  48. IE = YoutubePlaylistIE(DL)
  49. # TODO find a > 100 (paginating?) videos course
  50. IE.extract('https://www.youtube.com/course?list=ECUl4u3cNGP61MdtwGTqZA0MreSaDybji8')
  51. self.assertEqual(DL.result[0], ['http://www.youtube.com/watch?v=j9WZyLZCBzs'])
  52. self.assertEqual(len(DL.result), 25)
  53. self.assertEqual(DL.result[-1], ['http://www.youtube.com/watch?v=rYefUsYuEp0'])
  54. def test_youtube_channel(self):
  55. # I give up, please find a channel that does paginate and test this like test_youtube_playlist_long
  56. pass # TODO
  57. def test_youtube_user(self):
  58. DL = FakeDownloader()
  59. IE = YoutubeUserIE(DL)
  60. IE.extract('https://www.youtube.com/user/TheLinuxFoundation')
  61. self.assertTrue(len(DL.result) >= 320)
  62. if __name__ == '__main__':
  63. unittest.main()