]> jfr.im git - yt-dlp.git/blame - test/test_write_annotations.py
Merge remote-tracking branch 'origin/master'
[yt-dlp.git] / test / test_write_annotations.py
CommitLineData
1fb07d10
JG
1#!/usr/bin/env python
2# coding: utf-8
3
4import xml.etree.ElementTree
5import os
6import sys
7import unittest
8
9# Allow direct execution
10sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
11
12import youtube_dl.YoutubeDL
13import youtube_dl.extractor
14from youtube_dl.utils import *
15from .helper import try_rm
16
17PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json")
18
19# General configuration (from __init__, not very elegant...)
20jar = compat_cookiejar.CookieJar()
21cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar)
22proxy_handler = compat_urllib_request.ProxyHandler()
23opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler())
24compat_urllib_request.install_opener(opener)
25
26class YoutubeDL(youtube_dl.YoutubeDL):
27 def __init__(self, *args, **kwargs):
28 super(YoutubeDL, self).__init__(*args, **kwargs)
29 self.to_stderr = self.to_screen
30
31with io.open(PARAMETERS_FILE, encoding='utf-8') as pf:
32 params = json.load(pf)
33params['writeannotations'] = True
34params['skip_download'] = True
35params['writeinfojson'] = False
36params['format'] = 'flv'
37
38TEST_ID = 'gr51aVj-mLg'
39ANNOTATIONS_FILE = TEST_ID + '.flv.annotations.xml'
40EXPECTED_ANNOTATIONS = ['Speech bubble', 'Note', 'Title', 'Spotlight', 'Label']
41
42class TestAnnotations(unittest.TestCase):
43 def setUp(self):
44 # Clear old files
45 self.tearDown()
46
47
48 def test_info_json(self):
49 expected = list(EXPECTED_ANNOTATIONS) #Two annotations could have the same text.
50 ie = youtube_dl.extractor.YoutubeIE()
51 ydl = YoutubeDL(params)
52 ydl.add_info_extractor(ie)
53 ydl.download([TEST_ID])
54 self.assertTrue(os.path.exists(ANNOTATIONS_FILE))
55 annoxml = None
56 with io.open(ANNOTATIONS_FILE, 'r', encoding='utf-8') as annof:
57 annoxml = xml.etree.ElementTree.parse(annof)
58 self.assertTrue(annoxml is not None, 'Failed to parse annotations XML')
59 root = annoxml.getroot()
60 self.assertEqual(root.tag, 'document')
61 annotationsTag = root.find('annotations')
62 self.assertEqual(annotationsTag.tag, 'annotations')
63 annotations = annotationsTag.findall('annotation')
64
65 #Not all the annotations have TEXT children and the annotations are returned unsorted.
66 for a in annotations:
67 self.assertEqual(a.tag, 'annotation')
68 if a.get('type') == 'text':
69 textTag = a.find('TEXT')
70 text = textTag.text
71 self.assertTrue(text in expected) #assertIn only added in python 2.7
72 #remove the first occurance, there could be more than one annotation with the same text
73 expected.remove(text)
74 #We should have seen (and removed) all the expected annotation texts.
75 self.assertEqual(len(expected), 0, 'Not all expected annotations were found.')
76
77
78 def tearDown(self):
79 try_rm(ANNOTATIONS_FILE)
80
81if __name__ == '__main__':
82 unittest.main()