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.

101 lines
3.6 KiB

12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
12 years ago
  1. #!/usr/bin/env python
  2. import sys
  3. import unittest
  4. import json
  5. import io
  6. import hashlib
  7. # Allow direct execution
  8. import os
  9. sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  10. from youtube_dl.InfoExtractors import YoutubeIE
  11. from youtube_dl.utils import *
  12. from youtube_dl import FileDownloader
  13. PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
  14. with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
  15. parameters = json.load(pf)
  16. # General configuration (from __init__, not very elegant...)
  17. jar = compat_cookiejar.CookieJar()
  18. cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
  19. proxy_handler = compat_urllib_request.ProxyHandler()
  20. opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
  21. compat_urllib_request.install_opener(opener)
  22. class FakeDownloader(FileDownloader):
  23. def __init__(self):
  24. self.result = []
  25. self.params = parameters
  26. def to_screen(self, s):
  27. print(s)
  28. def trouble(self, s, tb=None):
  29. raise Exception(s)
  30. def download(self, x):
  31. self.result.append(x)
  32. md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest()
  33. class TestYoutubeSubtitles(unittest.TestCase):
  34. def setUp(self):
  35. DL = FakeDownloader()
  36. DL.params['allsubtitles'] = False
  37. DL.params['writesubtitles'] = False
  38. DL.params['subtitlesformat'] = 'srt'
  39. DL.params['listsubtitles'] = False
  40. def test_youtube_no_subtitles(self):
  41. DL = FakeDownloader()
  42. DL.params['writesubtitles'] = False
  43. IE = YoutubeIE(DL)
  44. info_dict = IE.extract('QRS8MkLhQmM')
  45. subtitles = info_dict[0]['subtitles']
  46. self.assertEqual(subtitles, None)
  47. def test_youtube_subtitles(self):
  48. DL = FakeDownloader()
  49. DL.params['writesubtitles'] = True
  50. IE = YoutubeIE(DL)
  51. info_dict = IE.extract('QRS8MkLhQmM')
  52. sub = info_dict[0]['subtitles'][0]
  53. self.assertEqual(md5(sub[2]), '4cd9278a35ba2305f47354ee13472260')
  54. def test_youtube_subtitles_it(self):
  55. DL = FakeDownloader()
  56. DL.params['writesubtitles'] = True
  57. DL.params['subtitleslang'] = 'it'
  58. IE = YoutubeIE(DL)
  59. info_dict = IE.extract('QRS8MkLhQmM')
  60. sub = info_dict[0]['subtitles'][0]
  61. self.assertEqual(md5(sub[2]), '164a51f16f260476a05b50fe4c2f161d')
  62. def test_youtube_onlysubtitles(self):
  63. DL = FakeDownloader()
  64. DL.params['writesubtitles'] = True
  65. DL.params['onlysubtitles'] = True
  66. IE = YoutubeIE(DL)
  67. info_dict = IE.extract('QRS8MkLhQmM')
  68. sub = info_dict[0]['subtitles'][0]
  69. self.assertEqual(md5(sub[2]), '4cd9278a35ba2305f47354ee13472260')
  70. def test_youtube_allsubtitles(self):
  71. DL = FakeDownloader()
  72. DL.params['allsubtitles'] = True
  73. IE = YoutubeIE(DL)
  74. info_dict = IE.extract('QRS8MkLhQmM')
  75. subtitles = info_dict[0]['subtitles']
  76. self.assertEqual(len(subtitles), 13)
  77. def test_youtube_subtitles_format(self):
  78. DL = FakeDownloader()
  79. DL.params['writesubtitles'] = True
  80. DL.params['subtitlesformat'] = 'sbv'
  81. IE = YoutubeIE(DL)
  82. info_dict = IE.extract('QRS8MkLhQmM')
  83. sub = info_dict[0]['subtitles'][0]
  84. self.assertEqual(md5(sub[2]), '13aeaa0c245a8bed9a451cb643e3ad8b')
  85. def test_youtube_list_subtitles(self):
  86. DL = FakeDownloader()
  87. DL.params['listsubtitles'] = True
  88. IE = YoutubeIE(DL)
  89. info_dict = IE.extract('QRS8MkLhQmM')
  90. self.assertEqual(info_dict, None)
  91. if __name__ == '__main__':
  92. unittest.main()