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

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