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