feat(analyzer): enhance anime detection with accent normalization and flexible matching
Build / Lint & Test (push) Successful in 19s

This commit is contained in:
PickleRick committed 2026-10-06 09:34:19 +02:00
1 parent c46e3dfdb3
commit 2ea83ae566
5 files changed
+143 -12

No files matched your search

@@ -47,9 +47,11 @@ const copyWithProgress = async ({
fs.mkdirSync(destDir, { recursive: true });
}
const displayFile = (source || '').replace(/\\/g, '/');
const report = (percent, message, eta) => {
if (typeof updateWorker === 'function') {
updateWorker({
file: displayFile,
percentage: Math.min(percent, 100),
ETA: eta || '00:00:00',
});
@@ -125,6 +127,7 @@ const copyWithProgress = async ({
if (typeof updateWorker === 'function') {
updateWorker({
file: displayFile,
percentage: percent,
ETA: eta,
});
@@ -249,7 +252,7 @@ const plugin = async (args) => {
const inputFile = inputFileObj._id;
const ext = path.extname(inputFile);
const baseName = path.basename(inputFile, ext);
const localCopy = path.join(workDir, `${baseName}_old${ext}`);
const localCopy = path.join(workDir, `${baseName}_old${ext}`).replace(/\\/g, '/');
const skipIfLocal = (args.inputs.skipIfLocal || 'true') === 'true';
jobLog(`[copyToWorkDir] Source : ${inputFile}\n`);
@@ -1,4 +1,4 @@
'use strict';
'use strict';
/* eslint-disable no-param-reassign */
@@ -89,6 +89,37 @@ const details = () => ({
],
});
const normalizeAccents = (str) =>
String(str || '')
.normalize('NFD')
.replace(/[\u0300-\u036f]/g, '');
const matchesAnime = (animeName, targetPath) => {
if (!animeName || !targetPath) return false;
const cleanTarget = normalizeAccents(targetPath).toLowerCase();
const cleanName = normalizeAccents(animeName).trim().toLowerCase();
if (!cleanName) return false;
// 1. Direct substring match (case/accent-insensitive)
if (cleanTarget.includes(cleanName)) return true;
// 2. Token / word boundary match allowing dots, dashes, underscores, spaces between words
// e.g. "Family Guy" matches "Family.Guy.1999" or "Family_Guy_S01E01"
const words = cleanName.split(/[^a-z0-9]+/).filter(Boolean);
if (words.length > 0) {
const escapedWords = words.map((w) => w.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'));
const flexiblePattern = new RegExp(`(^|[^a-z0-9])${escapedWords.join('[\\s._\\-]+')}([^a-z0-9]|$)`, 'i');
if (flexiblePattern.test(cleanTarget)) return true;
// 3. Match when punctuation/spaces/dots were completely collapsed (e.g. "bob's" -> "bobs")
const collapsedName = cleanName.replace(/[^a-z0-9]/g, '');
const collapsedTarget = cleanTarget.replace(/[^a-z0-9]/g, '');
if (collapsedName.length >= 3 && collapsedTarget.includes(collapsedName)) return true;
}
return false;
};
const parseRate = (rateStr) => {
if (!rateStr) return 0;
if (rateStr.includes('/')) {
@@ -133,14 +164,29 @@ const plugin = async (args) => {
const gopSceneCut = '40';
jobLog(`[mediaAnalyzerGOP] keyint=${gopKeyint} min-keyint=${gopMinKeyint} scenecut=${gopSceneCut}\n`);
// Normalize to forward slashes for cross-platform path matching
const normalizedPath = filePath.replace(/\\/g, '/');
const isMovie = moviePathPatterns.some((p) => normalizedPath.includes(p));
const matchedPat = moviePathPatterns.find((p) => normalizedPath.includes(p));
// Build candidate paths list to support workDir temporary copies
const candidatePaths = [
inputFileObj._id,
inputFileObj.file,
variables.originalLibraryFile,
variables.originalLibraryDir,
]
.filter(Boolean)
.map((p) => String(p).replace(/\\/g, '/'));
jobLog(`[mediaAnalyzerGOP] Candidate paths: ${candidatePaths.join(' | ')}\n`);
// Check if media matches movie path patterns
const matchedPat = moviePathPatterns.find((p) =>
candidatePaths.some((cPath) => cPath.toLowerCase().includes(p.toLowerCase())),
);
const isMovie = !!matchedPat;
jobLog(`[mediaAnalyzerGOP] isMovie=${isMovie}${matchedPat ? ` ("${matchedPat}")` : ''}\n`);
const lowerPath = normalizedPath.toLowerCase();
const matchedAnime = animeList.find((name) => lowerPath.includes(name.toLowerCase()));
// Check if media matches anime series list
const matchedAnime = animeList.find((name) =>
candidatePaths.some((cPath) => matchesAnime(name, cPath)),
);
const isAnimation = !!matchedAnime;
jobLog(`[mediaAnalyzerGOP] isAnimation=${isAnimation}${matchedAnime ? ` ("${matchedAnime}")` : ''}\n`);
@@ -173,3 +219,6 @@ const plugin = async (args) => {
module.exports.details = details;
module.exports.plugin = plugin;
module.exports.matchesAnime = matchesAnime;
module.exports.normalizeAccents = normalizeAccents;
@@ -47,9 +47,11 @@ const copyWithProgress = async ({
fs.mkdirSync(destDir, { recursive: true });
}
const displayFile = (source || '').replace(/\\/g, '/');
const report = (percent, message, eta) => {
if (typeof updateWorker === 'function') {
updateWorker({
file: displayFile,
percentage: Math.min(percent, 100),
ETA: eta || '00:00:00',
});
@@ -125,6 +127,7 @@ const copyWithProgress = async ({
if (typeof updateWorker === 'function') {
updateWorker({
file: displayFile,
percentage: percent,
ETA: eta,
});