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