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.

84 lines
3.3 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 hashlib
  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.extractor import YoutubeIE
  9. from youtube_dl.utils import *
  10. from .helper import FakeYDL
  11. md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest()
  12. class TestYoutubeSubtitles(unittest.TestCase):
  13. def setUp(self):
  14. self.DL = FakeYDL()
  15. self.url = 'QRS8MkLhQmM'
  16. def getInfoDict(self):
  17. IE = YoutubeIE(self.DL)
  18. info_dict = IE.extract(self.url)
  19. return info_dict
  20. def getSubtitles(self):
  21. info_dict = self.getInfoDict()
  22. return info_dict[0]['subtitles']
  23. def test_youtube_no_writesubtitles(self):
  24. self.DL.params['writesubtitles'] = False
  25. subtitles = self.getSubtitles()
  26. self.assertEqual(subtitles, None)
  27. def test_youtube_subtitles(self):
  28. self.DL.params['writesubtitles'] = True
  29. subtitles = self.getSubtitles()
  30. self.assertEqual(md5(subtitles['en']), '4cd9278a35ba2305f47354ee13472260')
  31. def test_youtube_subtitles_lang(self):
  32. self.DL.params['writesubtitles'] = True
  33. self.DL.params['subtitleslangs'] = ['it']
  34. subtitles = self.getSubtitles()
  35. self.assertEqual(md5(subtitles['it']), '164a51f16f260476a05b50fe4c2f161d')
  36. def test_youtube_allsubtitles(self):
  37. self.DL.params['writesubtitles'] = True
  38. self.DL.params['allsubtitles'] = True
  39. subtitles = self.getSubtitles()
  40. self.assertEqual(len(subtitles.keys()), 13)
  41. def test_youtube_subtitles_sbv_format(self):
  42. self.DL.params['writesubtitles'] = True
  43. self.DL.params['subtitlesformat'] = 'sbv'
  44. subtitles = self.getSubtitles()
  45. self.assertEqual(md5(subtitles['en']), '13aeaa0c245a8bed9a451cb643e3ad8b')
  46. def test_youtube_subtitles_vtt_format(self):
  47. self.DL.params['writesubtitles'] = True
  48. self.DL.params['subtitlesformat'] = 'vtt'
  49. subtitles = self.getSubtitles()
  50. self.assertEqual(md5(subtitles['en']), '356cdc577fde0c6783b9b822e7206ff7')
  51. def test_youtube_list_subtitles(self):
  52. self.DL.expect_warning(u'Video doesn\'t have automatic captions')
  53. self.DL.params['listsubtitles'] = True
  54. info_dict = self.getInfoDict()
  55. self.assertEqual(info_dict, None)
  56. def test_youtube_automatic_captions(self):
  57. self.url = '8YoUxe5ncPo'
  58. self.DL.params['writeautomaticsub'] = True
  59. self.DL.params['subtitleslangs'] = ['it']
  60. subtitles = self.getSubtitles()
  61. self.assertTrue(subtitles['it'] is not None)
  62. def test_youtube_nosubtitles(self):
  63. self.DL.expect_warning(u'video doesn\'t have subtitles')
  64. self.url = 'sAjKT8FhjI8'
  65. self.DL.params['writesubtitles'] = True
  66. self.DL.params['allsubtitles'] = True
  67. subtitles = self.getSubtitles()
  68. self.assertEqual(len(subtitles), 0)
  69. def test_youtube_multiple_langs(self):
  70. self.url = 'QRS8MkLhQmM'
  71. self.DL.params['writesubtitles'] = True
  72. langs = ['it', 'fr', 'de']
  73. self.DL.params['subtitleslangs'] = langs
  74. subtitles = self.getSubtitles()
  75. for lang in langs:
  76. self.assertTrue(subtitles.get(lang) is not None, u'Subtitles for \'%s\' not extracted' % lang)
  77. if __name__ == '__main__':
  78. unittest.main()