2022-05-29 18:46:45 +02:00
|
|
|
const { xmlToJSON } = require('../index')
|
|
|
|
const htmlSanitizer = require('../htmlSanitizer')
|
2021-11-09 03:05:12 +01:00
|
|
|
|
|
|
|
function parseCreators(metadata) {
|
|
|
|
if (!metadata['dc:creator']) return null
|
2023-01-02 17:47:13 +01:00
|
|
|
const creators = metadata['dc:creator']
|
2021-11-09 03:05:12 +01:00
|
|
|
if (!creators.length) return null
|
|
|
|
return creators.map(c => {
|
|
|
|
if (typeof c !== 'object' || !c['$'] || !c['_']) return false
|
|
|
|
return {
|
|
|
|
value: c['_'],
|
|
|
|
role: c['$']['opf:role'] || null,
|
|
|
|
fileAs: c['$']['opf:file-as'] || null
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-13 00:30:05 +02:00
|
|
|
function fetchCreators(creators, role) {
|
2021-11-09 03:05:12 +01:00
|
|
|
if (!creators || !creators.length) return null
|
2023-07-15 18:41:35 +02:00
|
|
|
return [...new Set(creators.filter(c => c.role === role).map(c => c.value))]
|
2021-11-09 03:05:12 +01:00
|
|
|
}
|
|
|
|
|
2021-11-10 00:54:28 +01:00
|
|
|
function fetchTagString(metadata, tag) {
|
|
|
|
if (!metadata[tag] || !metadata[tag].length) return null
|
2023-02-11 00:22:23 +01:00
|
|
|
let value = metadata[tag][0]
|
|
|
|
|
|
|
|
/*
|
|
|
|
EXAMPLES:
|
|
|
|
|
|
|
|
"dc:title": [
|
|
|
|
{
|
|
|
|
"_": "The Quest for Character",
|
|
|
|
"$": {
|
|
|
|
"opf:file-as": "Quest for Character What the Story of Socrates and Alcibiades"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
OR
|
|
|
|
|
|
|
|
"dc:title": [
|
|
|
|
"The Quest for Character"
|
|
|
|
]
|
|
|
|
*/
|
|
|
|
if (typeof value === 'object') value = value._
|
2023-01-02 17:47:13 +01:00
|
|
|
if (typeof value !== 'string') return null
|
|
|
|
return value
|
2021-11-10 00:54:28 +01:00
|
|
|
}
|
|
|
|
|
2021-11-09 03:05:12 +01:00
|
|
|
function fetchDate(metadata) {
|
2023-01-02 17:47:13 +01:00
|
|
|
const date = fetchTagString(metadata, 'dc:date')
|
2021-11-10 00:54:28 +01:00
|
|
|
if (!date) return null
|
2023-01-02 17:47:13 +01:00
|
|
|
const dateSplit = date.split('-')
|
2021-11-09 03:05:12 +01:00
|
|
|
if (!dateSplit.length || dateSplit[0].length !== 4 || isNaN(dateSplit[0])) return null
|
|
|
|
return dateSplit[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
function fetchPublisher(metadata) {
|
2021-11-10 00:54:28 +01:00
|
|
|
return fetchTagString(metadata, 'dc:publisher')
|
2021-11-09 03:05:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function fetchISBN(metadata) {
|
|
|
|
if (!metadata['dc:identifier'] || !metadata['dc:identifier'].length) return null
|
2023-01-02 17:47:13 +01:00
|
|
|
const identifiers = metadata['dc:identifier']
|
|
|
|
const isbnObj = identifiers.find(i => i['$'] && i['$']['opf:scheme'] === 'ISBN')
|
2021-11-09 03:05:12 +01:00
|
|
|
return isbnObj ? isbnObj['_'] || null : null
|
|
|
|
}
|
|
|
|
|
2023-01-02 17:47:13 +01:00
|
|
|
function fetchASIN(metadata) {
|
|
|
|
if (!metadata['dc:identifier'] || !metadata['dc:identifier'].length) return null
|
|
|
|
const identifiers = metadata['dc:identifier']
|
|
|
|
const asinObj = identifiers.find(i => i['$'] && i['$']['opf:scheme'] === 'ASIN')
|
|
|
|
return asinObj ? asinObj['_'] || null : null
|
|
|
|
}
|
|
|
|
|
2021-11-09 03:05:12 +01:00
|
|
|
function fetchTitle(metadata) {
|
2021-11-10 00:54:28 +01:00
|
|
|
return fetchTagString(metadata, 'dc:title')
|
|
|
|
}
|
|
|
|
|
2023-01-02 17:47:13 +01:00
|
|
|
function fetchSubtitle(metadata) {
|
|
|
|
return fetchTagString(metadata, 'dc:subtitle')
|
|
|
|
}
|
|
|
|
|
2021-11-10 00:54:28 +01:00
|
|
|
function fetchDescription(metadata) {
|
2023-01-02 17:47:13 +01:00
|
|
|
let description = fetchTagString(metadata, 'dc:description')
|
2021-11-10 00:54:28 +01:00
|
|
|
if (!description) return null
|
|
|
|
// check if description is HTML or plain text. only plain text allowed
|
|
|
|
// calibre stores < and > as < and >
|
|
|
|
description = description.replace(/</g, '<').replace(/>/g, '>')
|
2022-05-28 02:41:40 +02:00
|
|
|
return htmlSanitizer.stripAllTags(description)
|
2021-11-10 00:54:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function fetchGenres(metadata) {
|
|
|
|
if (!metadata['dc:subject'] || !metadata['dc:subject'].length) return []
|
2023-07-14 07:15:29 +02:00
|
|
|
return metadata['dc:subject'].map(g => typeof g === 'string' ? g : null).filter(g => !!g).filter((item, index) => metadata['dc:subject'].indexOf(item) === index)
|
2021-11-10 00:54:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function fetchLanguage(metadata) {
|
|
|
|
return fetchTagString(metadata, 'dc:language')
|
2021-11-09 03:05:12 +01:00
|
|
|
}
|
|
|
|
|
2022-05-19 02:25:18 +02:00
|
|
|
function fetchSeries(metadataMeta) {
|
|
|
|
if (!metadataMeta) return null
|
|
|
|
return fetchTagString(metadataMeta, "calibre:series")
|
2021-12-04 22:07:31 +01:00
|
|
|
}
|
|
|
|
|
2022-05-19 02:25:18 +02:00
|
|
|
function fetchVolumeNumber(metadataMeta) {
|
|
|
|
if (!metadataMeta) return null
|
|
|
|
return fetchTagString(metadataMeta, "calibre:series_index")
|
2021-12-04 22:07:31 +01:00
|
|
|
}
|
|
|
|
|
2022-02-01 03:32:03 +01:00
|
|
|
function fetchNarrators(creators, metadata) {
|
2023-01-02 17:47:13 +01:00
|
|
|
const narrators = fetchCreators(creators, 'nrt')
|
2023-03-05 19:40:21 +01:00
|
|
|
if (narrators?.length) return narrators
|
2022-02-01 17:50:42 +01:00
|
|
|
try {
|
2023-01-02 17:47:13 +01:00
|
|
|
const narratorsJSON = JSON.parse(fetchTagString(metadata.meta, "calibre:user_metadata:#narrators").replace(/"/g, '"'))
|
2022-08-13 00:30:05 +02:00
|
|
|
return narratorsJSON["#value#"]
|
2022-02-01 17:50:42 +01:00
|
|
|
} catch {
|
|
|
|
return null
|
|
|
|
}
|
2022-02-01 03:32:03 +01:00
|
|
|
}
|
|
|
|
|
2022-05-19 02:25:18 +02:00
|
|
|
function fetchTags(metadata) {
|
|
|
|
if (!metadata['dc:tag'] || !metadata['dc:tag'].length) return []
|
2023-07-14 07:15:29 +02:00
|
|
|
return metadata['dc:tag'].filter(tag => (typeof tag === 'string')).filter((item, index) => metadata['dc:tag'].indexOf(item) === index)
|
2022-05-19 02:25:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function stripPrefix(str) {
|
|
|
|
if (!str) return ''
|
|
|
|
return str.split(':').pop()
|
|
|
|
}
|
|
|
|
|
2021-11-09 03:05:12 +01:00
|
|
|
module.exports.parseOpfMetadataXML = async (xml) => {
|
2023-01-02 17:47:13 +01:00
|
|
|
const json = await xmlToJSON(xml)
|
2022-05-19 02:25:18 +02:00
|
|
|
|
|
|
|
if (!json) return null
|
|
|
|
|
|
|
|
// Handle <package ...> or with prefix <ns0:package ...>
|
|
|
|
const packageKey = Object.keys(json).find(key => stripPrefix(key) === 'package')
|
|
|
|
if (!packageKey) return null
|
|
|
|
const prefix = packageKey.split(':').shift()
|
2023-01-02 17:47:13 +01:00
|
|
|
let metadata = prefix ? json[packageKey][`${prefix}:metadata`] || json[packageKey].metadata : json[packageKey].metadata
|
2022-05-19 02:25:18 +02:00
|
|
|
if (!metadata) return null
|
2021-11-10 00:54:28 +01:00
|
|
|
|
2021-11-09 03:05:12 +01:00
|
|
|
if (Array.isArray(metadata)) {
|
|
|
|
if (!metadata.length) return null
|
|
|
|
metadata = metadata[0]
|
|
|
|
}
|
|
|
|
|
2022-05-19 02:25:18 +02:00
|
|
|
const metadataMeta = prefix ? metadata[`${prefix}:meta`] || metadata.meta : metadata.meta
|
|
|
|
|
|
|
|
metadata.meta = {}
|
2023-03-05 19:40:21 +01:00
|
|
|
if (metadataMeta?.length) {
|
2022-05-19 02:25:18 +02:00
|
|
|
metadataMeta.forEach((meta) => {
|
|
|
|
if (meta && meta['$'] && meta['$'].name) {
|
|
|
|
metadata.meta[meta['$'].name] = [meta['$'].content || '']
|
|
|
|
}
|
|
|
|
})
|
2021-12-04 23:15:23 +01:00
|
|
|
}
|
|
|
|
|
2022-08-13 00:30:05 +02:00
|
|
|
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 = {
|
2021-11-09 03:05:12 +01:00
|
|
|
title: fetchTitle(metadata),
|
2023-01-02 17:47:13 +01:00
|
|
|
subtitle: fetchSubtitle(metadata),
|
2022-08-13 00:30:05 +02:00
|
|
|
authors,
|
|
|
|
narrators,
|
2022-03-14 01:34:31 +01:00
|
|
|
publishedYear: fetchDate(metadata),
|
2021-11-09 03:05:12 +01:00
|
|
|
publisher: fetchPublisher(metadata),
|
2021-11-10 00:54:28 +01:00
|
|
|
isbn: fetchISBN(metadata),
|
2023-01-02 17:47:13 +01:00
|
|
|
asin: fetchASIN(metadata),
|
2021-11-10 00:54:28 +01:00
|
|
|
description: fetchDescription(metadata),
|
|
|
|
genres: fetchGenres(metadata),
|
2021-12-04 22:07:31 +01:00
|
|
|
language: fetchLanguage(metadata),
|
2022-05-19 02:25:18 +02:00
|
|
|
series: fetchSeries(metadata.meta),
|
|
|
|
sequence: fetchVolumeNumber(metadata.meta),
|
|
|
|
tags: fetchTags(metadata)
|
2021-11-09 03:05:12 +01:00
|
|
|
}
|
|
|
|
return data
|
|
|
|
}
|