-
Notifications
You must be signed in to change notification settings - Fork 0
/
health_check.py
135 lines (119 loc) · 4.03 KB
/
health_check.py
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import os
import sys
import json
import pathlib
class Playlist:
def __init__(self, name, filepaths):
self.name = name
self.filepaths = [
pathlib.Path(p) if isinstance(p, str) else [pathlib.Path(p[0]), p[1]]
for p in filepaths
]
self.mp3_paths = []
self.cover_path = f"{self.name}.png"
self.cover_paths = []
for path in self.filepaths:
if isinstance(path, list):
path = path[0]
mp3_path = f"{self.name}_{path.stem}.mp3"
self.mp3_paths.append(mp3_path)
if path.suffix[1:].lower() in [
"mp4",
"webm",
"avi",
"mkv",
"mov",
"flv",
"wmv",
"m4v",
"3gp",
"mpeg",
"mpg",
"ogv",
"mts",
"ts",
"aac",
"m4a",
"wma",
"alac",
"amr",
"au",
"snd",
"mpc",
"tta",
"caf",
]:
mp3_path = f"{self.name}_{path.stem}.mp3"
self.mp3_paths.append(mp3_path)
cover_path = f"{self.name}_{path.stem}.png"
self.cover_paths.append(cover_path)
def check_iterate(playlists: list[Playlist], path, mode):
for playlist in playlists:
if mode == "cover":
if playlist.cover_path == path:
return True
elif mode == "mp3":
if path in playlist.mp3_paths:
return True
elif mode == "covers":
if path in playlist.cover_paths:
return True
return False
def main():
do_remove = len(sys.argv) > 1 and sys.argv[1] == "--remove"
data = []
if os.path.exists("data/playlists.json"):
with open("data/playlists.json", "r") as file:
data = json.load(file)
playlists = [Playlist(pdata["name"], pdata["paths"]) for pdata in data]
anyf1 = False
if os.path.exists("data/covers"):
for file in os.listdir("data/covers"):
if not check_iterate(playlists, file, "cover"):
path = f"data/covers/{file}"
if do_remove:
print(f"Removing unused playlist cover: '{path}'")
os.remove(path)
else:
print(f"Found unused playlist cover: '{path}'")
anyf1 = True
anyf2 = False
if os.path.exists("data/music_covers"):
for file in os.listdir("data/music_covers"):
if not check_iterate(playlists, file, "covers"):
path = f"data/music_covers/{file}"
if do_remove:
print(f"Removing unused music cover: '{path}'")
os.remove(path)
else:
print(f"Found unused music cover: '{path}'")
anyf2 = True
anyf3 = False
if os.path.exists("data/mp3_converted"):
for file in os.listdir("data/mp3_converted"):
if not check_iterate(playlists, file, "mp3"):
path = f"data/mp3_converted/{file}"
if do_remove:
print(f"Removing unused MP3 file: '{path}'")
os.remove(path)
else:
print(f"Found unused MP3 file: '{path}'")
anyf3 = True
if not anyf1 and not anyf2 and not anyf3:
print("No unused files found")
else:
if not anyf1:
print("No unused playlist covers found")
if not anyf2:
print("No unused music covers found")
if not anyf3:
print("No unused MP3 files found")
if anyf1 or anyf2 or anyf3:
if do_remove:
return
print()
print(
"Run health_check.py with the --remove cli argument to automatically delete the unused files"
)
if __name__ == "__main__":
main()