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
|
|
|
|
var creators = metadata['dc:creator']
|
|
|
|
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
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
function fetchCreator(creators, role) {
|
|
|
|
if (!creators || !creators.length) return null
|
|
|
|
var creator = creators.find(c => c.role === role)
|
|
|
|
return creator ? creator.value : null
|
|
|
|
}
|
|
|
|
|
2021-11-10 00:54:28 +01:00
|
|
|
function fetchTagString(metadata, tag) {
|
|
|
|
if (!metadata[tag] || !metadata[tag].length) return null
|
|
|
|
var tag = metadata[tag][0]
|
|
|
|
if (typeof tag !== 'string') return null
|
|
|
|
return tag
|
|
|
|
}
|
|
|
|
|
2021-11-09 03:05:12 +01:00
|
|
|
function fetchDate(metadata) {
|
2021-11-10 00:54:28 +01:00
|
|
|
var date = fetchTagString(metadata, 'dc:date')
|
|
|
|
if (!date) return null
|
|
|
|
var 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
|
|
|
|
var identifiers = metadata['dc:identifier']
|
|
|
|
var isbnObj = identifiers.find(i => i['$'] && i['$']['opf:scheme'] === 'ISBN')
|
|
|
|
return isbnObj ? isbnObj['_'] || null : null
|
|
|
|
}
|
|
|
|
|
|
|
|
function fetchTitle(metadata) {
|
2021-11-10 00:54:28 +01:00
|
|
|
return fetchTagString(metadata, 'dc:title')
|
|
|
|
}
|
|
|
|
|
|
|
|
function fetchDescription(metadata) {
|
|
|
|
var description = fetchTagString(metadata, 'dc:description')
|
|
|
|
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 []
|
|
|
|
return metadata['dc:subject'].map(g => typeof g === 'string' ? g : null).filter(g => !!g)
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
var roleNrt = fetchCreator(creators, 'nrt')
|
2022-03-13 00:45:32 +01:00
|
|
|
if (typeof metadata.meta == "undefined" || roleNrt != null) return roleNrt
|
2022-02-01 17:50:42 +01:00
|
|
|
try {
|
2022-03-13 00:45:32 +01:00
|
|
|
var narratorsJSON = JSON.parse(fetchTagString(metadata.meta, "calibre:user_metadata:#narrators").replace(/"/g, '"'))
|
2022-02-01 17:50:42 +01:00
|
|
|
return narratorsJSON["#value#"].join(", ")
|
|
|
|
} 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 []
|
|
|
|
return metadata['dc:tag'].filter(tag => (typeof tag === 'string'))
|
|
|
|
}
|
|
|
|
|
|
|
|
function stripPrefix(str) {
|
|
|
|
if (!str) return ''
|
|
|
|
return str.split(':').pop()
|
|
|
|
}
|
|
|
|
|
2021-11-09 03:05:12 +01:00
|
|
|
module.exports.parseOpfMetadataXML = async (xml) => {
|
|
|
|
var 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()
|
|
|
|
var metadata = prefix ? json[packageKey][`${prefix}:metadata`] || json[packageKey].metadata : json[packageKey].metadata
|
|
|
|
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 = {}
|
|
|
|
if (metadataMeta && metadataMeta.length) {
|
|
|
|
metadataMeta.forEach((meta) => {
|
|
|
|
if (meta && meta['$'] && meta['$'].name) {
|
|
|
|
metadata.meta[meta['$'].name] = [meta['$'].content || '']
|
|
|
|
}
|
|
|
|
})
|
2021-12-04 23:15:23 +01:00
|
|
|
}
|
|
|
|
|
2021-11-09 03:05:12 +01:00
|
|
|
var creators = parseCreators(metadata)
|
|
|
|
var data = {
|
|
|
|
title: fetchTitle(metadata),
|
|
|
|
author: fetchCreator(creators, 'aut'),
|
2022-02-01 03:32:03 +01:00
|
|
|
narrator: fetchNarrators(creators, metadata),
|
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),
|
|
|
|
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
|
|
|
|
}
|