1
0
Fork 0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2025-03-09 12:50:23 -05:00

move _request_dump_filename to utils

Authored by: bashonly
This commit is contained in:
bashonly 2025-02-18 16:58:25 -06:00
parent 25d2cc59c3
commit 8eca1e0fdb
No known key found for this signature in database
GPG key ID: 783F096F253D15B0
4 changed files with 29 additions and 22 deletions

View file

@ -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

View file

@ -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)

View file

@ -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:

View file

@ -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