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