Add:Parse abmetadata metadata.abs file, save metadata.abs on all audiobook insert/update

This commit is contained in:
advplyr 2022-02-27 16:14:57 -06:00
parent 295c6b0c74
commit 779d22bf55
4 changed files with 131 additions and 18 deletions

View file

@ -45,4 +45,56 @@ function generate(audiobook, outputPath) {
return false
})
}
module.exports.generate = generate
module.exports.generate = generate
function parseAbMetadataText(text) {
if (!text) return null
var lines = text.split(/\r?\n/)
// Check first line and get abmetadata version number
var firstLine = lines.shift().toLowerCase()
if (!firstLine.startsWith(';abmetadata')) {
Logger.error(`Invalid abmetadata file first line is not ;abmetadata "${firstLine}"`)
return null
}
var abmetadataVersion = Number(firstLine.replace(';abmetadata', '').trim())
if (isNaN(abmetadataVersion)) {
Logger.warn(`Invalid abmetadata version ${abmetadataVersion} - using 1`)
abmetadataVersion = 1
}
// Remove comments and empty lines
const ignoreFirstChars = [' ', '#', ';'] // Ignore any line starting with the following
lines = lines.filter(line => !!line.trim() && !ignoreFirstChars.includes(line[0]))
// Get lines that map to book details (all lines before the first chapter section)
var firstSectionLine = lines.findIndex(l => l.startsWith('['))
var detailLines = firstSectionLine > 0 ? lines.slice(0, firstSectionLine) : lines
// Put valid book detail values into map
const bookDetails = {}
for (let i = 0; i < detailLines.length; i++) {
var line = detailLines[i]
var keyValue = line.split('=')
if (keyValue.length < 2) {
Logger.warn('abmetadata invalid line has no =', line)
} else if (!bookKeyMap[keyValue[0].trim()]) {
Logger.warn(`abmetadata key "${keyValue[0].trim()}" is not a valid book detail key`)
} else {
var key = keyValue[0].trim()
bookDetails[key] = keyValue[1].trim()
// Genres convert to array of strings
if (key === 'genres' && bookDetails[key]) {
bookDetails[key] = bookDetails[key].split(',').map(genre => genre.trim())
}
}
}
// TODO: Chapter support
return {
book: bookDetails
}
}
module.exports.parse = parseAbMetadataText