Изменение способа очистки пути

This commit is contained in:
Lev 2022-12-12 15:58:09 +07:00
parent 4238f32961
commit 6f5a79a063
No known key found for this signature in database
GPG key ID: 21C6C2C9C0A4460D

40
main.py
View file

@ -27,7 +27,7 @@ ALBUM_RE = re.compile(r'album/(\d+)$')
ARTIST_RE = re.compile(r'artist/(\d+)$') ARTIST_RE = re.compile(r'artist/(\d+)$')
PLAYLIST_RE = re.compile(r'([\w\-]+)/playlists/(\d+)$') PLAYLIST_RE = re.compile(r'([\w\-]+)/playlists/(\d+)$')
CLEAR_PATH_RE = re.compile(r'[^\w\-\.\'\\()/_ ]+') FILENAME_CLEAR_RE = re.compile(r'[^\w\-\.\'() ]+')
@dataclass @dataclass
@ -179,15 +179,24 @@ def get_playlist(session: Session, playlist: PlaylistId) -> list[BasicTrackInfo]
return [BasicTrackInfo.from_json(t) for t in raw_tracks] return [BasicTrackInfo.from_json(t) for t in raw_tracks]
def prepare_track_path(directory: Path, path: Path, track: BasicTrackInfo) -> Path: def prepare_track_path(path: Path, prepare_path: bool, track: BasicTrackInfo) -> Path:
path_part = str(path).replace('#number', str(track.number)) \ path_str = str(path)
.replace('#artist', track.artists[0]) \ repl_dict = {
.replace('#album-artist', track.album.artists[0]) \ '#number': track.number,
.replace('#title', track.title) \ '#artist': track.artists[0],
.replace('#album', track.album.title) \ '#album-artist': track.album.artists[0],
.replace('#year', str(track.album.year)) \ '#title': track.title,
+ '.mp3' '#album': track.album.title,
return directory / path_part '#year': track.album.year
}
for placeholder, replacement in repl_dict.items():
replacement = str(replacement)
if prepare_path:
replacement = FILENAME_CLEAR_RE.sub('_', replacement)
path_str = path_str.replace(placeholder, replacement)
path_str += '.mp3'
return Path(path_str)
def set_id3_tags(path: Path, track: BasicTrackInfo, lyrics: Optional[str], def set_id3_tags(path: Path, track: BasicTrackInfo, lyrics: Optional[str],
@ -212,11 +221,6 @@ def set_id3_tags(path: Path, track: BasicTrackInfo, lyrics: Optional[str],
tag.save() tag.save()
def clear_path(path: Path) -> Path:
cleared_path = CLEAR_PATH_RE.sub('_', str(path))
return Path(cleared_path)
if __name__ == '__main__': if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Загрузчик музыки с сервиса Яндекс.Музыка') parser = argparse.ArgumentParser(description='Загрузчик музыки с сервиса Яндекс.Музыка')
@ -260,7 +264,7 @@ if __name__ == '__main__':
path_group = parser.add_argument_group('Указание пути') path_group = parser.add_argument_group('Указание пути')
path_group.add_argument('--strict-path', action='store_true', path_group.add_argument('--strict-path', action='store_true',
help=('Очищать путь от недопустимых символов')) help='Очищать путь от недопустимых символов')
path_group.add_argument('--dir', default='.', metavar='<Папка>', path_group.add_argument('--dir', default='.', metavar='<Папка>',
help=help_str('Папка для загрузки музыки'), type=Path) help=help_str('Папка для загрузки музыки'), type=Path)
path_group.add_argument('--path-pattern', default=DEFAULT_PATH_PATTERN, path_group.add_argument('--path-pattern', default=DEFAULT_PATH_PATTERN,
@ -339,9 +343,7 @@ if __name__ == '__main__':
if album.version is not None: if album.version is not None:
album.title = f'{album.title} ({track.album.version})' album.title = f'{album.title} ({track.album.version})'
save_path = prepare_track_path(args.dir, args.path_pattern, track) save_path = prepare_track_path(args.path_pattern, args.strict_path, track)
if args.strict_path:
save_path = clear_path(save_path)
if args.skip_existing and save_path.is_file(): if args.skip_existing and save_path.is_file():
continue continue