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