generated from brennen/python_project
initial code added
This commit is contained in:
@@ -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
|
||||||
|
```
|
||||||
|
|||||||
@@ -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 = []
|
||||||
@@ -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)
|
||||||
Reference in New Issue
Block a user