]>
Commit | Line | Data |
---|---|---|
fd5ff020 FV |
1 | #!/usr/bin/env python |
2 | ||
44a5f171 PH |
3 | # Allow direct execution |
4 | import os | |
5 | import sys | |
6 | import unittest | |
7 | sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) | |
8 | ||
dd508b7c FV |
9 | from test.helper import ( |
10 | get_params, | |
11 | get_testcases, | |
dd508b7c FV |
12 | try_rm, |
13 | md5, | |
14 | report_warning | |
15 | ) | |
44a5f171 PH |
16 | |
17 | ||
efe8902f | 18 | import hashlib |
fd5ff020 | 19 | import io |
7f60b5aa | 20 | import json |
6b3aef80 | 21 | import socket |
cdab8aa3 | 22 | |
8222d8de | 23 | import youtube_dl.YoutubeDL |
44a5f171 PH |
24 | from youtube_dl.utils import ( |
25 | compat_str, | |
26 | compat_urllib_error, | |
f6cc16f5 | 27 | compat_HTTPError, |
44a5f171 PH |
28 | DownloadError, |
29 | ExtractorError, | |
30 | UnavailableVideoError, | |
31 | ) | |
9ee2b5f6 | 32 | from youtube_dl.extractor import get_info_extractor |
fd5ff020 | 33 | |
8cc83b8d FV |
34 | RETRIES = 3 |
35 | ||
8222d8de | 36 | class YoutubeDL(youtube_dl.YoutubeDL): |
fd5ff020 | 37 | def __init__(self, *args, **kwargs): |
fd5ff020 | 38 | self.to_stderr = self.to_screen |
0eaf520d | 39 | self.processed_info_dicts = [] |
8222d8de | 40 | super(YoutubeDL, self).__init__(*args, **kwargs) |
476203d0 | 41 | def report_warning(self, message): |
be95cac1 FV |
42 | # Don't accept warnings during tests |
43 | raise ExtractorError(message) | |
0eaf520d FV |
44 | def process_info(self, info_dict): |
45 | self.processed_info_dicts.append(info_dict) | |
8222d8de | 46 | return super(YoutubeDL, self).process_info(info_dict) |
1535ac2a | 47 | |
fd5ff020 FV |
48 | def _file_md5(fn): |
49 | with open(fn, 'rb') as f: | |
50 | return hashlib.md5(f.read()).hexdigest() | |
51 | ||
fc2c063e | 52 | defs = get_testcases() |
6b47c7f2 | 53 | |
0eaf520d | 54 | |
1535ac2a | 55 | class TestDownload(unittest.TestCase): |
744435f2 | 56 | maxDiff = None |
fd5ff020 | 57 | def setUp(self): |
fd5ff020 FV |
58 | self.defs = defs |
59 | ||
911ee27e | 60 | ### Dynamically generate tests |
5d01a647 PH |
61 | def generator(test_case): |
62 | ||
1535ac2a | 63 | def test_template(self): |
d1cade5a | 64 | ie = youtube_dl.extractor.get_info_extractor(test_case['name']) |
9ee2b5f6 | 65 | other_ies = [get_info_extractor(ie_key) for ie_key in test_case.get('add_ie', [])] |
bc2884af JMF |
66 | def print_skipping(reason): |
67 | print('Skipping %s: %s' % (test_case['name'], reason)) | |
9ee2b5f6 | 68 | if not ie.working(): |
bc2884af | 69 | print_skipping('IE marked as not _WORKING') |
fd5ff020 | 70 | return |
702665c0 JMF |
71 | if 'playlist' not in test_case: |
72 | info_dict = test_case.get('info_dict', {}) | |
73 | if not test_case.get('file') and not (info_dict.get('id') and info_dict.get('ext')): | |
74 | print_skipping('The output file cannot be know, the "file" ' | |
75 | 'key is missing or the info_dict is incomplete') | |
76 | return | |
fd5ff020 | 77 | if 'skip' in test_case: |
bc2884af | 78 | print_skipping(test_case['skip']) |
fd5ff020 | 79 | return |
9ee2b5f6 JMF |
80 | for other_ie in other_ies: |
81 | if not other_ie.working(): | |
82 | print_skipping(u'test depends on %sIE, marked as not WORKING' % other_ie.ie_key()) | |
83 | return | |
0eaf520d | 84 | |
44a5f171 | 85 | params = get_params(test_case.get('params', {})) |
0eaf520d | 86 | |
8222d8de | 87 | ydl = YoutubeDL(params) |
023fa8c4 | 88 | ydl.add_default_info_extractors() |
bffbd5f0 PH |
89 | finished_hook_called = set() |
90 | def _hook(status): | |
91 | if status['status'] == 'finished': | |
92 | finished_hook_called.add(status['filename']) | |
933605d7 | 93 | ydl.add_progress_hook(_hook) |
5c892b0b | 94 | |
702665c0 JMF |
95 | def get_tc_filename(tc): |
96 | return tc.get('file') or ydl.prepare_filename(tc.get('info_dict', {})) | |
97 | ||
5c892b0b | 98 | test_cases = test_case.get('playlist', [test_case]) |
702665c0 JMF |
99 | def try_rm_tcs_files(): |
100 | for tc in test_cases: | |
101 | tc_filename = get_tc_filename(tc) | |
102 | try_rm(tc_filename) | |
103 | try_rm(tc_filename + '.part') | |
4eb92208 | 104 | try_rm(os.path.splitext(tc_filename)[0] + '.info.json') |
702665c0 | 105 | try_rm_tcs_files() |
5c892b0b | 106 | try: |
dd508b7c FV |
107 | try_num = 1 |
108 | while True: | |
8cc83b8d | 109 | try: |
8222d8de | 110 | ydl.download([test_case['url']]) |
8cc83b8d | 111 | except (DownloadError, ExtractorError) as err: |
8cc83b8d | 112 | # Check if the exception is not a network related one |
f6cc16f5 | 113 | if not err.exc_info[0] in (compat_urllib_error.URLError, socket.timeout, UnavailableVideoError) or (err.exc_info[0] == compat_HTTPError and err.exc_info[1].code == 503): |
8cc83b8d FV |
114 | raise |
115 | ||
dd508b7c FV |
116 | if try_num == RETRIES: |
117 | report_warning(u'Failed due to network errors, skipping...') | |
118 | return | |
119 | ||
120 | print('Retrying: {0} failed tries\n\n##########\n\n'.format(try_num)) | |
121 | ||
122 | try_num += 1 | |
8cc83b8d FV |
123 | else: |
124 | break | |
5c892b0b PH |
125 | |
126 | for tc in test_cases: | |
702665c0 | 127 | tc_filename = get_tc_filename(tc) |
511eda8e | 128 | if not test_case.get('params', {}).get('skip_download', False): |
702665c0 JMF |
129 | self.assertTrue(os.path.exists(tc_filename), msg='Missing file ' + tc_filename) |
130 | self.assertTrue(tc_filename in finished_hook_called) | |
4eb92208 PH |
131 | info_json_fn = os.path.splitext(tc_filename)[0] + '.info.json' |
132 | self.assertTrue(os.path.exists(info_json_fn)) | |
5c892b0b | 133 | if 'md5' in tc: |
702665c0 | 134 | md5_for_file = _file_md5(tc_filename) |
5c892b0b | 135 | self.assertEqual(md5_for_file, tc['md5']) |
4eb92208 | 136 | with io.open(info_json_fn, encoding='utf-8') as infof: |
5c892b0b | 137 | info_dict = json.load(infof) |
51ce3a75 PH |
138 | for (info_field, expected) in tc.get('info_dict', {}).items(): |
139 | if isinstance(expected, compat_str) and expected.startswith('md5:'): | |
b7052e50 | 140 | got = 'md5:' + md5(info_dict.get(info_field)) |
78d3442b | 141 | else: |
51ce3a75 | 142 | got = info_dict.get(info_field) |
b7052e50 JMF |
143 | self.assertEqual(expected, got, |
144 | u'invalid value for field %s, expected %r, got %r' % (info_field, expected, got)) | |
78d3442b FV |
145 | |
146 | # If checkable fields are missing from the test case, print the info_dict | |
ee55fcbe | 147 | test_info_dict = dict((key, value if not isinstance(value, compat_str) or len(value) < 250 else 'md5:' + md5(value)) |
78d3442b FV |
148 | for key, value in info_dict.items() |
149 | if value and key in ('title', 'description', 'uploader', 'upload_date', 'uploader_id', 'location')) | |
150 | if not all(key in tc.get('info_dict', {}).keys() for key in test_info_dict.keys()): | |
484aaeb2 | 151 | sys.stderr.write(u'\n"info_dict": ' + json.dumps(test_info_dict, ensure_ascii=False, indent=4) + u'\n') |
78d3442b FV |
152 | |
153 | # Check for the presence of mandatory fields | |
154 | for key in ('id', 'url', 'title', 'ext'): | |
155 | self.assertTrue(key in info_dict.keys() and info_dict[key]) | |
9103bbc5 | 156 | # Check for mandatory fields that are automatically set by YoutubeDL |
be97abc2 | 157 | for key in ['webpage_url', 'extractor', 'extractor_key']: |
9103bbc5 | 158 | self.assertTrue(info_dict.get(key), u'Missing field: %s' % key) |
5c892b0b | 159 | finally: |
702665c0 | 160 | try_rm_tcs_files() |
fd5ff020 | 161 | |
1535ac2a | 162 | return test_template |
fd5ff020 | 163 | |
5d01a647 | 164 | ### And add them to TestDownload |
f7ab6cbe | 165 | for n, test_case in enumerate(defs): |
5d01a647 | 166 | test_method = generator(test_case) |
2eb88d95 PH |
167 | tname = 'test_' + str(test_case['name']) |
168 | i = 1 | |
169 | while hasattr(TestDownload, tname): | |
41beccba | 170 | tname = 'test_' + str(test_case['name']) + '_' + str(i) |
2eb88d95 PH |
171 | i += 1 |
172 | test_method.__name__ = tname | |
fd5ff020 | 173 | setattr(TestDownload, test_method.__name__, test_method) |
5d01a647 | 174 | del test_method |
cdab8aa3 PH |
175 | |
176 | ||
177 | if __name__ == '__main__': | |
178 | unittest.main() |