Scanner update - remove and update audiobooks on scans

This commit is contained in:
advplyr 2021-08-24 07:15:56 -05:00
parent db2f2d6660
commit c59cc52667
12 changed files with 239 additions and 65 deletions

View file

@ -78,7 +78,7 @@ function getTrackNumberFromFilename(filename) {
async function scanParts(audiobook, parts) {
if (!parts || !parts.length) {
Logger.error('Scan Parts', audiobook.title, 'No Parts', parts)
Logger.error('[AudioFileScanner] Scan Parts', audiobook.title, 'No Parts', parts)
return
}
var tracks = []
@ -87,7 +87,7 @@ async function scanParts(audiobook, parts) {
var scanData = await scan(fullPath)
if (!scanData || scanData.error) {
Logger.error('Scan failed for', parts[i])
Logger.error('[AudioFileScanner] Scan failed for', parts[i])
audiobook.invalidParts.push(parts[i])
continue;
}
@ -110,7 +110,7 @@ async function scanParts(audiobook, parts) {
if (parts.length > 1) {
trackNumber = isNumber(trackNumFromMeta) ? trackNumFromMeta : trackNumFromFilename
if (trackNumber === null) {
Logger.error('Invalid track number for', parts[i])
Logger.error('[AudioFileScanner] Invalid track number for', parts[i])
audioFileObj.invalid = true
audioFileObj.error = 'Failed to get track number'
continue;
@ -118,7 +118,7 @@ async function scanParts(audiobook, parts) {
}
if (tracks.find(t => t.index === trackNumber)) {
Logger.error('Duplicate track number for', parts[i])
Logger.error('[AudioFileScanner] Duplicate track number for', parts[i])
audioFileObj.invalid = true
audioFileObj.error = 'Duplicate track number'
continue;
@ -129,7 +129,7 @@ async function scanParts(audiobook, parts) {
}
if (!tracks.length) {
Logger.warn('No Tracks for audiobook', audiobook.id)
Logger.warn('[AudioFileScanner] No Tracks for audiobook', audiobook.id)
return
}
@ -148,26 +148,12 @@ async function scanParts(audiobook, parts) {
})
}
var parts_copy = tracks.map(p => ({ ...p }))
var current_index = 1
for (let i = 0; i < parts_copy.length; i++) {
var cleaned_part = parts_copy[i]
if (cleaned_part.index > current_index) {
var num_parts_missing = cleaned_part.index - current_index
for (let x = 0; x < num_parts_missing; x++) {
audiobook.missingParts.push(current_index + x)
}
}
current_index = cleaned_part.index + 1
}
if (audiobook.missingParts.length) {
Logger.info('Audiobook', audiobook.title, 'Has missing parts', audiobook.missingParts)
}
var hasTracksAlready = audiobook.tracks.length
tracks.forEach((track) => {
audiobook.addTrack(track)
})
if (hasTracksAlready) {
audiobook.tracks.sort((a, b) => a.index - b.index)
}
}
module.exports.scanParts = scanParts

View file

@ -1,3 +1,5 @@
const Path = require('path')
const levenshteinDistance = (str1, str2, caseSensitive = false) => {
if (!caseSensitive) {
str1 = str1.toLowerCase()
@ -44,4 +46,26 @@ module.exports.cleanString = cleanString
module.exports.isObject = (val) => {
return val !== null && typeof val === 'object'
}
function normalizePath(path) {
const replace = [
[/\\/g, '/'],
[/(\w):/, '/$1'],
[/(\w+)\/\.\.\/?/g, ''],
[/^\.\//, ''],
[/\/\.\//, '/'],
[/\/\.$/, ''],
[/\/$/, ''],
]
replace.forEach(array => {
while (array[0].test(path)) {
path = path.replace(array[0], array[1])
}
})
return path
}
module.exports.comparePaths = (path1, path2) => {
return (path1 === path2) || (normalizePath(path1) === normalizePath(path2))
}

View file

@ -69,7 +69,7 @@ async function getAllAudiobookFiles(abRootPath) {
title: title,
series: cleanString(series),
publishYear: publishYear,
path: relpath,
path: path,
fullPath: Path.join(abRootPath, path),
parts: [],
otherFiles: []