From 1ad9ea92b629eb137fc18fadf1f8dac7e5bf7e36 Mon Sep 17 00:00:00 2001 From: advplyr Date: Fri, 12 Aug 2022 17:30:05 -0500 Subject: [PATCH] Update:OPF parser return array of authors and narrators without attempting to parse names #907 --- server/objects/mediaTypes/Book.js | 17 +++++++++++------ server/utils/parsers/parseOpfMetadata.js | 21 +++++++++++---------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/server/objects/mediaTypes/Book.js b/server/objects/mediaTypes/Book.js index 8148f9d3..cb14292f 100644 --- a/server/objects/mediaTypes/Book.js +++ b/server/objects/mediaTypes/Book.js @@ -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)) { diff --git a/server/utils/parsers/parseOpfMetadata.js b/server/utils/parsers/parseOpfMetadata.js index 07fd305c..16d40409 100644 --- a/server/utils/parsers/parseOpfMetadata.js +++ b/server/utils/parsers/parseOpfMetadata.js @@ -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),