Update:OPF parser return array of authors and narrators without attempting to parse names #907

This commit is contained in:
advplyr 2022-08-12 17:30:05 -05:00
parent d15120eb5f
commit 1ad9ea92b6
2 changed files with 22 additions and 16 deletions

View File

@ -276,13 +276,18 @@ class Book {
if (opfMetadata.genres.length && (!this.metadata.genres.length || opfMetadataOverrideDetails)) {
metadataUpdatePayload[key] = opfMetadata.genres
}
} else if (key === 'author') {
if (opfMetadata.author && (!this.metadata.authors.length || opfMetadataOverrideDetails)) {
metadataUpdatePayload.authors = this.metadata.parseAuthorsTag(opfMetadata.author)
} else if (key === 'authors') {
if (opfMetadata.authors && opfMetadata.authors.length && (!this.metadata.authors.length || opfMetadataOverrideDetails)) {
metadataUpdatePayload.authors = opfMetadata.authors.map(authorName => {
return {
id: `new-${Math.floor(Math.random() * 1000000)}`,
name: authorName
}
})
}
} else if (key === 'narrator') {
if (opfMetadata.narrator && (!this.metadata.narrators.length || opfMetadataOverrideDetails)) {
metadataUpdatePayload.narrators = this.metadata.parseNarratorsTag(opfMetadata.narrator)
} else if (key === 'narrators') {
if (opfMetadata.narrators && opfMetadata.narrators.length && (!this.metadata.narrators.length || opfMetadataOverrideDetails)) {
metadataUpdatePayload.narrators = opfMetadata.narrators
}
} else if (key === 'series') {
if (opfMetadata.series && (!this.metadata.series.length || opfMetadataOverrideDetails)) {

View File

@ -15,10 +15,9 @@ function parseCreators(metadata) {
})
}
function fetchCreator(creators, role) {
function fetchCreators(creators, role) {
if (!creators || !creators.length) return null
var creator = creators.find(c => c.role === role)
return creator ? creator.value : null
return creators.filter(c => c.role === role).map(c => c.value)
}
function fetchTagString(metadata, tag) {
@ -80,11 +79,11 @@ function fetchVolumeNumber(metadataMeta) {
}
function fetchNarrators(creators, metadata) {
var roleNrt = fetchCreator(creators, 'nrt')
if (typeof metadata.meta == "undefined" || roleNrt != null) return roleNrt
var narrators = fetchCreators(creators, 'nrt')
if (typeof metadata.meta == "undefined" || narrators.length) return narrators
try {
var narratorsJSON = JSON.parse(fetchTagString(metadata.meta, "calibre:user_metadata:#narrators").replace(/"/g, '"'))
return narratorsJSON["#value#"].join(", ")
return narratorsJSON["#value#"]
} catch {
return null
}
@ -128,11 +127,13 @@ module.exports.parseOpfMetadataXML = async (xml) => {
})
}
var creators = parseCreators(metadata)
var data = {
const creators = parseCreators(metadata)
const authors = (fetchCreators(creators, 'aut') || []).filter(au => au && au.trim())
const narrators = (fetchNarrators(creators, metadata) || []).filter(nrt => nrt && nrt.trim())
const data = {
title: fetchTitle(metadata),
author: fetchCreator(creators, 'aut'),
narrator: fetchNarrators(creators, metadata),
authors,
narrators,
publishedYear: fetchDate(metadata),
publisher: fetchPublisher(metadata),
isbn: fetchISBN(metadata),