forked from ArthurBeaulieu/OstrichRemover
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOstrichRemover.py
470 lines (451 loc) · 22.6 KB
/
OstrichRemover.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
#!/usr/bin/env python3
# Python imports
import os
import sys
import argparse
import time
import datetime
import re
# Project imports
from src.models.folderInfo import FolderInfo
from src.scan.albumTester import AlbumTester
from src.fill.albumFiller import AlbumFiller
from src.clean.albumCleaner import AlbumCleaner
from src.analyze.metaAnalyzer import MetaAnalyzer
from src.stat.statMaker import StatMaker
from src.utils.tools import computePurity
from src.utils.reportBuilder import *
from src.utils.uiBuilder import *
from src.utils.tools import *
# Globals
global scriptVersion
scriptVersion = '1.6.1'
# Script main frame
def main():
# Init argparse arguments
ap = argparse.ArgumentParser()
ap.add_argument('folder', help='The input folder path to crawl (absolute or relative)')
# Main modes
ap.add_argument('-s', '--scan', help='Scan a folder to test it against naming convention', action='store_true')
ap.add_argument('-f', '--fill', help='Prefill tags with folder name and file name information', action='store_true')
ap.add_argument('-a', '--analyze', help='Analyze a folder of JSON dumps to make a meta analysis', action='store_true')
ap.add_argument('-t', '--stat', help='Aggregates stats about a given library', action='store_true')
ap.add_argument('-g', '--gen', help='Generates a JSON for each release artist', action='store_true')
# Additional modes
ap.add_argument('-c', '--clean', help='Clean all previously set tags, and ambiguous ones', action='store_true')
# Arguments
ap.add_argument('-d', '--dump', help='Dump JSON in ./dump folder by default, see -p for custom output folder', action='store_true')
ap.add_argument('-m', '--minify', help='Minify the JSON output', action='store_true')
ap.add_argument('-e', '--errors', help='Log errors only during run', action='store_true')
ap.add_argument('-v', '--verbose', help='Log detailed progress when running', action='store_true')
ap.add_argument('-p', '--path', help='The output path to store the dumped JSON', type=os.path.abspath)
args = vars(ap.parse_args())
# Preventing path from missing its trailing slash (or backslash for win compatibility)
if not args['folder'].endswith('\\') and not args['folder'].endswith('/'):
printInvalidPath(args['folder'])
sys.exit(-1)
# Exec script
printCredentials(scriptVersion)
# Perform a scan for the given folder against the naming convention
if args['scan']:
scanFolder(args)
# Pre-fill folder's track tags with information held in folder name and file name
elif args['fill']:
fillTags(args)
# Make a meta analysis of previously made scan to compile values
elif args['analyze']:
metaAnalysis(args)
# Make a meta analysis of previously made scan to compile values
elif args['stat']:
extractStats(args)
# Create a JSON file for all release artists and genre founds
elif args['gen']:
generateJSON(args)
# Clean all previously set tags (to prepare a track to be properly filled)
elif args['clean']:
if queryYesNo('> Warning, this command will erase any previously existing tags on audio files in this path. Just do it?', 'yes'):
printLineBreak()
cleanTags(args)
# Otherwise print an error message (missing arguments)
else:
printMissingArguments()
# Clean the tmp.jpg image that could resides from fill or scan
if os.path.exists('tmp.jpg'):
os.remove('tmp.jpg') # GC
# Will crawl the folder path given in argument, and all its sub-directories
def scanFolder(args):
# Retrieve folder global information
printRetrieveFolderInfo()
folderInfo = FolderInfo(args['folder'])
printRootFolderInfo(folderInfo)
# Scan internals
totalTracks = folderInfo.flacCounter + folderInfo.mp3Counter
rootPathLength = len(args['folder'].split(os.sep))
scannedTracks = 0
errorCounter = 0
albumTesters = []
# Scan progression utils
step = 10
percentage = step
previousLetter = '1' # Ordered folder/file parsing begins with numbers
# Start scan
printScanStart(args['folder'], totalTracks, len(ErrorEnum))
startTime = time.time()
# Sort directories so they are handled in the alphabetical order
for root, directories, files in sorted(os.walk(args['folder'])):
files = [f for f in files if not f[0] == '.'] # Ignore hidden files
directories[:] = [d for d in directories if not d[0] == '.'] # ignore hidden directories
# Split root into an array of folders
path = root.split(os.sep)
# Mutagen needs a preserved path when using ID3() or FLAC()
preservedPath = list(path)
# Poping all path element that are not the root folder, the artist sub folder or the album sub sub folder
for x in range(rootPathLength - 1):
path.pop(0)
# Current path is for an album directory : perform tests
if len(path) == 2 and path[1] != '':
albumTester = AlbumTester(files, preservedPath)
scannedTracks += albumTester.album.totalTrack
errorCounter += albumTester.tracksErrorCounter()
errorCounter += albumTester.errorCounter
albumTesters.append(albumTester)
# Display a progress every step %
scannedPercentage = (scannedTracks * 100) / totalTracks
if totalTracks > 10 and scannedPercentage >= step and scannedTracks < totalTracks:
if (scannedTracks * 100) / totalTracks > percentage and percentage < 100:
printScanProgress(percentage, previousLetter, path[0][0], errorCounter, scannedTracks,
computePurity(errorCounter, scannedTracks))
percentage += step
previousLetter = path[0][0] # path[0] is the Artists name
# In this case, ui has display a percentage progression. No need to add a line break if no progression is to be displayed
if totalTracks > 10 and percentage != 10:
printLineBreak()
duration = round(time.time() - startTime, 2)
printScanEnd(duration, errorCounter, totalTracks, computePurity(errorCounter, scannedTracks))
# Compute and save JSON report
if args['dump']:
saveReportFile(computeFillReport(scriptVersion, duration, folderInfo, albumTesters, errorCounter,
computePurity(errorCounter, scannedTracks)), 'Errors', args['minify'], args['path'])
# Verbose report
if args['verbose']:
printErroredTracksReport(albumTesters)
# Will pre-fill the tags for tracks in the given folder
def fillTags(args):
# Retrieve folder global information
printRetrieveFolderInfo()
folderInfo = FolderInfo(args['folder'])
printRootFolderInfo(folderInfo)
# Fill internals
filledTracks = 0
totalTracks = folderInfo.flacCounter + folderInfo.mp3Counter
# Start Fill
printFillStart(args['folder'], totalTracks)
rootPathLength = len(args['folder'].split(os.sep))
albumFillers = []
# Fill progression utils
step = 10
percentage = step
startTime = time.time()
# Sort directories so they are handled in the alphabetical order
for root, directories, files in sorted(os.walk(args['folder'])):
files = [f for f in files if not f[0] == '.'] # Ignore hidden files
directories[:] = [d for d in directories if not d[0] == '.'] # ignore hidden directories
# Split root into an array of folders
path = root.split(os.sep)
# Mutagen needs a preserved path when using ID3() or FLAC()
preservedPath = list(path)
# Poping all path element that are not the root folder, the artist sub folder or the album sub sub folder
for x in range(rootPathLength - 1):
path.pop(0)
# Current path is for an album directory : perform tests
if len(path) == 2 and path[1] != '':
albumFiller = AlbumFiller(files, preservedPath, args['verbose'], args['errors'])
albumFillers.append(albumFiller)
if albumFiller.hasErrors is False:
filledTracks += albumFiller.album.totalTrack
# Display a progress every step %
fillPercentage = (filledTracks * 100) / totalTracks
if totalTracks > 10 and fillPercentage >= step and filledTracks < totalTracks:
if (filledTracks * 100) / totalTracks > percentage and percentage < 100:
printFillProgress(percentage, filledTracks)
percentage += step
# Couldn't fill all track because of naming error
if totalTracks is not filledTracks:
printInvalidFolderStructure(filledTracks, totalTracks, 'fill')
# In this case, ui has display a percentage progression. No need to add a line break if no progression is to be displayed
if totalTracks > 10 and percentage != 10: # If more than 10 tracks and percentage have been displayed (if % = 10, it is its init value)
printLineBreak()
duration = round(time.time() - startTime, 2)
printFillEnd(duration, filledTracks)
# Will make a JSON file with compiled results from input path that contains JSON dumps (from --dump)
def metaAnalysis(args):
# Get JSON files in arg folder
jsonFiles = [ file for file in os.listdir(args['folder']) if file.endswith('.json') ]
# Start analyze
printAnalyzeStart(args['folder'], len(jsonFiles))
startTime = time.time()
# Generate MetaAnalyzer object from this JSON list
metaAnalyzer = MetaAnalyzer(sorted(jsonFiles), args['folder'])
printAnalyzeStatus(metaAnalyzer)
duration = round(time.time() - startTime, 2)
printAnalyzeEnd(duration, len(jsonFiles))
# Compute and save JSON report
if args['dump']:
saveReportFile(computeMetaAnalyzeReport(scriptVersion, duration, metaAnalyzer), 'Meta-Analyze', args['minify'], args['path'])
# This method will crawl an audio library and save all its main information (labels, artists, genres)
def extractStats(args):
# Retrieve folder global information
printRetrieveFolderInfo()
folderInfo = FolderInfo(args['folder'])
printRootFolderInfo(folderInfo)
# Stat internals
artists = []
genres = []
labels = []
analyzedTracks = 0
totalTracks = folderInfo.flacCounter + folderInfo.mp3Counter
# Analyze progression utils
printStatStart(args['folder'], totalTracks)
step = 10
percentage = step
startTime = time.time()
# Sort directories so they are handled in the alphabetical order
for root, directories, files in sorted(os.walk(args['folder'])):
files = [f for f in files if not f[0] == '.'] # Ignore hidden files
directories[:] = [d for d in directories if not d[0] == '.'] # ignore hidden directories
# Split root into an array of folders
path = root.split(os.sep)
# Mutagen needs a preserved path when using ID3() or FLAC()
preservedPath = list(path)
rootPathLength = len(args['folder'].split(os.sep))
# Poping all path element that are not the root folder, the artist sub folder or the album sub sub folder
for x in range(rootPathLength - 1):
path.pop(0)
# Current path is for an album directory : perform tests
if len(path) == 2 and path[1] != '':
albumStats = StatMaker(files, preservedPath)
artists = insertArtistInListIfNotExisting(artists, albumStats.artistsDetails)
genres = insertInListIfNotExisting(genres, albumStats.genres)
labels = insertInListIfNotExisting(labels, albumStats.labels)
analyzedTracks += len(albumStats.tracks)
# Display a progress every step %
fillPercentage = (analyzedTracks * 100) / totalTracks
if totalTracks > 10 and fillPercentage >= step and analyzedTracks < totalTracks:
if (analyzedTracks * 100) / totalTracks > percentage and percentage < 100:
printStatProgress(percentage, analyzedTracks)
percentage += step
# In this case, ui has display a percentage progression. No need to add a line break if no progression is to be displayed
if totalTracks > 10 and percentage != 10:
printLineBreak()
duration = round(time.time() - startTime, 2)
printStatEnd(duration, analyzedTracks)
# Compute and save JSON report
if args['dump']:
saveReportFile(computeStatReport(scriptVersion, duration, artists, genres, labels, folderInfo.folder), 'Stats', args['minify'], args['path'])
else:
print(artists)
print(genres)
print(labels)
# Thuis method will crawl the audio library and generate for each unique artist and genre a JSON file for ManaZeak assets
def generateJSON(args):
# Retrieve folder global information
printRetrieveFolderInfo()
folderInfo = FolderInfo(args['folder'])
printRootFolderInfo(folderInfo)
# Scan internals
totalTracks = folderInfo.flacCounter + folderInfo.mp3Counter
rootPathLength = len(args['folder'].split(os.sep))
parsedTracks = 0
errorCounter = 0
albumTesters = []
artists = []
artistsAlbums = {}
genres = []
genresAlbums = {}
labels = []
labelsAlbums = {}
# Scan progression utils
step = 10
percentage = step
previousLetter = '1' # Ordered folder/file parsing begins with numbers
# Start scan
printGenerationStart(totalTracks, args['path'])
startTime = time.time()
# Sort directories so they are handled in the alphabetical order
for root, directories, files in sorted(os.walk(args['folder'])):
files = [f for f in files if not f[0] == '.'] # Ignore hidden files
directories[:] = [d for d in directories if not d[0] == '.'] # ignore hidden directories
# Split root into an array of folders
path = root.split(os.sep)
# Mutagen needs a preserved path when using ID3() or FLAC()
preservedPath = list(path)
# Poping all path element that are not the root folder, the artist sub folder or the album sub sub folder
for x in range(rootPathLength - 1):
path.pop(0)
# Current path is for an album directory : perform tests
if len(path) == 2 and path[1] != '':
albumTester = AlbumTester(files, preservedPath)
parsedTracks += albumTester.album.totalTrack
# Artists section
# Append release artist if not already added
if not albumTester.album.albumArtist in artists:
artists.append(removeSpecialCharFromString(albumTester.album.albumArtist))
if not albumTester.album.albumArtist in artistsAlbums:
artistsAlbums[removeSpecialCharFromString(albumTester.album.albumArtist)] = {
'albumArtist': [albumTester.album],
'artist': [],
'performer': [],
'producer': [],
'composer': []
}
else:
artistsAlbums[removeSpecialCharFromString(albumTester.album.albumArtist)]['albumArtist'].append(albumTester.album)
# Label filling
if not albumTester.album.label in labels:
labels.append(removeSpecialCharFromString(albumTester.album.label))
if not albumTester.album.label in labelsAlbums:
labelsAlbums[removeSpecialCharFromString(albumTester.album.label)] = [albumTester.album]
else:
labelsAlbums[removeSpecialCharFromString(albumTester.album.label)].append(albumTester.album)
# Iterate over tracks now
for track in albumTester.tracks:
for artist in track.track.artists:
if not artist in artists:
artists.append(removeSpecialCharFromString(artist))
if not artist in artistsAlbums:
artistsAlbums[removeSpecialCharFromString(artist)] = {
'albumArtist': [],
'artist': [albumTester.album],
'performer': [],
'producer': [],
'composer': []
}
else:
artistsAlbums[removeSpecialCharFromString(artist)]['artist'].append(albumTester.album)
for performer in track.track.performers:
if not performer in artists:
artists.append(removeSpecialCharFromString(performer))
if not performer in artistsAlbums:
artistsAlbums[removeSpecialCharFromString(performer)] = {
'albumArtist': [],
'artist': [],
'performer': [albumTester.album],
'producer': [],
'composer': []
}
else:
artistsAlbums[removeSpecialCharFromString(performer)]['performer'].append(albumTester.album)
for producer in track.track.producers:
if not producer in artists:
artists.append(removeSpecialCharFromString(producer))
if not producer in artistsAlbums:
artistsAlbums[removeSpecialCharFromString(producer)] = {
'albumArtist': [],
'artist': [],
'performer': [],
'producer': [albumTester.album],
'composer': []
}
else:
artistsAlbums[removeSpecialCharFromString(producer)]['producer'].append(albumTester.album)
for composer in track.track.composers:
c = re.sub(r' \([^()]*\)', '', composer)
realName = re.search(r'\((.*?)\)', composer)
if not c in artists:
artists.append(removeSpecialCharFromString(c))
if not c in artistsAlbums:
artistsAlbums[removeSpecialCharFromString(c)] = {
'albumArtist': [],
'artist': [],
'performer': [],
'producer': [],
'composer': [albumTester.album]
}
else:
artistsAlbums[removeSpecialCharFromString(c)]['composer'].append(albumTester.album)
if realName != None and realName.group(1).find(',') == -1:
artistsAlbums[removeSpecialCharFromString(c)]['realName'] = removeSpecialCharFromString(realName.group(1))
# Updating genre
for genre in track.track.genres:
if not genre in genres:
genres.append(genre)
if not genre in genresAlbums:
genresAlbums[genre] = [albumTester.album]
else:
genresAlbums[genre].append(albumTester.album)
# Display a progress every step %
scannedPercentage = (parsedTracks * 100) / totalTracks
if totalTracks > 10 and scannedPercentage >= step and parsedTracks < totalTracks:
if (parsedTracks * 100) / totalTracks > percentage and percentage < 100:
printGenerationProgress(percentage, parsedTracks)
percentage += step
# In this case, ui has display a percentage progression. No need to add a line break if no progression is to be displayed
if totalTracks > 10 and percentage != 10:
printLineBreak()
duration = round(time.time() - startTime, 2)
printGenerationEnd(duration, len(artists), len(genres), len(labels))
# Compute and save JSON report
if args['path']:
saveGeneratedJSONFile(artists, artistsAlbums, 'artists', args['path'])
saveGeneratedJSONFile(genres, genresAlbums, 'genres', args['path'])
saveGeneratedJSONFile(labels, labelsAlbums, 'labels', args['path'])
# Perform cleaning on files (remove all not in lists)
for filename in os.listdir(args['path'] + '/artists/txt'):
if filename[:-5] not in artists:
os.remove(args['path'] + '/artists/txt/' + filename)
for filename in os.listdir(args['path'] + '/genres/txt'):
if filename[:-5] not in genres:
os.remove(args['path'] + '/genres/txt/' + filename)
for filename in os.listdir(args['path'] + '/labels/txt'):
if filename[:-5] not in labels:
os.remove(args['path'] + '/labels/txt/' + filename)
# Verbose report
if args['verbose']:
printErroredTracksReport(albumTesters)
# This method will clear ever tags in audio files for the scanned folder
def cleanTags(args):
# Retrieve folder global information
printRetrieveFolderInfo()
folderInfo = FolderInfo(args['folder'])
printRootFolderInfo(folderInfo)
# Fill internals
cleanedTracks = 0
totalTracks = folderInfo.flacCounter + folderInfo.mp3Counter
# Start Fill
printCleanStart(args['folder'], totalTracks)
rootPathLength = len(args['folder'].split(os.sep))
albumCleaners = []
# Fill progression utils
step = 10
percentage = step
startTime = time.time()
# Sort directories so they are handled in the alphabetical order
for root, directories, files in sorted(os.walk(args['folder'])):
files = [f for f in files if not f[0] == '.'] # Ignore hidden files
directories[:] = [d for d in directories if not d[0] == '.'] # ignore hidden directories
# Split root into an array of folders
path = root.split(os.sep)
# Mutagen needs a preserved path when using ID3() or FLAC()
preservedPath = list(path)
# Poping all path element that are not the root folder, the artist sub folder or the album sub sub folder
for x in range(rootPathLength - 1):
path.pop(0)
# Current path is for an album directory : perform tests
if len(path) == 2 and path[1] != '':
albumCleaner = AlbumCleaner(files, preservedPath)
albumCleaners.append(albumCleaner)
cleanedTracks += albumCleaner.album.totalTrack
# Display a progress every step %
cleanPercentage = (cleanedTracks * 100) / totalTracks
if totalTracks > 10 and cleanPercentage >= step:
if (cleanedTracks * 100) / totalTracks > percentage and percentage < 100:
printCleanProgress(percentage, cleanedTracks)
percentage += step
# In this case, ui has display a percentage progression. No need to add a line break if no progression is to be displayed
if totalTracks > 10:
printLineBreak()
duration = round(time.time() - startTime, 2)
printCleanEnd(duration, cleanedTracks)
# Script start point
if __name__ == '__main__':
main()