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