2022-03-12 02:46:32 +01:00
|
|
|
const Logger = require('../../Logger')
|
2023-10-20 00:20:12 +02:00
|
|
|
const { areEquivalent, copyValue, 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 = []
|
2024-09-12 23:36:39 +02:00
|
|
|
this.narrators = [] // Array of strings
|
2022-03-09 02:31:44 +01:00
|
|
|
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
|
2023-03-23 00:05:43 +01:00
|
|
|
this.abridged = false
|
2022-03-09 02:31:44 +01:00
|
|
|
|
|
|
|
if (metadata) {
|
|
|
|
this.construct(metadata)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
construct(metadata) {
|
|
|
|
this.title = metadata.title
|
|
|
|
this.subtitle = metadata.subtitle
|
2024-09-12 23:36:39 +02:00
|
|
|
this.authors = metadata.authors?.map ? metadata.authors.map((a) => ({ ...a })) : []
|
|
|
|
this.narrators = metadata.narrators ? [...metadata.narrators].filter((n) => n) : []
|
2024-11-18 14:59:02 +01:00
|
|
|
this.series = metadata.series?.map
|
|
|
|
? metadata.series.map((s) => ({
|
|
|
|
...s,
|
|
|
|
name: s.name || 'No Title'
|
|
|
|
}))
|
|
|
|
: []
|
2022-03-10 02:23:17 +01:00
|
|
|
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
|
2023-03-23 00:05:43 +01:00
|
|
|
this.abridged = !!metadata.abridged
|
2022-03-09 02:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
title: this.title,
|
|
|
|
subtitle: this.subtitle,
|
2024-09-12 23:36:39 +02:00
|
|
|
authors: this.authors.map((a) => ({ ...a })), // Author JSONMinimal with name and id
|
2022-03-09 02:31:44 +01:00
|
|
|
narrators: [...this.narrators],
|
2024-09-12 23:36:39 +02: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,
|
2023-03-23 00:05:43 +01:00
|
|
|
explicit: this.explicit,
|
|
|
|
abridged: this.abridged
|
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,
|
2023-03-23 00:05:43 +01:00
|
|
|
explicit: this.explicit,
|
|
|
|
abridged: this.abridged
|
2022-03-31 22:07:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2024-09-12 23:36:39 +02:00
|
|
|
authors: this.authors.map((a) => ({ ...a })), // Author JSONMinimal with name and id
|
2022-03-11 01:45:02 +01:00
|
|
|
narrators: [...this.narrators],
|
2024-09-12 23:36:39 +02:00
|
|
|
series: this.series.map((s) => ({ ...s })),
|
2022-03-11 01:45:02 +01:00
|
|
|
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,
|
2023-03-23 00:05:43 +01:00
|
|
|
seriesName: this.seriesName,
|
|
|
|
abridged: this.abridged
|
2022-03-11 01:45:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-16 01:23:31 +02:00
|
|
|
toJSONForMetadataFile() {
|
|
|
|
const json = this.toJSON()
|
2024-09-12 23:36:39 +02:00
|
|
|
json.authors = json.authors.map((au) => au.name)
|
|
|
|
json.series = json.series.map((se) => {
|
2023-05-16 01:23:31 +02:00
|
|
|
if (!se.sequence) return se.name
|
|
|
|
return `${se.name} #${se.sequence}`
|
|
|
|
})
|
|
|
|
return json
|
|
|
|
}
|
|
|
|
|
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 ''
|
2024-09-12 23:36:39 +02:00
|
|
|
return this.authors.map((au) => au.name).join(', ')
|
2022-03-11 01:45:02 +01:00
|
|
|
}
|
2024-09-12 23:36:39 +02:00
|
|
|
get authorNameLF() {
|
|
|
|
// Last, First
|
2022-03-17 18:25:12 +01:00
|
|
|
if (!this.authors.length) return ''
|
2024-09-12 23:36:39 +02:00
|
|
|
return this.authors.map((au) => parseNameString.nameToLastFirst(au.name)).join(', ')
|
2022-03-17 18:25:12 +01:00
|
|
|
}
|
2022-03-13 18:39:12 +01:00
|
|
|
get seriesName() {
|
|
|
|
if (!this.series.length) return ''
|
2024-09-12 23:36:39 +02:00
|
|
|
return this.series
|
|
|
|
.map((se) => {
|
|
|
|
if (!se.sequence) return se.name
|
|
|
|
return `${se.name} #${se.sequence}`
|
|
|
|
})
|
|
|
|
.join(', ')
|
2022-09-25 22:56:06 +02:00
|
|
|
}
|
2022-03-11 01:45:02 +01:00
|
|
|
get narratorName() {
|
|
|
|
return this.narrators.join(', ')
|
|
|
|
}
|
|
|
|
|
2022-04-10 02:44:46 +02:00
|
|
|
getSeries(seriesId) {
|
2024-09-12 23:36:39 +02:00
|
|
|
return this.series.find((se) => se.id == seriesId)
|
2022-04-10 02:44:46 +02:00
|
|
|
}
|
2022-03-27 23:16:08 +02:00
|
|
|
getSeriesSequence(seriesId) {
|
2024-09-12 23:36:39 +02: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-03-12 02:46:32 +01:00
|
|
|
|
|
|
|
update(payload) {
|
2023-05-16 01:23:31 +02:00
|
|
|
const json = this.toJSON()
|
|
|
|
let hasUpdates = false
|
|
|
|
|
2022-03-12 02:46:32 +01:00
|
|
|
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-09 02:31:44 +01:00
|
|
|
}
|
2022-05-20 05:43:17 +02:00
|
|
|
module.exports = BookMetadata
|