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.

79 lines
2.5 KiB

  1. #!/usr/bin/env python
  2. # coding: utf-8
  3. # Allow direct execution
  4. import os
  5. import sys
  6. import unittest
  7. sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
  8. from test.helper import get_params, try_rm
  9. import io
  10. import xml.etree.ElementTree
  11. import youtube_dl.YoutubeDL
  12. import youtube_dl.extractor
  13. class YoutubeDL(youtube_dl.YoutubeDL):
  14. def __init__(self, *args, **kwargs):
  15. super(YoutubeDL, self).__init__(*args, **kwargs)
  16. self.to_stderr = self.to_screen
  17. params = get_params({
  18. 'writeannotations': True,
  19. 'skip_download': True,
  20. 'writeinfojson': False,
  21. 'format': 'flv',
  22. })
  23. TEST_ID = 'gr51aVj-mLg'
  24. ANNOTATIONS_FILE = TEST_ID + '.flv.annotations.xml'
  25. EXPECTED_ANNOTATIONS = ['Speech bubble', 'Note', 'Title', 'Spotlight', 'Label']
  26. class TestAnnotations(unittest.TestCase):
  27. def setUp(self):
  28. # Clear old files
  29. self.tearDown()
  30. def test_info_json(self):
  31. expected = list(EXPECTED_ANNOTATIONS) #Two annotations could have the same text.
  32. ie = youtube_dl.extractor.YoutubeIE()
  33. ydl = YoutubeDL(params)
  34. ydl.add_info_extractor(ie)
  35. ydl.download([TEST_ID])
  36. self.assertTrue(os.path.exists(ANNOTATIONS_FILE))
  37. annoxml = None
  38. with io.open(ANNOTATIONS_FILE, 'r', encoding='utf-8') as annof:
  39. annoxml = xml.etree.ElementTree.parse(annof)
  40. self.assertTrue(annoxml is not None, 'Failed to parse annotations XML')
  41. root = annoxml.getroot()
  42. self.assertEqual(root.tag, 'document')
  43. annotationsTag = root.find('annotations')
  44. self.assertEqual(annotationsTag.tag, 'annotations')
  45. annotations = annotationsTag.findall('annotation')
  46. #Not all the annotations have TEXT children and the annotations are returned unsorted.
  47. for a in annotations:
  48. self.assertEqual(a.tag, 'annotation')
  49. if a.get('type') == 'text':
  50. textTag = a.find('TEXT')
  51. text = textTag.text
  52. self.assertTrue(text in expected) #assertIn only added in python 2.7
  53. #remove the first occurance, there could be more than one annotation with the same text
  54. expected.remove(text)
  55. #We should have seen (and removed) all the expected annotation texts.
  56. self.assertEqual(len(expected), 0, 'Not all expected annotations were found.')
  57. def tearDown(self):
  58. try_rm(ANNOTATIONS_FILE)
  59. if __name__ == '__main__':
  60. unittest.main()