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