]> Raphaƫl G. Git Repositories - youtubedl/blob - test/test_write_annotations.py
debian/NEWS: Update with notes about split audio/video downloads.
[youtubedl] / test / test_write_annotations.py
1 #!/usr/bin/env python
2 # coding: utf-8
3
4 # Allow direct execution
5 import os
6 import sys
7 import unittest
8 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9
10 from test.helper import get_params, global_setup, try_rm
11 global_setup()
12
13
14 import io
15
16 import xml.etree.ElementTree
17
18 import youtube_dl.YoutubeDL
19 import youtube_dl.extractor
20
21
22 class 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
27 params = get_params({
28 'writeannotations': True,
29 'skip_download': True,
30 'writeinfojson': False,
31 'format': 'flv',
32 })
33
34
35
36 TEST_ID = 'gr51aVj-mLg'
37 ANNOTATIONS_FILE = TEST_ID + '.flv.annotations.xml'
38 EXPECTED_ANNOTATIONS = ['Speech bubble', 'Note', 'Title', 'Spotlight', 'Label']
39
40 class TestAnnotations(unittest.TestCase):
41 def setUp(self):
42 # Clear old files
43 self.tearDown()
44
45
46 def test_info_json(self):
47 expected = list(EXPECTED_ANNOTATIONS) #Two annotations could have the same text.
48 ie = youtube_dl.extractor.YoutubeIE()
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:
55 annoxml = xml.etree.ElementTree.parse(annof)
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
63 #Not all the annotations have TEXT children and the annotations are returned unsorted.
64 for a in annotations:
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
70 #remove the first occurance, there could be more than one annotation with the same text
71 expected.remove(text)
72 #We should have seen (and removed) all the expected annotation texts.
73 self.assertEqual(len(expected), 0, 'Not all expected annotations were found.')
74
75
76 def tearDown(self):
77 try_rm(ANNOTATIONS_FILE)
78
79 if __name__ == '__main__':
80 unittest.main()