Skip to content

Commit

Permalink
Merge pull request #22 from rombat/fix/patch-matching-all-cases
Browse files Browse the repository at this point in the history
fix: path matching to find tracks
  • Loading branch information
rombat authored Jul 28, 2024
2 parents e15c2a9 + bc70d90 commit 9163e0b
Show file tree
Hide file tree
Showing 8 changed files with 1,197 additions and 25 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ jobs:
run: npm install -g pkg
- name: Install dependencies
run: npx ci --include=dev
- name: Tests
run: npm run test
- name: Release
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
Expand Down
1 change: 1 addition & 0 deletions release.sh → build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ cp -r index.js package*.json lib .dist
cd .dist
npm install --omit=dev

echo "Building MBNDS exe..."
pkg -c package.json -t node18-win-x64 index.js -o musicbee-navidrome-sync.exe
20 changes: 10 additions & 10 deletions lib/handlers/MBNDSynchronizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const cliProgress = require('cli-progress');
const path = require('path');

const dbManager = require('./dbManager');
const { findBestMatch } = require('../helpers/helpers');
const packageJson = require('../../package.json');

class MBNDSynchronizer {
constructor(options, action) {
Expand Down Expand Up @@ -56,7 +58,7 @@ class MBNDSynchronizer {
initiate = async action => {
const { options, paths } = this;
if (Object.keys(options).length) {
console.log('Command running with following options:', options);
console.log(`MBNDS v${packageJson.version} running with following options:`, options);
}

if (action === 'fullSync') {
Expand Down Expand Up @@ -210,18 +212,13 @@ class MBNDSynchronizer {
musicBeeCollection.map(track =>
limit(async () => {
progressBar?.increment();
const filePathArray = track.filePath.split('\\');
const filePathEnd = `${filePathArray.length >= 2 ? filePathArray[filePathArray.length - 2] : ''}${dbFolderSeparator}${
track.folder
}${dbFolderSeparator}${track.filename}`;

const foundTrack = await Track.findOne({
const foundTracks = await Track.findAll({
where: {
[Op.and]: [
{ title: track.title },
{
path: {
[Op.endsWith]: `${filePathEnd}`
[Op.endsWith]: `${track.filename}`
}
}
]
Expand All @@ -236,15 +233,18 @@ class MBNDSynchronizer {
required: false
}
});
const foundTrack = findBestMatch(track, foundTracks);

if (!foundTrack) {
notFoundTracks.push(track);
if (options.verbose) {
console.error(`track not found: ${filePathEnd}`);
console.error(`track not found: ${track.filePath}`);
}
return;
}

if (options.verbose) {
console.log(`processing track: ${filePathEnd}`);
console.log(`processing track: ${track.filePath}`);
}

let annotation = foundTrack?.trackAnnotation;
Expand Down
50 changes: 50 additions & 0 deletions lib/helpers/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* Normalize path to use forward slashes
* @param {string} path
* @returns {string}
*/
const normalizePath = path => path.replace(/\\/g, '/');

/**
* Get path segments
* @param {string} path
* @returns {string[]}
*/
const getSegments = path => normalizePath(path).split('/');

/**
* Find best match for a given MusicBee track
* @param {object} mbTrack
* @param {object[]} ndTracks
* @returns {object|undefined}
*/
const findBestMatch = (mbTrack, ndTracks) => {
const mbTrackSegments = getSegments(mbTrack.filePath).reverse();
mbTrackSegments.unshift(mbTrack.filename);
let bestMatch;
let bestMatchScore = 0;

ndTracks.filter(Boolean).forEach(ndTrack => {
const ndTrackSegments = getSegments(ndTrack.toJSON().path).reverse();
let matchScore = 0;

if (mbTrackSegments[0] !== ndTrackSegments[0]) {
return;
}
for (let i = 1; i <= Math.min(mbTrackSegments.length, ndTrackSegments.length); i++) {
if (mbTrackSegments[i] === ndTrackSegments[i]) {
matchScore++;
} else {
break;
}
}
if (matchScore > bestMatchScore) {
bestMatchScore = matchScore;
bestMatch = ndTrack;
}
});

return bestMatch;
};

module.exports = { findBestMatch };
Loading

0 comments on commit 9163e0b

Please sign in to comment.