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