]>
Commit | Line | Data |
---|---|---|
cc52de43 | 1 | #!/usr/bin/env python3 |
44a5f171 | 2 | # Allow direct execution |
1fb07d10 JG |
3 | import os |
4 | import sys | |
5 | import unittest | |
44a5f171 | 6 | |
f8271158 | 7 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
44a5f171 | 8 | |
44a5f171 | 9 | import xml.etree.ElementTree |
f8271158 | 10 | from test.helper import get_params, is_download_test, try_rm |
1fb07d10 | 11 | |
7a5c1cfe | 12 | import yt_dlp.extractor |
f8271158 | 13 | import yt_dlp.YoutubeDL |
1fb07d10 | 14 | |
1fb07d10 | 15 | |
7a5c1cfe | 16 | class YoutubeDL(yt_dlp.YoutubeDL): |
1fb07d10 | 17 | def __init__(self, *args, **kwargs): |
86e5f3ed | 18 | super().__init__(*args, **kwargs) |
1fb07d10 JG |
19 | self.to_stderr = self.to_screen |
20 | ||
582be358 | 21 | |
44a5f171 PH |
22 | params = get_params({ |
23 | 'writeannotations': True, | |
24 | 'skip_download': True, | |
25 | 'writeinfojson': False, | |
26 | 'format': 'flv', | |
27 | }) | |
28 | ||
29 | ||
1fb07d10 | 30 | TEST_ID = 'gr51aVj-mLg' |
c67a055d | 31 | ANNOTATIONS_FILE = TEST_ID + '.annotations.xml' |
1fb07d10 JG |
32 | EXPECTED_ANNOTATIONS = ['Speech bubble', 'Note', 'Title', 'Spotlight', 'Label'] |
33 | ||
5f6a1245 | 34 | |
060ac762 | 35 | @is_download_test |
1fb07d10 JG |
36 | class TestAnnotations(unittest.TestCase): |
37 | def setUp(self): | |
38 | # Clear old files | |
39 | self.tearDown() | |
40 | ||
1fb07d10 | 41 | def test_info_json(self): |
5f6a1245 | 42 | expected = list(EXPECTED_ANNOTATIONS) # Two annotations could have the same text. |
7a5c1cfe | 43 | ie = yt_dlp.extractor.YoutubeIE() |
1fb07d10 JG |
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 | |
86e5f3ed | 49 | with open(ANNOTATIONS_FILE, encoding='utf-8') as annof: |
5f6a1245 | 50 | annoxml = xml.etree.ElementTree.parse(annof) |
1fb07d10 JG |
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 | ||
5f6a1245 | 58 | # Not all the annotations have TEXT children and the annotations are returned unsorted. |
1fb07d10 | 59 | for a in annotations: |
5f6a1245 JW |
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 | |
dfb1b146 | 65 | # remove the first occurrence, there could be more than one annotation with the same text |
5f6a1245 JW |
66 | expected.remove(text) |
67 | # We should have seen (and removed) all the expected annotation texts. | |
1fb07d10 | 68 | self.assertEqual(len(expected), 0, 'Not all expected annotations were found.') |
1fb07d10 JG |
69 | |
70 | def tearDown(self): | |
71 | try_rm(ANNOTATIONS_FILE) | |
72 | ||
582be358 | 73 | |
1fb07d10 JG |
74 | if __name__ == '__main__': |
75 | unittest.main() |