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