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.

82 lines
3.1 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['allsubtitles'] = True
  40. subtitles = self.getSubtitles()
  41. self.assertEqual(len(subtitles.keys()), 13)
  42. def test_youtube_subtitles_sbv_format(self):
  43. self.DL.params['writesubtitles'] = True
  44. self.DL.params['subtitlesformat'] = 'sbv'
  45. subtitles = self.getSubtitles()
  46. self.assertEqual(md5(subtitles['en']), '13aeaa0c245a8bed9a451cb643e3ad8b')
  47. def test_youtube_subtitles_vtt_format(self):
  48. self.DL.params['writesubtitles'] = True
  49. self.DL.params['subtitlesformat'] = 'vtt'
  50. subtitles = self.getSubtitles()
  51. self.assertEqual(md5(subtitles['en']), '356cdc577fde0c6783b9b822e7206ff7')
  52. def test_youtube_list_subtitles(self):
  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.url = 'sAjKT8FhjI8'
  64. self.DL.params['allsubtitles'] = True
  65. subtitles = self.getSubtitles()
  66. self.assertEqual(len(subtitles), 0)
  67. def test_youtube_multiple_langs(self):
  68. self.url = 'QRS8MkLhQmM'
  69. self.DL.params['writesubtitles'] = True
  70. langs = ['it', 'fr', 'de']
  71. self.DL.params['subtitleslangs'] = langs
  72. subtitles = self.getSubtitles()
  73. for lang in langs:
  74. self.assertTrue(subtitles.get(lang) is not None, u'Subtitles for \'%s\' not extracted' % lang)
  75. if __name__ == '__main__':
  76. unittest.main()