-
Notifications
You must be signed in to change notification settings - Fork 0
/
drRoon.py
278 lines (250 loc) · 11.6 KB
/
drRoon.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import os
import shutil
import re
import logging
import argparse
from mutagen.id3 import ID3, TXXX
from mutagen.flac import FLAC
from mutagen.mp4 import MP4
from mutagen.dsf import DSF
from mutagen import MutagenError
# Set up logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
def safe_rename(old_path, new_path):
if os.path.exists(new_path):
base, extension = os.path.splitext(new_path)
counter = 1
while os.path.exists(f"{base}_{counter}{extension}"):
counter += 1
new_path = f"{base}_{counter}{extension}"
try:
shutil.move(old_path, new_path)
logger.info(f"Renamed '{old_path}' to '{new_path}'")
except OSError as e:
logger.error(f"Error renaming '{old_path}' to '{new_path}': {str(e)}")
def get_dr_value(dr_file_path):
try:
with open(dr_file_path, 'r', encoding='utf-8') as f:
content = f.read()
match = re.search(r'Official DR value: DR(\d+)', content)
if match:
return match.group(1)
else:
logger.warning(f"No DR value found in {dr_file_path}")
return None
except FileNotFoundError:
logger.warning(f"{dr_file_path} not found")
return None
except IOError as e:
logger.error(f"Error reading {dr_file_path}: {str(e)}")
return None
def process_directory(directory, dr_value, metadata_choice):
success = True
if metadata_choice != "4":
success = update_metadata_in_directory(directory, dr_value, metadata_choice)
return success
def update_metadata_in_directory(directory, dr_value, metadata_choice):
success = True
for filename in os.listdir(directory):
if filename.startswith('.'):
continue
filepath = os.path.join(directory, filename)
try:
if filepath.lower().endswith(('.mp3', '.flac', '.m4a', '.dsf')):
if filepath.lower().endswith('.mp3'):
audio = ID3(filepath)
updated = update_id3_tags(audio, dr_value, metadata_choice)
elif filepath.lower().endswith('.flac'):
audio = FLAC(filepath)
updated = update_flac_tags(audio, dr_value, metadata_choice)
elif filepath.lower().endswith('.m4a'):
audio = MP4(filepath)
updated = update_mp4_tags(audio, dr_value, metadata_choice)
elif filepath.lower().endswith('.dsf'):
audio = DSF(filepath)
updated = update_dsf_tags(audio, dr_value, metadata_choice)
if updated:
audio.save()
logger.info(f"Updated metadata for {filename}")
else:
logger.info(f"No changes needed for {filename}")
else:
logger.debug(f"Skipped unsupported file type: {filename}")
except MutagenError as e:
logger.error(f"Error updating metadata for {filename}: {str(e)}")
success = False
except IOError as e:
logger.error(f"Error reading/writing file {filename}: {str(e)}")
success = False
return success
def update_dr_value(existing_value, dr_value, tag_type):
dr_text = f"DR {dr_value}"
separator = '; ' if tag_type == 'ROONALBUMTAG' else ', '
if not existing_value:
return dr_text
existing_parts = [part.strip() for part in existing_value.rstrip(separator).split(separator)]
existing_dr = next((part for part in existing_parts if part.startswith('DR ')), None)
if existing_dr == dr_text:
return None # No change needed
new_parts = [part for part in existing_parts if not part.startswith('DR ')] + [dr_text]
return separator.join(new_parts)
def update_id3_tags(audio, dr_value, metadata_choice):
updated = False
if metadata_choice in ["1", "2"]:
if 'TXXX:VERSION' in audio:
new_value = update_dr_value(audio['TXXX:VERSION'].text[0], dr_value, 'VERSION')
if new_value:
audio['TXXX:VERSION'].text = [new_value]
updated = True
else:
audio.add(TXXX(encoding=3, desc='VERSION', text=f"DR {dr_value}"))
updated = True
if metadata_choice in ["1", "3"]:
if 'TXXX:ROONALBUMTAG' in audio:
new_value = update_dr_value(audio['TXXX:ROONALBUMTAG'].text[0], dr_value, 'ROONALBUMTAG')
if new_value:
audio['TXXX:ROONALBUMTAG'].text = [new_value]
updated = True
else:
audio.add(TXXX(encoding=3, desc='ROONALBUMTAG', text=f"DR {dr_value}"))
updated = True
return updated
def update_flac_tags(audio, dr_value, metadata_choice):
updated = False
if metadata_choice in ["1", "2"]:
if 'VERSION' in audio:
new_value = update_dr_value(audio['VERSION'][0], dr_value, 'VERSION')
if new_value:
audio['VERSION'] = [new_value]
updated = True
else:
audio['VERSION'] = [f"DR {dr_value}"]
updated = True
if metadata_choice in ["1", "3"]:
if 'ROONALBUMTAG' in audio:
new_value = update_dr_value(audio['ROONALBUMTAG'][0], dr_value, 'ROONALBUMTAG')
if new_value:
audio['ROONALBUMTAG'] = [new_value]
updated = True
else:
audio['ROONALBUMTAG'] = [f"DR {dr_value}"]
updated = True
return updated
def update_mp4_tags(audio, dr_value, metadata_choice):
updated = False
if metadata_choice in ["1", "2"]:
tag = '----:com.apple.iTunes:VERSION'
if tag in audio:
new_value = update_dr_value(audio[tag][0].decode('utf-8'), dr_value, 'VERSION')
if new_value:
audio[tag] = [new_value.encode('utf-8')]
updated = True
else:
audio[tag] = [f"DR {dr_value}".encode('utf-8')]
updated = True
if metadata_choice in ["1", "3"]:
tag = '----:com.apple.iTunes:ROONALBUMTAG'
if tag in audio:
new_value = update_dr_value(audio[tag][0].decode('utf-8'), dr_value, 'ROONALBUMTAG')
if new_value:
audio[tag] = [new_value.encode('utf-8')]
updated = True
else:
audio[tag] = [f"DR {dr_value}".encode('utf-8')]
updated = True
return updated
def update_dsf_tags(audio, dr_value, metadata_choice):
updated = False
if audio.tags is None:
audio.add_tags()
id3 = audio.tags
if metadata_choice in ["1", "2"]:
if 'TXXX:VERSION' in id3:
new_value = update_dr_value(id3['TXXX:VERSION'].text[0], dr_value, 'VERSION')
if new_value:
id3['TXXX:VERSION'].text = [new_value]
updated = True
else:
id3.add(TXXX(encoding=3, desc='VERSION', text=f"DR {dr_value}"))
updated = True
if metadata_choice in ["1", "3"]:
if 'TXXX:ROONALBUMTAG' in id3:
new_value = update_dr_value(id3['TXXX:ROONALBUMTAG'].text[0], dr_value, 'ROONALBUMTAG')
if new_value:
id3['TXXX:ROONALBUMTAG'].text = [new_value]
updated = True
else:
id3.add(TXXX(encoding=3, desc='ROONALBUMTAG', text=f"DR {dr_value}"))
updated = True
return updated
def find_missing_logfiles(root_folder):
music_extensions = ('.mp3', '.dsf', '.flac', '.m4a')
required_files = ('foo_dr.txt', 'foo_dr_processed.txt')
for dirpath, dirnames, filenames in os.walk(root_folder):
has_music_files = any(filename.lower().endswith(music_extensions) for filename in filenames)
has_required_file = any(required_file.lower() in [f.lower() for f in filenames] for required_file in required_files)
if has_music_files and not has_required_file:
print(f"Album folder missing required logfile: {dirpath}")
def main():
parser = argparse.ArgumentParser(description="Process DR values in audio files and directories.")
parser.add_argument("root_dir", help="Root directory to process")
parser.add_argument("--folder-score", choices=["0", "1"], help="Add DR score to album folder name (0: No, 1: Yes)")
parser.add_argument("--metadata", choices=["0", "1", "2", "3"], help="Add DR score to metadata (0: No, 1: Both VERSION and ROONALBUMTAG, 2: VERSION only, 3: ROONALBUMTAG only)")
parser.add_argument("--rename-logfile", choices=["0", "1"], help="Rename foo_dr.txt after processing (0: No, 1: Yes)")
parser.add_argument("--find-missing", action="store_true", help="Find folder albums missing the required logfiles")
args = parser.parse_args()
root_dir = args.root_dir
if not os.path.isdir(root_dir):
logger.error(f"The specified directory '{root_dir}' does not exist.")
return
if args.find_missing:
find_missing_logfiles(root_dir)
return
folder_name_choice = args.folder_score
if folder_name_choice is None:
print("Would you like to add the DR score to the album folder name?")
print("0. No (default)")
print("1. Yes")
folder_name_choice = input("Enter your choice (0 or 1): ").strip() or "0"
metadata_choice = args.metadata
if metadata_choice is None:
print("Would you like to add the DR score to metadata?")
print("0. No")
print("1. Yes, to both the VERSION and ROONALBUMTAG tags (default)")
print("2. Yes, to the VERSION tag only")
print("3. Yes, to the ROONALBUMTAG tag only")
metadata_choice = input("Enter your choice (0, 1, 2, or 3): ").strip() or "1"
rename_dr_file_choice = args.rename_logfile
if rename_dr_file_choice is None:
print("Would you like to rename foo_dr.txt to foo_dr_processed.txt after successful processing?")
print("0. No")
print("1. Yes (default)")
rename_dr_file_choice = input("Enter your choice (0 or 1): ").strip() or "1"
for root, dirs, files in os.walk(root_dir):
dirs[:] = [d for d in dirs if not d.startswith('.')]
if 'foo_dr.txt' in files:
dr_file_path = os.path.join(root, 'foo_dr.txt')
dr_value = get_dr_value(dr_file_path)
if dr_value:
logger.info(f"Processing directory: {root}")
success = process_directory(root, dr_value, metadata_choice)
if success and rename_dr_file_choice == "1":
new_dr_file_path = os.path.join(root, 'foo_dr_processed.txt')
safe_rename(dr_file_path, new_dr_file_path)
if folder_name_choice == "1":
folder_name = os.path.basename(root)
current_dr = re.search(r'\(DR (\d+)\)', folder_name)
if current_dr:
if current_dr.group(1) != dr_value:
new_name = re.sub(r'\(DR \d+\)', f'(DR {dr_value})', folder_name)
new_path = os.path.join(os.path.dirname(root), new_name)
safe_rename(os.path.abspath(root), os.path.abspath(new_path))
else:
logger.info(f"Folder '{folder_name}' already contains correct DR value. Skipping rename.")
else:
new_name = f"{folder_name} (DR {dr_value})"
new_path = os.path.join(os.path.dirname(root), new_name)
safe_rename(os.path.abspath(root), os.path.abspath(new_path))
if __name__ == "__main__":
main()