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