-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathm3u_orphans.py
More file actions
executable file
·74 lines (63 loc) · 2.21 KB
/
m3u_orphans.py
File metadata and controls
executable file
·74 lines (63 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
"""
Check m3u playlists for orphaned references
Only checks for physical files
usage: m3u_orphans path/to/playlist -b path/to/playlist/items/base
"""
import argparse
import sys
from pathlib import Path
PLAYLIST_MAGIC="#EXTM3U"
parser = argparse.ArgumentParser(
prog='m3u_orphans',
description="identify orphaned tracks")
# need a playlist file to check
parser.add_argument('filename', help="playlist file to check")
# where to start search
parser.add_argument('-b', '--base', action="store",
help="where to search from files on the playlist")
# where to put corrected playlist
parser.add_argument('-o', '--output-file',
help="where to put playlist with corrections made")
args = parser.parse_args()
def main():
# verify playlist is a valid file
playlist = Path(args.filename)
if not playlist.is_file():
print("'{}' is not a file!".format(args.filename))
sys.exit()
# corrected playlist
out = ""
with playlist.open() as file:
lines = file.readlines()
# strip newline and check if it matches the magic
if lines[0][:-1] != PLAYLIST_MAGIC:
print("'{}' is not a valid playlist!".format(args.filename))
sys.exit()
out += lines[0]
# check all the songs
for line in lines[1:]:
# strip newline
song = line[:-1]
base = "." if args.base is None else args.base
song_path = Path(base, song)
# if it's fine, just add it
if song_path.is_file():
out += line
continue
# otherwise, try some corrections
#TODO: use arguments to do this
#song = song.replace('/wrong/path/', '/right/path/')
song_path = Path(song)
if not song_path.is_file():
print("'{}' is orphaned!".format(song))
else:
print("applied correction to '{}'".format(song))
out += song + "\n"
if args.output_file:
with open(args.output_file, "w") as file:
file.write(out)
else:
print("Nothing to write")
if __name__ == "__main__":
main()