diff --git a/README.md b/README.md index b7ebb19..91611a3 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,9 @@ -# python_project +# Relative Playlist Maker +Recursively traverse a music folder containing tracks and playlist files that reference them, replacing absolute paths in the playlists with equivalent relative paths. + +## Usage + +```shell +python relative-playlist-maker.py ~/Music +``` diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..2b727d0 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,6 @@ +[project] +name = "relative-playlist-maker" +version = "0.1.0" +description = "Recursively traverse a music folder containing tracks and playlist files that reference them, replacing absolute paths in the playlists with equivalent relative paths." +requires-python = ">=3.4" +dependencies = [] diff --git a/relative-playlist-maker.py b/relative-playlist-maker.py new file mode 100644 index 0000000..c93e7cd --- /dev/null +++ b/relative-playlist-maker.py @@ -0,0 +1,41 @@ +from argparse import ArgumentParser +from pathlib import Path +from logging import basicConfig, warning, WARNING +from shutil import copyfile + +def main(music_folder: Path): + music_folder = music_folder.resolve() + + for m3u in music_folder.rglob("*.m3u"): + copyfile(m3u, m3u.with_suffix(".m3u.bak")) + + with m3u.open("r") as contents: + tracks = [Path(p).resolve() for p in contents.readlines()] + + if not all(music_folder in track.parents for track in tracks): + warning(f"{m3u} contains references to tracks that are not in {music_folder} and will be skipped") + continue + + with m3u.open("w") as updated_contents: + for track in tracks: + updated_contents.write(str(track.relative_to(m3u, walk_up=True)).replace("\\", "/")) + + +if __name__ == "__main__": + basicConfig( + level=WARNING, + ) + + parser = ArgumentParser( + description="" + ) + + parser.add_argument( + "music_folder", + required=True, + type=Path + ) + + args = parser.parse_args() + + main(args.music_folder) diff --git a/uv.lock b/uv.lock new file mode 100644 index 0000000..f33540e --- /dev/null +++ b/uv.lock @@ -0,0 +1,8 @@ +version = 1 +revision = 3 +requires-python = ">=3.4" + +[[package]] +name = "relative-playlist-maker" +version = "0.1.0" +source = { virtual = "." }