From 8eca1e0fdbba9482436b725dfddb10cd924813e3 Mon Sep 17 00:00:00 2001 From: bashonly Date: Tue, 18 Feb 2025 16:58:25 -0600 Subject: [PATCH] move `_request_dump_filename` to `utils` Authored by: bashonly --- yt_dlp/YoutubeDL.py | 18 ------------------ yt_dlp/downloader/hls.py | 5 ++++- yt_dlp/extractor/common.py | 10 +++++++--- yt_dlp/utils/_utils.py | 18 ++++++++++++++++++ 4 files changed, 29 insertions(+), 22 deletions(-) diff --git a/yt_dlp/YoutubeDL.py b/yt_dlp/YoutubeDL.py index d589db562..3293a9076 100644 --- a/yt_dlp/YoutubeDL.py +++ b/yt_dlp/YoutubeDL.py @@ -5,7 +5,6 @@ import errno import fileinput import functools -import hashlib import http.cookiejar import io import itertools @@ -4426,20 +4425,3 @@ def _write_thumbnails(self, label, info_dict, filename, thumb_filename_base=None if ret and not write_all: break return ret - - def _request_dump_filename(self, url, video_id, data=None): - if data is not None: - data = hashlib.md5(data).hexdigest() - basen = join_nonempty(video_id, data, url, delim='_') - trim_length = self.params.get('trim_file_name') or 240 - if len(basen) > trim_length: - h = '___' + hashlib.md5(basen.encode()).hexdigest() - basen = basen[:trim_length - len(h)] + h - filename = sanitize_filename(f'{basen}.dump', restricted=True) - # Working around MAX_PATH limitation on Windows (see - # http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx) - if os.name == 'nt': - absfilepath = os.path.abspath(filename) - if len(absfilepath) > 259: - filename = fR'\\?\{absfilepath}' - return filename diff --git a/yt_dlp/downloader/hls.py b/yt_dlp/downloader/hls.py index 1e9ab911f..940df7848 100644 --- a/yt_dlp/downloader/hls.py +++ b/yt_dlp/downloader/hls.py @@ -9,6 +9,7 @@ from .. import webvtt from ..dependencies import Cryptodome from ..utils import ( + _request_dump_filename, bug_reports_message, parse_m3u8_attributes, remove_start, @@ -82,7 +83,9 @@ def real_download(self, filename, info_dict): man_url = urlh.url s_bytes = urlh.read() if self.params.get('write_pages'): - dump_filename = self.ydl._request_dump_filename(man_url, info_dict['id'], None) + dump_filename = _request_dump_filename( + man_url, info_dict['id'], None, + trim_length=self.params.get('trim_file_name')) self.to_screen(f'[{self.FD_NAME}] Saving request to {dump_filename}') with open(dump_filename, 'wb') as outf: outf.write(s_bytes) diff --git a/yt_dlp/extractor/common.py b/yt_dlp/extractor/common.py index a809537c4..b26f0cdaf 100644 --- a/yt_dlp/extractor/common.py +++ b/yt_dlp/extractor/common.py @@ -51,6 +51,7 @@ RegexNotFoundError, RetryManager, UnsupportedError, + _request_dump_filename, age_restricted, base_url, bug_reports_message, @@ -1048,7 +1049,9 @@ def _webpage_read_content(self, urlh, url_or_request, video_id, note=None, errno if self.get_param('write_pages'): if isinstance(url_or_request, Request): data = self._create_request(url_or_request, data).data - filename = self._downloader._request_dump_filename(urlh.url, video_id, data) + filename = _request_dump_filename( + urlh.url, video_id, data, + trim_length=self.get_param('trim_file_name')) self.to_screen(f'Saving request to {filename}') with open(filename, 'wb') as outf: outf.write(webpage_bytes) @@ -1109,8 +1112,9 @@ def download_content(self, url_or_request, video_id, note=note, errnote=errnote, impersonate=None, require_impersonation=False): if self.get_param('load_pages'): url_or_request = self._create_request(url_or_request, data, headers, query) - filename = self._downloader._request_dump_filename( - url_or_request.url, video_id, url_or_request.data) + filename = _request_dump_filename( + url_or_request.url, video_id, url_or_request.data, + trim_length=self.get_param('trim_file_name')) self.to_screen(f'Loading request from {filename}') try: with open(filename, 'rb') as dumpf: diff --git a/yt_dlp/utils/_utils.py b/yt_dlp/utils/_utils.py index a71a381e5..ae5bc1a9a 100644 --- a/yt_dlp/utils/_utils.py +++ b/yt_dlp/utils/_utils.py @@ -5659,3 +5659,21 @@ def stdout(self, message): def stderr(self, message): if self._ydl: self._ydl.to_stderr(message) + + +def _request_dump_filename(self, url, video_id, data=None, trim_length=None): + if data is not None: + data = hashlib.md5(data).hexdigest() + basen = join_nonempty(video_id, data, url, delim='_') + trim_length = trim_length or 240 + if len(basen) > trim_length: + h = '___' + hashlib.md5(basen.encode()).hexdigest() + basen = basen[:trim_length - len(h)] + h + filename = sanitize_filename(f'{basen}.dump', restricted=True) + # Working around MAX_PATH limitation on Windows (see + # http://msdn.microsoft.com/en-us/library/windows/desktop/aa365247(v=vs.85).aspx) + if os.name == 'nt': + absfilepath = os.path.abspath(filename) + if len(absfilepath) > 259: + filename = fR'\\?\{absfilepath}' + return filename