Skip to content

Instantly share code, notes, and snippets.

@glombek
Created April 17, 2024 16:33
Show Gist options
  • Save glombek/6fbb9dc3232207617788cc974858acf4 to your computer and use it in GitHub Desktop.
Save glombek/6fbb9dc3232207617788cc974858acf4 to your computer and use it in GitHub Desktop.
Similarity app custom script to select a file to mark from each complete grouping
/*
Author: Joe Glombek
Version: 1.1
Description:
Test script
Mark files in the ideal file format we expect.
*/
/// Return the one you want from the complete set of duplicates
function processGroup(currentSet) {
var matching = currentSet.filter(item => checkStructure(item));
if (matching.length == 0) {
//None match, just pick one
return key;
}
if (matching.length == 1) {
//Only one match, that's easy!
return matching[0];
}
//Both match, pick the longer path (it might include more detail, like a CD number)
const longestString = matching.reduce((longest, current) => {
return current.length > longest.length ? current : longest;
}, '');
return longestString;
}
function checkStructure(value) {
return /^M:\\(?<artist>[^\\]+)\\(?<album>[^\\]+)\\((?<cd>\d)\.)?(?<track>\d\d) - [^\\\.]+\..{1,3}$/gi.test(value);
}
// unmark all files
results.audio.unmark();
var dups = results.audio.dups;
var currentSet = [];
var best = {};
var keyLookup = {};
var sets = {};
for (var idx = 0; idx < dups.length; ++idx) {
// skip counter-pair (1-2 and 2-1), process pair only once
if (dups[idx].item1.path > dups[idx].item2.path) continue;
var key;
var keyA = keyLookup[dups[idx].item1.path];
var keyB = keyLookup[dups[idx].item2.path];
if (keyA == null && keyB == null) {
// First time!
key = dups[idx].item1.path;
sets[key] = [];
log(`New key: ${key}`);
}
else if (keyA != null && keyB != null) {
//merge both sets into keyA
key = keyA;
log(`${keyA} ${sets[keyA].length}`);
log(`${keyB} ${sets[keyB].length}`);
var combined = sets[keyA].concat(sets[keyB]);
log(`Combined: ${combined.length}`);
delete sets[keyB];
sets[key] = combined;
log(`${key} merged with ${keyB} - ${sets[key].length} total`);
}
else if (keyA) {
key = keyA;
}
else {
key = keyB;
}
var currentSet = sets[key];
//Add these to set
if (currentSet.indexOf(dups[idx].item1.path) < 0) {
currentSet.push(dups[idx].item1.path)
}
if (currentSet.indexOf(dups[idx].item2.path) < 0) {
currentSet.push(dups[idx].item2.path)
}
//update all keys
currentSet.forEach(i => {
keyLookup[i] = key;
log(`${i} => ${key}`);
});
var result = processGroup(currentSet);
best[key] = result;
}
currentSet = [];
for (var idx = 0; idx < dups.length; ++idx) {
// skip counter-pair (1-2 and 2-1), process pair only once
if (dups[idx].item1.path > dups[idx].item2.path) continue;
var key = keyLookup[dups[idx].item1.path];
var select = best[key];
if (!select) {
//we don't care about this set
log("------------");
log("❌ " + key);
continue;
}
//Logging for sanity
if (currentSet.length > 0 && currentSet[0] == dups[idx].item1.path) {
//This is the same set
currentSet.push(dups[idx].item2.path);
log((dups[idx].item2.path == select ? "*" : "") + dups[idx].item2.path);
}
else {
currentSet = [dups[idx].item1.path, dups[idx].item2.path];
log("------------");
log("🔑 " + key)
log((dups[idx].item1.path == select ? "*" : "") + dups[idx].item1.path);
log((dups[idx].item2.path == select ? "*" : "") + dups[idx].item2.path);
}
dups[idx].item1.marked = (dups[idx].item1.path == select);
dups[idx].item2.marked = (dups[idx].item2.path == select);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment