4 # Allow direct execution
8 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
10 from test.helper import get_params, try_rm
15 import xml.etree.ElementTree
17 import youtube_dl.YoutubeDL
18 import youtube_dl.extractor
21 class YoutubeDL(youtube_dl.YoutubeDL):
22 def __init__(self, *args, **kwargs):
23 super(YoutubeDL, self).__init__(*args, **kwargs)
24 self.to_stderr = self.to_screen
27 'writeannotations': True,
28 'skip_download': True,
29 'writeinfojson': False,
35 TEST_ID = 'gr51aVj-mLg'
36 ANNOTATIONS_FILE = TEST_ID + '.flv.annotations.xml'
37 EXPECTED_ANNOTATIONS = ['Speech bubble', 'Note', 'Title', 'Spotlight', 'Label']
39 class TestAnnotations(unittest.TestCase):
45 def test_info_json(self):
46 expected = list(EXPECTED_ANNOTATIONS) #Two annotations could have the same text.
47 ie = youtube_dl.extractor.YoutubeIE()
48 ydl = YoutubeDL(params)
49 ydl.add_info_extractor(ie)
50 ydl.download([TEST_ID])
51 self.assertTrue(os.path.exists(ANNOTATIONS_FILE))
53 with io.open(ANNOTATIONS_FILE, 'r', encoding='utf-8') as annof:
54 annoxml = xml.etree.ElementTree.parse(annof)
55 self.assertTrue(annoxml is not None, 'Failed to parse annotations XML')
56 root = annoxml.getroot()
57 self.assertEqual(root.tag, 'document')
58 annotationsTag = root.find('annotations')
59 self.assertEqual(annotationsTag.tag, 'annotations')
60 annotations = annotationsTag.findall('annotation')
62 #Not all the annotations have TEXT children and the annotations are returned unsorted.
64 self.assertEqual(a.tag, 'annotation')
65 if a.get('type') == 'text':
66 textTag = a.find('TEXT')
68 self.assertTrue(text in expected) #assertIn only added in python 2.7
69 #remove the first occurance, there could be more than one annotation with the same text
71 #We should have seen (and removed) all the expected annotation texts.
72 self.assertEqual(len(expected), 0, 'Not all expected annotations were found.')
76 try_rm(ANNOTATIONS_FILE)
78 if __name__ == '__main__':