2021-08-21 20:02:24 +02:00
|
|
|
const Path = require('path')
|
2021-09-04 21:17:26 +02:00
|
|
|
const Logger = require('../Logger')
|
|
|
|
const parseAuthors = require('../utils/parseAuthors')
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
class Book {
|
|
|
|
constructor(book = null) {
|
|
|
|
this.title = null
|
2021-09-05 02:58:39 +02:00
|
|
|
this.subtitle = null
|
2021-08-18 00:01:11 +02:00
|
|
|
this.author = null
|
2021-08-25 03:24:40 +02:00
|
|
|
this.authorFL = null
|
|
|
|
this.authorLF = null
|
2021-10-07 04:08:52 +02:00
|
|
|
this.narrator = null
|
2021-08-19 18:31:03 +02:00
|
|
|
this.series = null
|
2021-08-25 03:24:40 +02:00
|
|
|
this.volumeNumber = null
|
2021-08-18 00:01:11 +02:00
|
|
|
this.publishYear = null
|
|
|
|
this.publisher = null
|
|
|
|
this.description = null
|
2021-10-28 21:41:42 +02:00
|
|
|
this.isbn = null
|
2021-11-10 00:54:28 +01:00
|
|
|
this.langauge = null
|
2021-08-18 00:01:11 +02:00
|
|
|
this.cover = null
|
2021-10-05 05:11:42 +02:00
|
|
|
this.coverFullPath = null
|
2021-08-20 00:29:36 +02:00
|
|
|
this.genres = []
|
2021-10-06 04:10:49 +02:00
|
|
|
|
2021-09-22 03:57:33 +02:00
|
|
|
this.lastUpdate = null
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2021-10-06 04:10:49 +02:00
|
|
|
// Should not continue looking up a cover when it is not findable
|
|
|
|
this.lastCoverSearch = null
|
|
|
|
this.lastCoverSearchTitle = null
|
|
|
|
this.lastCoverSearchAuthor = null
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
if (book) {
|
|
|
|
this.construct(book)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 23:23:35 +02:00
|
|
|
get _title() { return this.title || '' }
|
2021-09-05 02:58:39 +02:00
|
|
|
get _subtitle() { return this.subtitle || '' }
|
2021-10-07 04:08:52 +02:00
|
|
|
get _narrator() { return this.narrator || '' }
|
2021-10-24 03:31:48 +02:00
|
|
|
get _author() { return this.authorFL || '' }
|
2021-08-21 23:23:35 +02:00
|
|
|
get _series() { return this.series || '' }
|
2021-10-31 22:40:27 +01:00
|
|
|
get _authorsList() { return this._author.split(', ') }
|
2021-11-10 00:54:28 +01:00
|
|
|
get _genres() { return this.genres || [] }
|
2021-08-21 23:23:35 +02:00
|
|
|
|
2021-10-06 04:10:49 +02:00
|
|
|
get shouldSearchForCover() {
|
2021-10-24 03:31:48 +02:00
|
|
|
if (this.authorFL !== this.lastCoverSearchAuthor || this.title !== this.lastCoverSearchTitle || !this.lastCoverSearch) return true
|
2021-10-06 04:10:49 +02:00
|
|
|
var timeSinceLastSearch = Date.now() - this.lastCoverSearch
|
|
|
|
return timeSinceLastSearch > 1000 * 60 * 60 * 24 * 7 // every 7 days do another lookup
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
construct(book) {
|
|
|
|
this.title = book.title
|
2021-09-05 02:58:39 +02:00
|
|
|
this.subtitle = book.subtitle || null
|
2021-08-18 00:01:11 +02:00
|
|
|
this.author = book.author
|
2021-08-25 03:24:40 +02:00
|
|
|
this.authorFL = book.authorFL || null
|
|
|
|
this.authorLF = book.authorLF || null
|
2021-10-07 04:08:52 +02:00
|
|
|
this.narrator = book.narrator || book.narrarator || null // Mispelled initially... need to catch those
|
2021-08-19 18:31:03 +02:00
|
|
|
this.series = book.series
|
2021-08-25 03:24:40 +02:00
|
|
|
this.volumeNumber = book.volumeNumber || null
|
2021-08-20 02:14:24 +02:00
|
|
|
this.publishYear = book.publishYear
|
2021-08-18 00:01:11 +02:00
|
|
|
this.publisher = book.publisher
|
|
|
|
this.description = book.description
|
2021-10-28 21:41:42 +02:00
|
|
|
this.isbn = book.isbn || null
|
2021-11-10 00:54:28 +01:00
|
|
|
this.language = book.language || null
|
2021-08-18 00:01:11 +02:00
|
|
|
this.cover = book.cover
|
2021-10-05 05:11:42 +02:00
|
|
|
this.coverFullPath = book.coverFullPath || null
|
2021-08-20 00:29:36 +02:00
|
|
|
this.genres = book.genres
|
2021-09-22 03:57:33 +02:00
|
|
|
this.lastUpdate = book.lastUpdate || Date.now()
|
2021-10-06 04:10:49 +02:00
|
|
|
this.lastCoverSearch = book.lastCoverSearch || null
|
|
|
|
this.lastCoverSearchTitle = book.lastCoverSearchTitle || null
|
|
|
|
this.lastCoverSearchAuthor = book.lastCoverSearchAuthor || null
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
toJSON() {
|
|
|
|
return {
|
|
|
|
title: this.title,
|
2021-09-05 02:58:39 +02:00
|
|
|
subtitle: this.subtitle,
|
2021-08-18 00:01:11 +02:00
|
|
|
author: this.author,
|
2021-08-25 03:24:40 +02:00
|
|
|
authorFL: this.authorFL,
|
|
|
|
authorLF: this.authorLF,
|
2021-10-07 04:08:52 +02:00
|
|
|
narrator: this.narrator,
|
2021-08-19 18:31:03 +02:00
|
|
|
series: this.series,
|
2021-08-25 03:24:40 +02:00
|
|
|
volumeNumber: this.volumeNumber,
|
2021-08-20 02:14:24 +02:00
|
|
|
publishYear: this.publishYear,
|
2021-08-18 00:01:11 +02:00
|
|
|
publisher: this.publisher,
|
|
|
|
description: this.description,
|
2021-10-28 21:41:42 +02:00
|
|
|
isbn: this.isbn,
|
2021-11-10 00:54:28 +01:00
|
|
|
language: this.language,
|
2021-08-18 00:01:11 +02:00
|
|
|
cover: this.cover,
|
2021-10-05 05:11:42 +02:00
|
|
|
coverFullPath: this.coverFullPath,
|
2021-09-22 03:57:33 +02:00
|
|
|
genres: this.genres,
|
2021-10-06 04:10:49 +02:00
|
|
|
lastUpdate: this.lastUpdate,
|
|
|
|
lastCoverSearch: this.lastCoverSearch,
|
|
|
|
lastCoverSearchTitle: this.lastCoverSearchTitle,
|
|
|
|
lastCoverSearchAuthor: this.lastCoverSearchAuthor
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-25 13:38:32 +02:00
|
|
|
setParseAuthor(author) {
|
|
|
|
if (!author) {
|
|
|
|
var hasUpdated = this.authorFL || this.authorLF
|
|
|
|
this.authorFL = null
|
|
|
|
this.authorLF = null
|
|
|
|
return hasUpdated
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
var { authorLF, authorFL } = parseAuthors(author)
|
|
|
|
var hasUpdated = authorLF !== this.authorLF || authorFL !== this.authorFL
|
|
|
|
this.authorFL = authorFL || null
|
|
|
|
this.authorLF = authorLF || null
|
|
|
|
return hasUpdated
|
|
|
|
} catch (err) {
|
|
|
|
Logger.error('[Book] Parse authors failed', err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
setData(data) {
|
|
|
|
this.title = data.title || null
|
2021-09-05 02:58:39 +02:00
|
|
|
this.subtitle = data.subtitle || null
|
2021-08-18 00:01:11 +02:00
|
|
|
this.author = data.author || null
|
2021-10-07 04:08:52 +02:00
|
|
|
this.narrator = data.narrator || data.narrarator || null
|
2021-08-19 18:31:03 +02:00
|
|
|
this.series = data.series || null
|
2021-08-25 03:24:40 +02:00
|
|
|
this.volumeNumber = data.volumeNumber || null
|
2021-08-20 02:14:24 +02:00
|
|
|
this.publishYear = data.publishYear || null
|
2021-08-18 00:01:11 +02:00
|
|
|
this.description = data.description || null
|
2021-10-28 21:41:42 +02:00
|
|
|
this.isbn = data.isbn || null
|
2021-11-10 00:54:28 +01:00
|
|
|
this.language = data.language || null
|
2021-08-18 00:01:11 +02:00
|
|
|
this.cover = data.cover || null
|
2021-10-05 05:11:42 +02:00
|
|
|
this.coverFullPath = data.coverFullPath || null
|
2021-08-20 00:29:36 +02:00
|
|
|
this.genres = data.genres || []
|
2021-09-22 03:57:33 +02:00
|
|
|
this.lastUpdate = Date.now()
|
2021-10-06 04:10:49 +02:00
|
|
|
this.lastCoverSearch = data.lastCoverSearch || null
|
|
|
|
this.lastCoverSearchTitle = data.lastCoverSearchTitle || null
|
|
|
|
this.lastCoverSearchAuthor = data.lastCoverSearchAuthor || null
|
2021-08-21 20:02:24 +02:00
|
|
|
|
2021-08-25 13:38:32 +02:00
|
|
|
if (data.author) {
|
|
|
|
this.setParseAuthor(this.author)
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
update(payload) {
|
|
|
|
var hasUpdates = false
|
2021-08-26 02:15:00 +02:00
|
|
|
|
2021-11-06 23:26:44 +01:00
|
|
|
// Clean cover paths if passed
|
2021-08-26 02:15:00 +02:00
|
|
|
if (payload.cover) {
|
|
|
|
if (!payload.cover.startsWith('http:') && !payload.cover.startsWith('https:')) {
|
2021-11-06 23:26:44 +01:00
|
|
|
payload.cover = payload.cover.replace(/\\/g, '/')
|
|
|
|
if (payload.coverFullPath) payload.coverFullPath = payload.coverFullPath.replace(/\\/g, '/')
|
2021-10-16 03:31:00 +02:00
|
|
|
else {
|
|
|
|
Logger.warn(`[Book] "${this.title}" updating book cover to "${payload.cover}" but no full path was passed`)
|
|
|
|
}
|
2021-08-26 02:15:00 +02:00
|
|
|
}
|
2021-10-16 03:31:00 +02:00
|
|
|
} else if (payload.coverFullPath) {
|
|
|
|
Logger.warn(`[Book] "${this.title}" updating book full cover path to "${payload.coverFullPath}" but no relative path was passed`)
|
2021-11-06 23:26:44 +01:00
|
|
|
payload.coverFullPath = payload.coverFullPath.replace(/\\/g, '/')
|
2021-08-26 02:15:00 +02:00
|
|
|
}
|
|
|
|
|
2021-08-18 00:01:11 +02:00
|
|
|
for (const key in payload) {
|
|
|
|
if (payload[key] === undefined) continue;
|
|
|
|
|
2021-08-20 00:29:36 +02:00
|
|
|
if (key === 'genres') {
|
|
|
|
if (payload['genres'] === null && this.genres !== null) {
|
|
|
|
this.genres = []
|
2021-08-18 00:01:11 +02:00
|
|
|
hasUpdates = true
|
2021-08-20 00:29:36 +02:00
|
|
|
} else if (payload['genres'].join(',') !== this.genres.join(',')) {
|
|
|
|
this.genres = payload['genres']
|
2021-08-18 00:01:11 +02:00
|
|
|
hasUpdates = true
|
|
|
|
}
|
2021-08-25 13:38:32 +02:00
|
|
|
} else if (key === 'author') {
|
|
|
|
if (this.author !== payload.author) {
|
|
|
|
this.author = payload.author || null
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
if (this.setParseAuthor(this.author)) {
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
} else if (this[key] !== undefined && payload[key] !== this[key]) {
|
|
|
|
this[key] = payload[key]
|
|
|
|
hasUpdates = true
|
|
|
|
}
|
|
|
|
}
|
2021-09-22 03:57:33 +02:00
|
|
|
|
|
|
|
if (hasUpdates) {
|
|
|
|
this.lastUpdate = Date.now()
|
|
|
|
}
|
|
|
|
|
2021-08-25 03:24:40 +02:00
|
|
|
return hasUpdates
|
|
|
|
}
|
|
|
|
|
2021-10-06 04:10:49 +02:00
|
|
|
updateLastCoverSearch(coverWasFound) {
|
|
|
|
this.lastCoverSearch = coverWasFound ? null : Date.now()
|
2021-10-24 03:31:48 +02:00
|
|
|
this.lastCoverSearchAuthor = coverWasFound ? null : this.authorFL
|
2021-10-06 04:10:49 +02:00
|
|
|
this.lastCoverSearchTitle = coverWasFound ? null : this.title
|
|
|
|
}
|
|
|
|
|
|
|
|
updateCover(cover, coverFullPath) {
|
2021-09-22 03:57:33 +02:00
|
|
|
if (!cover) return false
|
|
|
|
if (!cover.startsWith('http:') && !cover.startsWith('https:')) {
|
2021-11-06 23:26:44 +01:00
|
|
|
cover = cover.replace(/\\/g, '/')
|
|
|
|
this.coverFullPath = coverFullPath.replace(/\\/g, '/')
|
2021-10-06 04:10:49 +02:00
|
|
|
} else {
|
|
|
|
this.coverFullPath = cover
|
2021-09-22 03:57:33 +02:00
|
|
|
}
|
|
|
|
this.cover = cover
|
|
|
|
this.lastUpdate = Date.now()
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-10-05 05:11:42 +02:00
|
|
|
removeCover() {
|
|
|
|
this.cover = null
|
|
|
|
this.coverFullPath = null
|
|
|
|
this.lastUpdate = Date.now()
|
|
|
|
}
|
|
|
|
|
2021-08-26 00:36:54 +02:00
|
|
|
// If audiobook directory path was changed, check and update properties set from dirnames
|
|
|
|
// May be worthwhile checking if these were manually updated and not override manual updates
|
|
|
|
syncPathsUpdated(audiobookData) {
|
2021-09-18 18:13:05 +02:00
|
|
|
var keysToSync = ['author', 'title', 'series', 'publishYear', 'volumeNumber']
|
2021-08-26 00:36:54 +02:00
|
|
|
var syncPayload = {}
|
|
|
|
keysToSync.forEach((key) => {
|
|
|
|
if (audiobookData[key]) syncPayload[key] = audiobookData[key]
|
|
|
|
})
|
|
|
|
if (!Object.keys(syncPayload).length) return false
|
|
|
|
return this.update(syncPayload)
|
|
|
|
}
|
|
|
|
|
2021-08-21 23:23:35 +02:00
|
|
|
isSearchMatch(search) {
|
2021-09-05 02:58:39 +02:00
|
|
|
return this._title.toLowerCase().includes(search) || this._subtitle.toLowerCase().includes(search) || this._author.toLowerCase().includes(search) || this._series.toLowerCase().includes(search)
|
2021-08-21 23:23:35 +02:00
|
|
|
}
|
2021-09-30 03:43:36 +02:00
|
|
|
|
2021-10-09 18:09:06 +02:00
|
|
|
getQueryMatches(search) {
|
2021-10-17 18:29:52 +02:00
|
|
|
var titleMatch = this._title.toLowerCase().includes(search)
|
|
|
|
var subtitleMatch = this._subtitle.toLowerCase().includes(search)
|
2021-10-31 22:40:27 +01:00
|
|
|
|
|
|
|
var authorsMatched = this._authorsList.filter(auth => auth.toLowerCase().includes(search))
|
|
|
|
|
|
|
|
// var authorMatch = this._author.toLowerCase().includes(search)
|
2021-10-09 18:09:06 +02:00
|
|
|
var seriesMatch = this._series.toLowerCase().includes(search)
|
2021-10-17 18:29:52 +02:00
|
|
|
|
2021-10-31 22:40:27 +01:00
|
|
|
var bookMatchKey = titleMatch ? 'title' : subtitleMatch ? 'subtitle' : authorsMatched.length ? 'authorFL' : seriesMatch ? 'series' : false
|
2021-10-17 18:29:52 +02:00
|
|
|
var bookMatchText = bookMatchKey ? this[bookMatchKey] : ''
|
2021-10-09 18:09:06 +02:00
|
|
|
return {
|
2021-10-17 18:29:52 +02:00
|
|
|
book: bookMatchKey,
|
|
|
|
bookMatchText,
|
2021-10-31 22:40:27 +01:00
|
|
|
authors: authorsMatched.length ? authorsMatched : false,
|
2021-10-09 18:09:06 +02:00
|
|
|
series: seriesMatch ? this._series : false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-23 03:08:02 +02:00
|
|
|
parseGenresTag(genreTag) {
|
|
|
|
if (!genreTag || !genreTag.length) return []
|
|
|
|
var separators = ['/', '//', ';']
|
|
|
|
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]
|
|
|
|
}
|
|
|
|
|
2021-09-30 03:43:36 +02:00
|
|
|
setDetailsFromFileMetadata(audioFileMetadata) {
|
|
|
|
const MetadataMapArray = [
|
|
|
|
{
|
|
|
|
tag: 'tagComposer',
|
2021-10-07 04:08:52 +02:00
|
|
|
key: 'narrator'
|
2021-09-30 03:43:36 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagDescription',
|
|
|
|
key: 'description'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagPublisher',
|
|
|
|
key: 'publisher'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagDate',
|
|
|
|
key: 'publishYear'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagSubtitle',
|
|
|
|
key: 'subtitle'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagArtist',
|
|
|
|
key: 'author'
|
2021-10-23 03:08:02 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagGenre',
|
|
|
|
key: 'genres'
|
2021-10-24 00:32:42 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagSeries',
|
|
|
|
key: 'series'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
tag: 'tagSeriesPart',
|
|
|
|
key: 'volumeNumber'
|
2021-09-30 03:43:36 +02:00
|
|
|
}
|
|
|
|
]
|
|
|
|
|
|
|
|
var updatePayload = {}
|
2021-10-24 00:32:42 +02:00
|
|
|
// Metadata is only mapped to the book if it is empty
|
2021-09-30 03:43:36 +02:00
|
|
|
MetadataMapArray.forEach((mapping) => {
|
2021-10-23 13:50:13 +02:00
|
|
|
if (audioFileMetadata[mapping.tag]) {
|
2021-10-23 03:08:02 +02:00
|
|
|
// Genres can contain multiple
|
2021-10-23 13:50:13 +02:00
|
|
|
if (mapping.key === 'genres' && (!this[mapping.key].length || !this[mapping.key])) {
|
2021-10-23 03:08:02 +02:00
|
|
|
updatePayload[mapping.key] = this.parseGenresTag(audioFileMetadata[mapping.tag])
|
|
|
|
Logger.debug(`[Book] Mapping metadata to key ${mapping.tag} => ${mapping.key}: ${updatePayload[mapping.key].join(',')}`)
|
2021-10-23 13:50:13 +02:00
|
|
|
} else if (!this[mapping.key]) {
|
2021-10-23 03:08:02 +02:00
|
|
|
updatePayload[mapping.key] = audioFileMetadata[mapping.tag]
|
|
|
|
Logger.debug(`[Book] Mapping metadata to key ${mapping.tag} => ${mapping.key}: ${updatePayload[mapping.key]}`)
|
|
|
|
}
|
2021-09-30 03:43:36 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
if (Object.keys(updatePayload).length) {
|
|
|
|
return this.update(updatePayload)
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
module.exports = Book
|