Safe filename regexp
This commit is contained in:
parent
a24ebe8aff
commit
e8b1330809
2 changed files with 13 additions and 4 deletions
|
@ -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"
|
||||
)
|
||||
|
|
|
@ -30,7 +30,7 @@ ytlink () {
|
|||
}
|
||||
|
||||
safename () {
|
||||
safe="${1//[^A-Za-z0-9А-Яа-я ().,_-]/}"
|
||||
safe="${1//[^A-Za-z0-9А-ЯЁа-яё ().,_-]/_}"
|
||||
echo "${safe// /_}" | sed -E 's/_+/_/'
|
||||
}
|
||||
|
||||
|
|
Reference in a new issue