1
0
Fork 0
mirror of https://github.com/ytdl-org/youtube-dl.git synced 2024-06-01 18:09:28 +00:00

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,
)
from youtube_dl.extractor import get_info_extractor
from youtube_dl.downloader.common import FileDownloader
RETRIES = 3
@ -56,9 +57,10 @@ class YoutubeDL(youtube_dl.YoutubeDL):
return super(YoutubeDL, self).process_info(info_dict)
def _file_md5(fn):
def _file_md5(fn, length=None):
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()
@ -224,7 +226,7 @@ def generator(test_case, tname):
(tc_filename, format_bytes(expected_minsize),
format_bytes(got_fsize)))
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)
# Finally, check test cases' data again but this time against
# extracted data from info JSON file written during processing