]>
Commit | Line | Data |
---|---|---|
1fb07d10 JG |
1 | #!/usr/bin/env python |
2 | # coding: utf-8 | |
3 | ||
4 | import xml.etree.ElementTree | |
5 | import os | |
6 | import sys | |
7 | import unittest | |
8 | ||
9 | # Allow direct execution | |
10 | sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
11 | ||
12 | import youtube_dl.YoutubeDL | |
13 | import youtube_dl.extractor | |
14 | from youtube_dl.utils import * | |
15 | from .helper import try_rm | |
16 | ||
17 | PARAMETERS_FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "parameters.json") | |
18 | ||
19 | # General configuration (from __init__, not very elegant...) | |
20 | jar = compat_cookiejar.CookieJar() | |
21 | cookie_processor = compat_urllib_request.HTTPCookieProcessor(jar) | |
22 | proxy_handler = compat_urllib_request.ProxyHandler() | |
23 | opener = compat_urllib_request.build_opener(proxy_handler, cookie_processor, YoutubeDLHandler()) | |
24 | compat_urllib_request.install_opener(opener) | |
25 | ||
26 | class 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 | ||
31 | with io.open(PARAMETERS_FILE, encoding='utf-8') as pf: | |
32 | params = json.load(pf) | |
33 | params['writeannotations'] = True | |
34 | params['skip_download'] = True | |
35 | params['writeinfojson'] = False | |
36 | params['format'] = 'flv' | |
37 | ||
38 | TEST_ID = 'gr51aVj-mLg' | |
39 | ANNOTATIONS_FILE = TEST_ID + '.flv.annotations.xml' | |
40 | EXPECTED_ANNOTATIONS = ['Speech bubble', 'Note', 'Title', 'Spotlight', 'Label'] | |
41 | ||
42 | class 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 | ||
81 | if __name__ == '__main__': | |
82 | unittest.main() |