Calculate file hash on an initial portion, if specified

External downloaders may not support limiting the download to a specified size
This commit is contained in:
df 2021-09-11 11:47:17 +01:00
parent a803582717
commit 051e014e56

View File

@ -37,6 +37,7 @@ from youtube_dl.utils import (
UnavailableVideoError, UnavailableVideoError,
) )
from youtube_dl.extractor import get_info_extractor from youtube_dl.extractor import get_info_extractor
from youtube_dl.downloader.common import FileDownloader
RETRIES = 3 RETRIES = 3
@ -56,9 +57,10 @@ class YoutubeDL(youtube_dl.YoutubeDL):
return super(YoutubeDL, self).process_info(info_dict) return super(YoutubeDL, self).process_info(info_dict)
def _file_md5(fn): def _file_md5(fn, length=None):
with open(fn, 'rb') as f: with open(fn, 'rb') as f:
return hashlib.md5(f.read()).hexdigest() return hashlib.md5(
f.read(length) if length is not None else f.read()).hexdigest()
defs = gettestcases() defs = gettestcases()
@ -224,7 +226,7 @@ def generator(test_case, tname):
(tc_filename, format_bytes(expected_minsize), (tc_filename, format_bytes(expected_minsize),
format_bytes(got_fsize))) format_bytes(got_fsize)))
if 'md5' in tc: if 'md5' in tc:
md5_for_file = _file_md5(tc_filename) md5_for_file = _file_md5(tc_filename) if not params.get('test') else _file_md5(tc_filename, FileDownloader._TEST_FILE_SIZE)
self.assertEqual(tc['md5'], md5_for_file) self.assertEqual(tc['md5'], md5_for_file)
# Finally, check test cases' data again but this time against # Finally, check test cases' data again but this time against
# extracted data from info JSON file written during processing # extracted data from info JSON file written during processing