2022-03-12 02:46:32 +01:00
|
|
|
const Logger = require('../../Logger')
|
2022-11-03 05:14:07 +01:00
|
|
|
const { areEquivalent, copyValue, cleanStringForSearch, getTitleIgnorePrefix, getTitlePrefixAtEnd } = require('../../utils/index')
|
2022-05-29 18:46:45 +02:00
|
|
|
const parseNameString = require('../../utils/parsers/parseNameString')
|
2022-03-09 02:31:44 +01:00
|
|
|
class BookMetadata {
|
|
|
|
constructor(metadata) {
|
|
|
|
this.title = null
|
|
|
|
this.subtitle = null
|
|
|
|
this.authors = []
|
|
|
|
this.narrators = [] // Array of strings
|
|
|
|
this.series = []
|
|
|
|
this.genres = [] // Array of strings
|
|
|
|
this.publishedYear = null
|
|
|
|
this.publishedDate = null
|
|
|
|
this.publisher = null
|
|
|
|
this.description = null
|
|
|
|
this.isbn = null
|
|
|
|
this.asin = null
|
|
|
|
this.language = null
|
2022-03-11 01:45:02 +01:00
|
|
|
this.explicit = false
|
2022-03-09 02:31:44 +01:00
|
|
|
|
|
|
|
if (metadata) {
|
|
|
|
this.construct(metadata)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
construct(metadata) {
|
|
|
|
this.title = metadata.title
|
|
|
|
this.subtitle = metadata.subtitle
|
2022-03-10 02:23:17 +01:00
|
|
|
this.authors = (metadata.authors && metadata.authors.map) ? metadata.authors.map(a => ({ ...a })) : []
|
|
|
|
this.narrators = metadata.narrators ? [...metadata.narrators] : []
|
|
|
|
this.series = (metadata.series && metadata.series.map) ? metadata.series.map(s => ({ ...s })) : []
|
|
|
|
this.genres = metadata.genres ? [...metadata.genres] : []
|
|
|
|
this.publishedYear = metadata.publishedYear || null
|
|
|
|
this.publishedDate = metadata.publishedDate || null
|
2022-03-09 02:31:44 +01:00
|
|
|
this.publisher = metadata.publisher
|
|
|
|
this.description = metadata.description
|
|
|
|
this.isbn = metadata.isbn
|
|
|
|
this.asin = metadata.asin
|
|
|
|
this.language = metadata.language
|
2022-03-18 01:10:47 +01:00
|
|
|
this.explicit = !!metadata.explicit
|
2022-03-09 02:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
title: this.title,
|
|
|
|
subtitle: this.subtitle,
|
|
|
|
authors: this.authors.map(a => ({ ...a })), // Author JSONMinimal with name and id
|
|
|
|
narrators: [...this.narrators],
|
2022-03-16 00:57:15 +01:00
|
|
|
series: this.series.map(s => ({ ...s })), // Series JSONMinimal with name, id and sequence
|
2022-03-09 02:31:44 +01:00
|
|
|
genres: [...this.genres],
|
|
|
|
publishedYear: this.publishedYear,
|
|
|
|
publishedDate: this.publishedDate,
|
|
|
|
publisher: this.publisher,
|
|
|
|
description: this.description,
|
|
|
|
isbn: this.isbn,
|
|
|
|
asin: this.asin,
|
2022-03-11 01:45:02 +01:00
|
|
|
language: this.language,
|
|
|
|
explicit: this.explicit
|
2022-03-09 02:31:44 +01:00
|
|
|
}
|
|
|
|
}
|
2022-03-11 01:45:02 +01:00
|
|
|
|
2022-03-31 22:07:50 +02:00
|
|
|
toJSONMinified() {
|
|
|
|
return {
|
|
|
|
title: this.title,
|
2022-11-03 05:14:07 +01:00
|
|
|
titleIgnorePrefix: this.titlePrefixAtEnd,
|
2022-03-31 22:07:50 +02:00
|
|
|
subtitle: this.subtitle,
|
|
|
|
authorName: this.authorName,
|
|
|
|
authorNameLF: this.authorNameLF,
|
|
|
|
narratorName: this.narratorName,
|
|
|
|
seriesName: this.seriesName,
|
|
|
|
genres: [...this.genres],
|
|
|
|
publishedYear: this.publishedYear,
|
|
|
|
publishedDate: this.publishedDate,
|
|
|
|
publisher: this.publisher,
|
|
|
|
description: this.description,
|
|
|
|
isbn: this.isbn,
|
|
|
|
asin: this.asin,
|
|
|
|
language: this.language,
|
|
|
|
explicit: this.explicit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-11 01:45:02 +01:00
|
|
|
toJSONExpanded() {
|
|
|
|
return {
|
|
|
|
title: this.title,
|
2022-11-03 05:14:07 +01:00
|
|
|
titleIgnorePrefix: this.titlePrefixAtEnd,
|
2022-03-11 01:45:02 +01:00
|
|
|
subtitle: this.subtitle,
|
|
|
|
authors: this.authors.map(a => ({ ...a })), // Author JSONMinimal with name and id
|
|
|
|
narrators: [...this.narrators],
|
|
|
|
series: this.series.map(s => ({ ...s })),
|
|
|
|
genres: [...this.genres],
|
|
|
|
publishedYear: this.publishedYear,
|
|
|
|
publishedDate: this.publishedDate,
|
|
|
|
publisher: this.publisher,
|
|
|
|
description: this.description,
|
|
|
|
isbn: this.isbn,
|
|
|
|
asin: this.asin,
|
|
|
|
language: this.language,
|
|
|
|
explicit: this.explicit,
|
|
|
|
authorName: this.authorName,
|
2022-03-17 18:25:12 +01:00
|
|
|
authorNameLF: this.authorNameLF,
|
2022-03-26 17:59:34 +01:00
|
|
|
narratorName: this.narratorName,
|
|
|
|
seriesName: this.seriesName
|
2022-03-11 01:45:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-16 00:57:15 +01:00
|
|
|
clone() {
|
|
|
|
return new BookMetadata(this.toJSON())
|
|
|
|
}
|
|
|
|
|
2022-03-11 01:45:02 +01:00
|
|
|
get titleIgnorePrefix() {
|
2022-07-30 23:18:26 +02:00
|
|
|
return getTitleIgnorePrefix(this.title)
|
2022-03-11 01:45:02 +01:00
|
|
|
}
|
2022-11-03 05:14:07 +01:00
|
|
|
get titlePrefixAtEnd() {
|
|
|
|
return getTitlePrefixAtEnd(this.title)
|
|
|
|
}
|
2022-03-11 01:45:02 +01:00
|
|
|
get authorName() {
|
2022-03-13 00:45:32 +01:00
|
|
|
if (!this.authors.length) return ''
|
2022-03-11 01:45:02 +01:00
|
|
|
return this.authors.map(au => au.name).join(', ')
|
|
|
|
}
|
2022-03-17 18:25:12 +01:00
|
|
|
get authorNameLF() { // Last, First
|
|
|
|
if (!this.authors.length) return ''
|
|
|
|
return this.authors.map(au => parseNameString.nameToLastFirst(au.name)).join(', ')
|
|
|
|
}
|
2022-03-13 18:39:12 +01:00
|
|
|
get seriesName() {
|
|
|
|
if (!this.series.length) return ''
|
|
|
|
return this.series.map(se => {
|
|
|
|
if (!se.sequence) return se.name
|
|
|
|
return `${se.name} #${se.sequence}`
|
|
|
|
}).join(', ')
|
|
|
|
}
|
2022-07-30 23:18:26 +02:00
|
|
|
get seriesNameIgnorePrefix() {
|
|
|
|
if (!this.series.length) return ''
|
|
|
|
return this.series.map(se => {
|
|
|
|
if (!se.sequence) return getTitleIgnorePrefix(se.name)
|
|
|
|
return `${getTitleIgnorePrefix(se.name)} #${se.sequence}`
|
|
|
|
}).join(', ')
|
|
|
|
}
|
2022-11-03 05:14:07 +01:00
|
|
|
get seriesNamePrefixAtEnd() {
|
|
|
|
if (!this.series.length) return ''
|
|
|
|
return this.series.map(se => {
|
|
|
|
if (!se.sequence) return getTitlePrefixAtEnd(se.name)
|
|
|
|
return `${getTitlePrefixAtEnd(se.name)} #${se.sequence}`
|
|
|
|
}).join(', ')
|
|
|
|
}
|
2022-09-25 22:56:06 +02:00
|
|
|
get firstSeriesName() {
|
|
|
|
if (!this.series.length) return ''
|
|
|
|
return this.series[0].name
|
|
|
|
}
|
|
|
|
get firstSeriesSequence() {
|
|
|
|
if (!this.series.length) return ''
|
|
|
|
return this.series[0].sequence
|
|
|
|
}
|
2022-03-11 01:45:02 +01:00
|
|
|
get narratorName() {
|
|
|
|
return this.narrators.join(', ')
|
|
|
|
}
|
2022-03-13 00:45:32 +01:00
|
|
|
get coverSearchQuery() {
|
|
|
|
if (!this.authorName) return this.title
|
|
|
|
return this.title + '&' + this.authorName
|
|
|
|
}
|
2022-03-11 01:45:02 +01:00
|
|
|
|
2022-03-13 01:50:31 +01:00
|
|
|
hasAuthor(id) {
|
|
|
|
return !!this.authors.find(au => au.id == id)
|
2022-03-11 01:45:02 +01:00
|
|
|
}
|
2022-03-13 01:50:31 +01:00
|
|
|
hasSeries(seriesId) {
|
|
|
|
return !!this.series.find(se => se.id == seriesId)
|
2022-03-11 01:45:02 +01:00
|
|
|
}
|
|
|
|
hasNarrator(narratorName) {
|
|
|
|
return this.narrators.includes(narratorName)
|
|
|
|
}
|
2022-04-10 02:44:46 +02:00
|
|
|
getSeries(seriesId) {
|
|
|
|
return this.series.find(se => se.id == seriesId)
|
|
|
|
}
|
|
|
|
getFirstSeries() {
|
|
|
|
return this.series.length ? this.series[0] : null
|
|
|
|
}
|
2022-03-27 23:16:08 +02:00
|
|
|
getSeriesSequence(seriesId) {
|
2022-12-31 23:58:19 +01:00
|
|
|
const series = this.series.find(se => se.id == seriesId)
|
2022-03-27 23:16:08 +02:00
|
|
|
if (!series) return null
|
|
|
|
return series.sequence || ''
|
|
|
|
}
|
2022-04-23 00:11:03 +02:00
|
|
|
getSeriesSortTitle(series) {
|
|
|
|
if (!series) return ''
|
|
|
|
if (!series.sequence) return series.name
|
|
|
|
return `${series.name} #${series.sequence}`
|
|
|
|
}
|
2022-03-12 02:46:32 +01:00
|
|
|
|
|
|
|
update(payload) {
|
|
|
|
var json = this.toJSON()
|
|
|
|
var hasUpdates = false
|
|
|
|
for (const key in json) {
|
|
|
|
if (payload[key] !== undefined) {
|
|
|
|
if (!areEquivalent(payload[key], json[key])) {
|
|
|
|
this[key] = copyValue(payload[key])
|
|
|
|
Logger.debug('[BookMetadata] Key updated', key, this[key])
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasUpdates
|
|
|
|
}
|
2022-03-13 00:45:32 +01:00
|
|
|
|
2022-03-18 15:38:36 +01:00
|
|
|
// Updates author name
|
|
|
|
updateAuthor(updatedAuthor) {
|
|
|
|
var author = this.authors.find(au => au.id === updatedAuthor.id)
|
|
|
|
if (!author || author.name == updatedAuthor.name) return false
|
|
|
|
author.name = updatedAuthor.name
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-07-24 19:00:36 +02:00
|
|
|
replaceAuthor(oldAuthor, newAuthor) {
|
|
|
|
this.authors = this.authors.filter(au => au.id !== oldAuthor.id) // Remove old author
|
|
|
|
this.authors.push({
|
|
|
|
id: newAuthor.id,
|
|
|
|
name: newAuthor.name
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-13 00:45:32 +01:00
|
|
|
setData(scanMediaData = {}) {
|
|
|
|
this.title = scanMediaData.title || null
|
|
|
|
this.subtitle = scanMediaData.subtitle || null
|
2022-05-20 05:43:17 +02:00
|
|
|
this.narrators = this.parseNarratorsTag(scanMediaData.narrators)
|
2022-03-14 01:34:31 +01:00
|
|
|
this.publishedYear = scanMediaData.publishedYear || null
|
2022-03-13 00:45:32 +01:00
|
|
|
this.description = scanMediaData.description || null
|
|
|
|
this.isbn = scanMediaData.isbn || null
|
|
|
|
this.asin = scanMediaData.asin || null
|
|
|
|
this.language = scanMediaData.language || null
|
|
|
|
this.genres = []
|
2022-03-18 01:10:47 +01:00
|
|
|
this.explicit = !!scanMediaData.explicit
|
2022-03-13 00:45:32 +01:00
|
|
|
|
|
|
|
if (scanMediaData.author) {
|
|
|
|
this.authors = this.parseAuthorsTag(scanMediaData.author)
|
|
|
|
}
|
|
|
|
if (scanMediaData.series) {
|
|
|
|
this.series = this.parseSeriesTag(scanMediaData.series, scanMediaData.sequence)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setDataFromAudioMetaTags(audioFileMetaTags, overrideExistingDetails = false) {
|
|
|
|
const MetadataMapArray = [
|
|
|
|
{
|
|
|
|
tag: 'tagComposer',
|
|
|
|
key: 'narrators'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagDescription',
|
2022-10-22 15:37:56 +02:00
|
|
|
altTag: 'tagComment',
|
2022-03-13 00:45:32 +01:00
|
|
|
key: 'description'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagPublisher',
|
|
|
|
key: 'publisher'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagDate',
|
2022-03-14 01:34:31 +01:00
|
|
|
key: 'publishedYear'
|
2022-03-13 00:45:32 +01:00
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagSubtitle',
|
|
|
|
key: 'subtitle'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagAlbum',
|
|
|
|
altTag: 'tagTitle',
|
|
|
|
key: 'title',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagArtist',
|
|
|
|
altTag: 'tagAlbumArtist',
|
|
|
|
key: 'authors'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagGenre',
|
|
|
|
key: 'genres'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagSeries',
|
|
|
|
key: 'series'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagIsbn',
|
|
|
|
key: 'isbn'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagLanguage',
|
|
|
|
key: 'language'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagASIN',
|
|
|
|
key: 'asin'
|
2022-06-11 11:00:07 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagOverdriveMediaMarker',
|
|
|
|
key: 'overdriveMediaMarker'
|
2022-03-13 00:45:32 +01:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
2022-12-21 14:13:28 +01:00
|
|
|
const updatePayload = {}
|
2022-03-13 00:45:32 +01:00
|
|
|
|
|
|
|
// Metadata is only mapped to the book if it is empty
|
|
|
|
MetadataMapArray.forEach((mapping) => {
|
2022-12-21 14:13:28 +01:00
|
|
|
let value = audioFileMetaTags[mapping.tag]
|
|
|
|
// let tagToUse = mapping.tag
|
2022-03-13 00:45:32 +01:00
|
|
|
if (!value && mapping.altTag) {
|
|
|
|
value = audioFileMetaTags[mapping.altTag]
|
2022-12-21 14:13:28 +01:00
|
|
|
// tagToUse = mapping.altTag
|
2022-03-13 00:45:32 +01:00
|
|
|
}
|
2022-12-21 14:13:28 +01:00
|
|
|
|
2023-01-02 23:35:39 +01:00
|
|
|
if (value && typeof value === 'string') {
|
|
|
|
value = value.trim() // Trim whitespace
|
2022-12-21 14:13:28 +01:00
|
|
|
|
2022-03-13 00:45:32 +01:00
|
|
|
if (mapping.key === 'narrators' && (!this.narrators.length || overrideExistingDetails)) {
|
|
|
|
updatePayload.narrators = this.parseNarratorsTag(value)
|
|
|
|
} else if (mapping.key === 'authors' && (!this.authors.length || overrideExistingDetails)) {
|
|
|
|
updatePayload.authors = this.parseAuthorsTag(value)
|
|
|
|
} else if (mapping.key === 'genres' && (!this.genres.length || overrideExistingDetails)) {
|
|
|
|
updatePayload.genres = this.parseGenresTag(value)
|
|
|
|
} else if (mapping.key === 'series' && (!this.series.length || overrideExistingDetails)) {
|
2022-12-21 14:13:28 +01:00
|
|
|
const sequenceTag = audioFileMetaTags.tagSeriesPart || null
|
2022-03-13 00:45:32 +01:00
|
|
|
updatePayload.series = this.parseSeriesTag(value, sequenceTag)
|
|
|
|
} else if (!this[mapping.key] || overrideExistingDetails) {
|
|
|
|
updatePayload[mapping.key] = value
|
|
|
|
// Logger.debug(`[Book] Mapping metadata to key ${tagToUse} => ${mapping.key}: ${updatePayload[mapping.key]}`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (Object.keys(updatePayload).length) {
|
|
|
|
return this.update(updatePayload)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns array of names in First Last format
|
|
|
|
parseNarratorsTag(narratorsTag) {
|
2023-01-02 23:35:39 +01:00
|
|
|
const parsed = parseNameString.parse(narratorsTag)
|
2022-03-13 00:45:32 +01:00
|
|
|
return parsed ? parsed.names : []
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return array of authors minified with placeholder id
|
|
|
|
parseAuthorsTag(authorsTag) {
|
2023-01-02 23:35:39 +01:00
|
|
|
const parsed = parseNameString.parse(authorsTag)
|
2022-03-13 00:45:32 +01:00
|
|
|
if (!parsed) return []
|
2022-03-13 19:47:36 +01:00
|
|
|
return (parsed.names || []).map((au) => {
|
2022-03-13 00:45:32 +01:00
|
|
|
return {
|
|
|
|
id: `new-${Math.floor(Math.random() * 1000000)}`,
|
|
|
|
name: au
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
parseGenresTag(genreTag) {
|
|
|
|
if (!genreTag || !genreTag.length) return []
|
2023-01-02 23:35:39 +01:00
|
|
|
const separators = ['/', '//', ';']
|
2022-03-13 00:45:32 +01:00
|
|
|
for (let i = 0; i < separators.length; i++) {
|
|
|
|
if (genreTag.includes(separators[i])) {
|
|
|
|
return genreTag.split(separators[i]).map(genre => genre.trim()).filter(g => !!g)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return [genreTag]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return array with series with placeholder id
|
|
|
|
parseSeriesTag(seriesTag, sequenceTag) {
|
|
|
|
if (!seriesTag) return []
|
|
|
|
return [{
|
|
|
|
id: `new-${Math.floor(Math.random() * 1000000)}`,
|
|
|
|
name: seriesTag,
|
|
|
|
sequence: sequenceTag || ''
|
|
|
|
}]
|
|
|
|
}
|
2022-03-13 18:39:12 +01:00
|
|
|
|
|
|
|
searchSeries(query) {
|
2022-06-26 22:46:16 +02:00
|
|
|
return this.series.filter(se => cleanStringForSearch(se.name).includes(query))
|
2022-03-13 18:39:12 +01:00
|
|
|
}
|
|
|
|
searchAuthors(query) {
|
2022-06-26 22:46:16 +02:00
|
|
|
return this.authors.filter(au => cleanStringForSearch(au.name).includes(query))
|
2022-03-13 18:39:12 +01:00
|
|
|
}
|
|
|
|
searchQuery(query) { // Returns key if match is found
|
2023-01-05 00:43:15 +01:00
|
|
|
const keysToCheck = ['title', 'asin', 'isbn']
|
|
|
|
for (const key of keysToCheck) {
|
2022-06-26 22:46:16 +02:00
|
|
|
if (this[key] && cleanStringForSearch(String(this[key])).includes(query)) {
|
2022-03-13 18:39:12 +01:00
|
|
|
return {
|
|
|
|
matchKey: key,
|
|
|
|
matchText: this[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null
|
|
|
|
}
|
2022-03-09 02:31:44 +01:00
|
|
|
}
|
2022-05-20 05:43:17 +02:00
|
|
|
module.exports = BookMetadata
|