From e2b7c5475fa217d5ffce025db50c8331417cfc3a Mon Sep 17 00:00:00 2001 From: Lars Noack Date: Fri, 26 Apr 2024 13:03:20 +0200 Subject: [PATCH] fix: trace was passed in the request method shortened url for trace --- .vscode/settings.json | 2 ++ music_kraken/connection/connection.py | 4 ++-- music_kraken/utils/string_processing.py | 6 ++++++ 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 2897ca2..662ba25 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -25,9 +25,11 @@ "metallum", "musify", "OKBLUE", + "pathvalidate", "Referer", "sponsorblock", "tracksort", + "translit", "unmap", "youtube" ] diff --git a/music_kraken/connection/connection.py b/music_kraken/connection/connection.py index d9982bc..fe7f2ff 100644 --- a/music_kraken/connection/connection.py +++ b/music_kraken/connection/connection.py @@ -16,6 +16,7 @@ from .cache import Cache from .rotating import RotatingProxy from ..objects import Target from ..utils import request_trace +from ..utils.string_processing import shorten_display_url from ..utils.config import main_settings from ..utils.support_classes.download_result import DownloadResult from ..utils.hacking import merge_args @@ -149,8 +150,6 @@ class Connection: exclude_headers: List[str] = None, **kwargs ) -> Optional[requests.Response]: - trace_string = f"{method} {url} \t{'[stream]' if kwargs.get('stream', False) else ''}" - if method is None: raise AttributeError("method is not set.") method = method.upper() @@ -163,6 +162,7 @@ class Connection: current_kwargs.update(**kwargs) parsed_url = urlparse(url) + trace_string = f"{method} {shorten_display_url(url)} \t{'[stream]' if kwargs.get('stream', False) else ''}" if not raw_headers: _headers = copy.copy(self.HEADER_VALUES) diff --git a/music_kraken/utils/string_processing.py b/music_kraken/utils/string_processing.py index 17c9117..9acd3c8 100644 --- a/music_kraken/utils/string_processing.py +++ b/music_kraken/utils/string_processing.py @@ -178,3 +178,9 @@ def match_length(length_1: int | None, length_2: int | None) -> bool: if length_1 is None or length_2 is None: return True return abs(length_1 - length_2) <= ALLOWED_LENGTH_DISTANCE + +def shorten_display_url(url: str, max_length: int = 150, chars_at_end: int = 4, shorten_string: str = "[...]") -> str: + if len(url) <= max_length + chars_at_end + len(shorten_string): + return url + + return url[:max_length] + shorten_string + url[-chars_at_end:]