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.2 KiB

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.extractor import YoutubeIE
  11. from youtube_dl.utils import *
  12. from helper import FakeYDL
  13. md5 = lambda s: hashlib.md5(s.encode('utf-8')).hexdigest()
  14. class TestYoutubeSubtitles(unittest.TestCase):
  15. def setUp(self):
  16. self.DL = FakeYDL()
  17. self.url = 'QRS8MkLhQmM'
  18. def getInfoDict(self):
  19. IE = YoutubeIE(self.DL)
  20. info_dict = IE.extract(self.url)
  21. return info_dict
  22. def getSubtitles(self):
  23. info_dict = self.getInfoDict()
  24. return info_dict[0]['subtitles']
  25. def test_youtube_no_writesubtitles(self):
  26. self.DL.params['writesubtitles'] = False
  27. subtitles = self.getSubtitles()
  28. self.assertEqual(subtitles, None)
  29. def test_youtube_subtitles(self):
  30. self.DL.params['writesubtitles'] = True
  31. subtitles = self.getSubtitles()
  32. self.assertEqual(md5(subtitles['en']), '4cd9278a35ba2305f47354ee13472260')
  33. def test_youtube_subtitles_lang(self):
  34. self.DL.params['writesubtitles'] = True
  35. self.DL.params['subtitleslangs'] = ['it']
  36. subtitles = self.getSubtitles()
  37. self.assertEqual(md5(subtitles['it']), '164a51f16f260476a05b50fe4c2f161d')
  38. def test_youtube_allsubtitles(self):
  39. self.DL.params['writesubtitles'] = True
  40. self.DL.params['allsubtitles'] = True
  41. subtitles = self.getSubtitles()
  42. self.assertEqual(len(subtitles.keys()), 13)
  43. def test_youtube_subtitles_sbv_format(self):
  44. self.DL.params['writesubtitles'] = True
  45. self.DL.params['subtitlesformat'] = 'sbv'
  46. subtitles = self.getSubtitles()
  47. self.assertEqual(md5(subtitles['en']), '13aeaa0c245a8bed9a451cb643e3ad8b')
  48. def test_youtube_subtitles_vtt_format(self):
  49. self.DL.params['writesubtitles'] = True
  50. self.DL.params['subtitlesformat'] = 'vtt'
  51. subtitles = self.getSubtitles()
  52. self.assertEqual(md5(subtitles['en']), '356cdc577fde0c6783b9b822e7206ff7')
  53. def test_youtube_list_subtitles(self):
  54. self.DL.params['listsubtitles'] = True
  55. info_dict = self.getInfoDict()
  56. self.assertEqual(info_dict, None)
  57. def test_youtube_automatic_captions(self):
  58. self.url = '8YoUxe5ncPo'
  59. self.DL.params['writeautomaticsub'] = True
  60. self.DL.params['subtitleslangs'] = ['it']
  61. subtitles = self.getSubtitles()
  62. self.assertTrue(subtitles['it'] is not None)
  63. def test_youtube_nosubtitles(self):
  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()