From e8b1330809b2b9354b9cbf37715b085377f8c1b7 Mon Sep 17 00:00:00 2001 From: DarkCat09 Date: Tue, 7 Feb 2023 19:49:22 +0400 Subject: [PATCH] Safe filename regexp --- .id3tag_helper.py | 15 ++++++++++++--- autoytdlp.sh | 2 +- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/.id3tag_helper.py b/.id3tag_helper.py index 421197e..d7a9f53 100755 --- a/.id3tag_helper.py +++ b/.id3tag_helper.py @@ -31,6 +31,10 @@ USERAGENT = ( LYRICS_ROW = '.main-page>.row>.col-xs-12' +safename_re = re.compile( + r'[^A-Za-z0-9А-ЯЁа-яё \'".,()\[\]&!#$@_~=*+-]' +) + session = requests.Session() session.headers['User-Agent'] = USERAGENT @@ -95,6 +99,11 @@ def input_num(msg: str, def_: int = 0) -> int: return def_ +def safename(value: str) -> str: + + return safename_re.sub(' ', value) + + def conv_title(file: str) -> str: # Remove file path @@ -358,12 +367,12 @@ def tagmp3( newdir = ( Path('./tagged') / - parsed['artist'] / - parsed['album'] + safename(parsed['artist']) / + safename(parsed['album']) ) os.makedirs(newdir, exist_ok=True) - newpath = newdir / ( + newpath = newdir / safename( f"{parsed['track_no']}. " + f"{parsed['title']}.mp3" ) diff --git a/autoytdlp.sh b/autoytdlp.sh index ec3e8ac..ec63410 100755 --- a/autoytdlp.sh +++ b/autoytdlp.sh @@ -30,7 +30,7 @@ ytlink () { } safename () { - safe="${1//[^A-Za-z0-9А-Яа-я ().,_-]/}" + safe="${1//[^A-Za-z0-9А-ЯЁа-яё ().,_-]/_}" echo "${safe// /_}" | sed -E 's/_+/_/' }