mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-07-09 17:04:58 +02:00
Change: scanner uses any .opf file, use description if plain text, use genres #141, Add: language book detail
This commit is contained in:
parent
3eb0dc9ac3
commit
7141f70aa5
6 changed files with 76 additions and 37 deletions
|
@ -20,20 +20,23 @@ function fetchCreator(creators, role) {
|
|||
return creator ? creator.value : null
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
function fetchDate(metadata) {
|
||||
if (!metadata['dc:date']) return null
|
||||
var dates = metadata['dc:date']
|
||||
if (!dates.length || typeof dates[0] !== 'string') return null
|
||||
var dateSplit = dates[0].split('-')
|
||||
var date = fetchTagString(metadata, 'dc:date')
|
||||
if (!date) return null
|
||||
var dateSplit = date.split('-')
|
||||
if (!dateSplit.length || dateSplit[0].length !== 4 || isNaN(dateSplit[0])) return null
|
||||
return dateSplit[0]
|
||||
}
|
||||
|
||||
function fetchPublisher(metadata) {
|
||||
if (!metadata['dc:publisher']) return null
|
||||
var publishers = metadata['dc:publisher']
|
||||
if (!publishers.length || typeof publishers[0] !== 'string') return null
|
||||
return publishers[0]
|
||||
return fetchTagString(metadata, 'dc:publisher')
|
||||
}
|
||||
|
||||
function fetchISBN(metadata) {
|
||||
|
@ -44,22 +47,33 @@ function fetchISBN(metadata) {
|
|||
}
|
||||
|
||||
function fetchTitle(metadata) {
|
||||
if (!metadata['dc:title']) return null
|
||||
var titles = metadata['dc:title']
|
||||
if (!titles.length) return null
|
||||
if (typeof titles[0] === 'string') {
|
||||
return titles[0]
|
||||
}
|
||||
if (titles[0]['_']) {
|
||||
return titles[0]['_']
|
||||
}
|
||||
return null
|
||||
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, '>')
|
||||
if (description.match(/<!DOCTYPE html>|<\/?\s*[a-z-][^>]*\s*>|(\&(?:[\w\d]+|#\d+|#x[a-f\d]+);)/)) return null
|
||||
return description
|
||||
}
|
||||
|
||||
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')
|
||||
}
|
||||
|
||||
module.exports.parseOpfMetadataXML = async (xml) => {
|
||||
var json = await xmlToJSON(xml)
|
||||
if (!json || !json.package || !json.package.metadata) return null
|
||||
var metadata = json.package.metadata
|
||||
|
||||
if (Array.isArray(metadata)) {
|
||||
if (!metadata.length) return null
|
||||
metadata = metadata[0]
|
||||
|
@ -72,7 +86,10 @@ module.exports.parseOpfMetadataXML = async (xml) => {
|
|||
narrator: fetchCreator(creators, 'nrt'),
|
||||
publishYear: fetchDate(metadata),
|
||||
publisher: fetchPublisher(metadata),
|
||||
isbn: fetchISBN(metadata)
|
||||
isbn: fetchISBN(metadata),
|
||||
description: fetchDescription(metadata),
|
||||
genres: fetchGenres(metadata),
|
||||
language: fetchLanguage(metadata)
|
||||
}
|
||||
return data
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue