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.

100 lines
3.5 KiB

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