]>
jfr.im git - yt-dlp.git/blob - test/test_write_annotations.py
4 import xml
.etree
.ElementTree
9 # Allow direct execution
10 sys
.path
.append(os
.path
.dirname(os
.path
.dirname(os
.path
.abspath(__file__
))))
12 import youtube_dl
.YoutubeDL
13 import youtube_dl
.extractor
14 from youtube_dl
.utils
import *
15 from .helper
import try_rm
17 PARAMETERS_FILE
= os
.path
.join(os
.path
.dirname(os
.path
.abspath(__file__
)), "parameters.json")
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
)
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
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'
38 TEST_ID
= 'gr51aVj-mLg'
39 ANNOTATIONS_FILE
= TEST_ID
+ '.flv.annotations.xml'
40 EXPECTED_ANNOTATIONS
= ['Speech bubble', 'Note', 'Title', 'Spotlight', 'Label']
42 class TestAnnotations(unittest
.TestCase
):
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
))
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')
65 #Not all the annotations have TEXT children and the annotations are returned unsorted.
67 self
.assertEqual(a
.tag
, 'annotation')
68 if a
.get('type') == 'text':
69 textTag
= a
.find('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
74 #We should have seen (and removed) all the expected annotation texts.
75 self
.assertEqual(len(expected
), 0, 'Not all expected annotations were found.')
79 try_rm(ANNOTATIONS_FILE
)
81 if __name__
== '__main__':