Safe filename regexp

This commit is contained in:
DarkCat09 2023-02-07 19:49:22 +04:00
parent a24ebe8aff
commit e8b1330809
2 changed files with 13 additions and 4 deletions

View file

@ -31,6 +31,10 @@ USERAGENT = (
LYRICS_ROW = '.main-page>.row>.col-xs-12' LYRICS_ROW = '.main-page>.row>.col-xs-12'
safename_re = re.compile(
r'[^A-Za-z0-9А-ЯЁа-яё \'".,()\[\]&!#$@_~=*+-]'
)
session = requests.Session() session = requests.Session()
session.headers['User-Agent'] = USERAGENT session.headers['User-Agent'] = USERAGENT
@ -95,6 +99,11 @@ def input_num(msg: str, def_: int = 0) -> int:
return def_ return def_
def safename(value: str) -> str:
return safename_re.sub(' ', value)
def conv_title(file: str) -> str: def conv_title(file: str) -> str:
# Remove file path # Remove file path
@ -358,12 +367,12 @@ def tagmp3(
newdir = ( newdir = (
Path('./tagged') / Path('./tagged') /
parsed['artist'] / safename(parsed['artist']) /
parsed['album'] safename(parsed['album'])
) )
os.makedirs(newdir, exist_ok=True) os.makedirs(newdir, exist_ok=True)
newpath = newdir / ( newpath = newdir / safename(
f"{parsed['track_no']}. " + f"{parsed['track_no']}. " +
f"{parsed['title']}.mp3" f"{parsed['title']}.mp3"
) )

View file

@ -30,7 +30,7 @@ ytlink () {
} }
safename () { safename () {
safe="${1//[^A-Za-z0-9А-Яа-я ().,_-]/}" safe="${1//[^A-Za-z0-9А-ЯЁа-яё ().,_-]/_}"
echo "${safe// /_}" | sed -E 's/_+/_/' echo "${safe// /_}" | sed -E 's/_+/_/'
} }