2021-12-26 18:25:07 +01:00
|
|
|
const { sort, createNewSortInstance } = require('fast-sort')
|
|
|
|
const naturalSort = createNewSortInstance({
|
|
|
|
comparer: new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' }).compare
|
|
|
|
})
|
2021-12-01 03:02:40 +01:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
decode(text) {
|
|
|
|
return Buffer.from(decodeURIComponent(text), 'base64').toString()
|
|
|
|
},
|
|
|
|
|
2022-03-11 01:45:02 +01:00
|
|
|
getFilteredLibraryItems(libraryItems, filterBy, user) {
|
|
|
|
var filtered = libraryItems
|
|
|
|
|
|
|
|
var searchGroups = ['genres', 'tags', 'series', 'authors', 'progress', 'narrators', 'languages']
|
|
|
|
var group = searchGroups.find(_group => filterBy.startsWith(_group + '.'))
|
|
|
|
if (group) {
|
|
|
|
var filterVal = filterBy.replace(`${group}.`, '')
|
|
|
|
var filter = this.decode(filterVal)
|
|
|
|
if (group === 'genres') filtered = filtered.filter(li => li.media.metadata && li.media.metadata.genres.includes(filter))
|
|
|
|
else if (group === 'tags') filtered = filtered.filter(li => li.media.tags.includes(filter))
|
|
|
|
else if (group === 'series') {
|
|
|
|
if (filter === 'No Series') filtered = filtered.filter(li => li.media.metadata && !li.media.metadata.series.length)
|
2022-03-13 01:50:31 +01:00
|
|
|
else {
|
|
|
|
filtered = filtered.filter(li => li.media.metadata && li.media.metadata.hasSeries(filter))
|
|
|
|
}
|
2022-03-11 01:45:02 +01:00
|
|
|
}
|
|
|
|
else if (group === 'authors') filtered = filtered.filter(li => li.media.metadata && li.media.metadata.hasAuthor(filter))
|
|
|
|
else if (group === 'narrators') filtered = filtered.filter(li => li.media.metadata && li.media.metadata.hasNarrator(filter))
|
|
|
|
else if (group === 'progress') {
|
|
|
|
filtered = filtered.filter(li => {
|
2022-03-16 00:57:15 +01:00
|
|
|
var userAudiobook = user.getLibraryItemProgress(li.id)
|
2022-03-11 01:45:02 +01:00
|
|
|
var isRead = userAudiobook && userAudiobook.isRead
|
|
|
|
if (filter === 'Read' && isRead) return true
|
|
|
|
if (filter === 'Unread' && !isRead) return true
|
|
|
|
if (filter === 'In Progress' && (userAudiobook && !userAudiobook.isRead && userAudiobook.progress > 0)) return true
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
} else if (group === 'languages') {
|
|
|
|
filtered = filtered.filter(li => li.media.metadata && li.media.metadata.language === filter)
|
|
|
|
}
|
|
|
|
} else if (filterBy === 'issues') {
|
|
|
|
filtered = filtered.filter(ab => {
|
|
|
|
// TODO: Update filter for issues
|
|
|
|
return ab.isMissing
|
|
|
|
// return ab.numMissingParts || ab.numInvalidParts || ab.isMissing || ab.isInvalid
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return filtered
|
|
|
|
},
|
|
|
|
|
2021-12-01 03:02:40 +01:00
|
|
|
getFiltered(audiobooks, filterBy, user) {
|
|
|
|
var filtered = audiobooks
|
|
|
|
|
2022-01-10 01:37:16 +01:00
|
|
|
var searchGroups = ['genres', 'tags', 'series', 'authors', 'progress', 'narrators', 'languages']
|
2021-12-01 03:02:40 +01:00
|
|
|
var group = searchGroups.find(_group => filterBy.startsWith(_group + '.'))
|
|
|
|
if (group) {
|
|
|
|
var filterVal = filterBy.replace(`${group}.`, '')
|
|
|
|
var filter = this.decode(filterVal)
|
|
|
|
if (group === 'genres') filtered = filtered.filter(ab => ab.book && ab.book.genres.includes(filter))
|
|
|
|
else if (group === 'tags') filtered = filtered.filter(ab => ab.tags.includes(filter))
|
|
|
|
else if (group === 'series') {
|
|
|
|
if (filter === 'No Series') filtered = filtered.filter(ab => ab.book && !ab.book.series)
|
|
|
|
else filtered = filtered.filter(ab => ab.book && ab.book.series === filter)
|
|
|
|
}
|
|
|
|
else if (group === 'authors') filtered = filtered.filter(ab => ab.book && ab.book.authorFL && ab.book.authorFL.split(', ').includes(filter))
|
|
|
|
else if (group === 'narrators') filtered = filtered.filter(ab => ab.book && ab.book.narratorFL && ab.book.narratorFL.split(', ').includes(filter))
|
|
|
|
else if (group === 'progress') {
|
|
|
|
filtered = filtered.filter(ab => {
|
2022-03-16 00:57:15 +01:00
|
|
|
var userAudiobook = user.getLibraryItemProgress(ab.id)
|
2021-12-01 03:02:40 +01:00
|
|
|
var isRead = userAudiobook && userAudiobook.isRead
|
|
|
|
if (filter === 'Read' && isRead) return true
|
|
|
|
if (filter === 'Unread' && !isRead) return true
|
|
|
|
if (filter === 'In Progress' && (userAudiobook && !userAudiobook.isRead && userAudiobook.progress > 0)) return true
|
|
|
|
return false
|
|
|
|
})
|
2022-01-10 01:37:16 +01:00
|
|
|
} else if (group === 'languages') {
|
|
|
|
filtered = filtered.filter(ab => ab.book && ab.book.language === filter)
|
2021-12-01 03:02:40 +01:00
|
|
|
}
|
|
|
|
} else if (filterBy === 'issues') {
|
|
|
|
filtered = filtered.filter(ab => {
|
2021-12-02 02:07:03 +01:00
|
|
|
return ab.numMissingParts || ab.numInvalidParts || ab.isMissing || ab.isInvalid
|
2021-12-01 03:02:40 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return filtered
|
|
|
|
},
|
|
|
|
|
2022-03-11 01:45:02 +01:00
|
|
|
getDistinctFilterDataNew(libraryItems) {
|
|
|
|
var data = {
|
|
|
|
authors: [],
|
|
|
|
genres: [],
|
|
|
|
tags: [],
|
|
|
|
series: [],
|
|
|
|
narrators: [],
|
|
|
|
languages: []
|
|
|
|
}
|
|
|
|
libraryItems.forEach((li) => {
|
|
|
|
var mediaMetadata = li.media.metadata
|
|
|
|
if (mediaMetadata.authors.length) {
|
|
|
|
mediaMetadata.authors.forEach((author) => {
|
2022-03-12 02:46:32 +01:00
|
|
|
if (author && !data.authors.find(au => au.id === author.id)) data.authors.push({ id: author.id, name: author.name })
|
2022-03-11 01:45:02 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if (mediaMetadata.series.length) {
|
|
|
|
mediaMetadata.series.forEach((series) => {
|
2022-03-12 02:46:32 +01:00
|
|
|
if (series && !data.series.find(se => se.id === series.id)) data.series.push({ id: series.id, name: series.name })
|
2022-03-11 01:45:02 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if (mediaMetadata.genres.length) {
|
|
|
|
mediaMetadata.genres.forEach((genre) => {
|
|
|
|
if (genre && !data.genres.includes(genre)) data.genres.push(genre)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (li.media.tags.length) {
|
|
|
|
li.media.tags.forEach((tag) => {
|
|
|
|
if (tag && !data.tags.includes(tag)) data.tags.push(tag)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (mediaMetadata.narrators.length) {
|
|
|
|
mediaMetadata.narrators.forEach((narrator) => {
|
|
|
|
if (narrator && !data.narrators.includes(narrator)) data.narrators.push(narrator)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (mediaMetadata.language && !data.languages.includes(mediaMetadata.language)) data.languages.push(mediaMetadata.language)
|
|
|
|
})
|
|
|
|
data.authors = naturalSort(data.authors).asc()
|
|
|
|
data.genres = naturalSort(data.genres).asc()
|
|
|
|
data.tags = naturalSort(data.tags).asc()
|
|
|
|
data.series = naturalSort(data.series).asc()
|
|
|
|
data.narrators = naturalSort(data.narrators).asc()
|
|
|
|
data.languages = naturalSort(data.languages).asc()
|
|
|
|
return data
|
|
|
|
},
|
|
|
|
|
2021-12-24 23:37:57 +01:00
|
|
|
getSeriesFromBooks(books, minified = false) {
|
2021-12-01 03:02:40 +01:00
|
|
|
var _series = {}
|
2022-03-13 01:50:31 +01:00
|
|
|
books.forEach((libraryItem) => {
|
|
|
|
if (libraryItem.media.metadata.series && libraryItem.media.metadata.series.length) {
|
|
|
|
libraryItem.media.metadata.series.forEach((series) => {
|
|
|
|
var abJson = minified ? libraryItem.toJSONMinified() : libraryItem.toJSONExpanded()
|
|
|
|
abJson.sequence = series.sequence
|
|
|
|
if (!_series[series.id]) {
|
|
|
|
_series[series.id] = {
|
|
|
|
id: series.id,
|
|
|
|
name: series.name,
|
|
|
|
type: 'series',
|
|
|
|
books: [abJson]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_series[series.id].books.push(abJson)
|
2021-12-01 03:02:40 +01:00
|
|
|
}
|
2022-03-13 01:50:31 +01:00
|
|
|
})
|
2021-12-01 03:02:40 +01:00
|
|
|
}
|
|
|
|
})
|
2021-12-06 23:18:26 +01:00
|
|
|
return Object.values(_series).map((series) => {
|
2022-03-13 01:50:31 +01:00
|
|
|
series.books = naturalSort(series.books).asc(li => li.sequence)
|
2021-12-06 23:18:26 +01:00
|
|
|
return series
|
|
|
|
})
|
2021-12-01 03:02:40 +01:00
|
|
|
},
|
|
|
|
|
2022-01-25 11:05:39 +01:00
|
|
|
getSeriesWithProgressFromBooks(user, books) {
|
|
|
|
var _series = {}
|
|
|
|
books.forEach((audiobook) => {
|
|
|
|
if (audiobook.book.series) {
|
2022-03-16 00:57:15 +01:00
|
|
|
var bookWithUserAb = { userAudiobook: user.getLibraryItemProgress(audiobook.id), book: audiobook }
|
2022-01-25 11:05:39 +01:00
|
|
|
if (!_series[audiobook.book.series]) {
|
|
|
|
_series[audiobook.book.series] = {
|
|
|
|
id: audiobook.book.series,
|
|
|
|
name: audiobook.book.series,
|
|
|
|
type: 'series',
|
|
|
|
books: [bookWithUserAb]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
_series[audiobook.book.series].books.push(bookWithUserAb)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return Object.values(_series).map((series) => {
|
|
|
|
series.books = naturalSort(series.books).asc(ab => ab.book.book.volumeNumber)
|
|
|
|
return series
|
|
|
|
}).filter((series) => series.books.some((book) => book.userAudiobook && book.userAudiobook.isRead))
|
|
|
|
},
|
|
|
|
|
2022-03-13 01:50:31 +01:00
|
|
|
sortSeriesBooks(books, seriesId, minified = false) {
|
|
|
|
return naturalSort(books).asc(li => {
|
|
|
|
if (!li.media.metadata.series) return null
|
|
|
|
var series = li.media.metadata.series.find(se => se.id === seriesId)
|
|
|
|
if (!series) return null
|
|
|
|
return series.sequence
|
|
|
|
}).map(li => {
|
|
|
|
if (minified) return li.toJSONMinified()
|
|
|
|
return li.toJSONExpanded()
|
2021-12-26 18:25:07 +01:00
|
|
|
})
|
|
|
|
},
|
|
|
|
|
2021-12-01 03:02:40 +01:00
|
|
|
getBooksWithUserAudiobook(user, books) {
|
|
|
|
return books.map(book => {
|
|
|
|
return {
|
2022-03-16 00:57:15 +01:00
|
|
|
userAudiobook: user.getLibraryItemProgress(book.id),
|
2021-12-01 03:02:40 +01:00
|
|
|
book
|
|
|
|
}
|
2022-01-06 01:01:33 +01:00
|
|
|
}).filter(b => !!b.userAudiobook)
|
2021-12-01 03:02:40 +01:00
|
|
|
},
|
|
|
|
|
2021-12-24 23:37:57 +01:00
|
|
|
getBooksMostRecentlyRead(booksWithUserAb, limit, minified = false) {
|
2021-12-02 02:07:03 +01:00
|
|
|
var booksWithProgress = booksWithUserAb.filter((data) => data.userAudiobook && data.userAudiobook.progress > 0 && !data.userAudiobook.isRead)
|
2021-12-01 03:02:40 +01:00
|
|
|
booksWithProgress.sort((a, b) => {
|
|
|
|
return b.userAudiobook.lastUpdate - a.userAudiobook.lastUpdate
|
|
|
|
})
|
2021-12-24 23:37:57 +01:00
|
|
|
return booksWithProgress.map(b => minified ? b.book.toJSONMinified() : b.book.toJSONExpanded()).slice(0, limit)
|
2021-12-01 03:02:40 +01:00
|
|
|
},
|
|
|
|
|
2022-01-25 11:05:39 +01:00
|
|
|
getBooksNextInSeries(seriesWithUserAb, limit, minified = false) {
|
|
|
|
var incompleteSeires = seriesWithUserAb.filter((series) => series.books.some((book) => !book.userAudiobook || (!book.userAudiobook.isRead && book.userAudiobook.progress == 0)))
|
|
|
|
var booksNextInSeries = []
|
|
|
|
incompleteSeires.forEach((series) => {
|
2022-01-27 23:53:18 +01:00
|
|
|
var dateLastRead = series.books.filter((data) => data.userAudiobook && data.userAudiobook.isRead).sort((a, b) => { return b.userAudiobook.finishedAt - a.userAudiobook.finishedAt })[0].userAudiobook.finishedAt
|
|
|
|
var nextUnreadBook = series.books.filter((data) => !data.userAudiobook || (!data.userAudiobook.isRead && data.userAudiobook.progress == 0))[0]
|
2022-01-25 11:05:39 +01:00
|
|
|
nextUnreadBook.DateLastReadSeries = dateLastRead
|
|
|
|
booksNextInSeries.push(nextUnreadBook)
|
|
|
|
})
|
2022-01-27 23:53:18 +01:00
|
|
|
return booksNextInSeries.sort((a, b) => { return b.DateLastReadSeries - a.DateLastReadSeries }).map(b => minified ? b.book.toJSONMinified() : b.book.toJSONExpanded()).slice(0, limit)
|
2022-01-25 11:05:39 +01:00
|
|
|
},
|
|
|
|
|
2021-12-24 23:37:57 +01:00
|
|
|
getBooksMostRecentlyAdded(books, limit, minified = false) {
|
2021-12-01 03:02:40 +01:00
|
|
|
var booksSortedByAddedAt = sort(books).desc(book => book.addedAt)
|
2021-12-24 23:37:57 +01:00
|
|
|
return booksSortedByAddedAt.map(b => minified ? b.toJSONMinified() : b.toJSONExpanded()).slice(0, limit)
|
2021-12-01 03:02:40 +01:00
|
|
|
},
|
|
|
|
|
2021-12-24 23:37:57 +01:00
|
|
|
getBooksMostRecentlyFinished(booksWithUserAb, limit, minified = false) {
|
2021-12-01 03:02:40 +01:00
|
|
|
var booksRead = booksWithUserAb.filter((data) => data.userAudiobook && data.userAudiobook.isRead)
|
|
|
|
booksRead.sort((a, b) => {
|
|
|
|
return b.userAudiobook.finishedAt - a.userAudiobook.finishedAt
|
|
|
|
})
|
2021-12-24 23:37:57 +01:00
|
|
|
return booksRead.map(b => minified ? b.book.toJSONMinified() : b.book.toJSONExpanded()).slice(0, limit)
|
2021-12-01 03:02:40 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
getSeriesMostRecentlyAdded(series, limit) {
|
|
|
|
var seriesSortedByAddedAt = sort(series).desc(_series => {
|
|
|
|
var booksSortedByMostRecent = sort(_series.books).desc(b => b.addedAt)
|
|
|
|
return booksSortedByMostRecent[0].addedAt
|
|
|
|
})
|
|
|
|
return seriesSortedByAddedAt.slice(0, limit)
|
2021-12-02 02:07:03 +01:00
|
|
|
},
|
|
|
|
|
2022-03-13 23:33:50 +01:00
|
|
|
getGenresWithCount(libraryItems) {
|
2021-12-02 02:07:03 +01:00
|
|
|
var genresMap = {}
|
2022-03-13 23:33:50 +01:00
|
|
|
libraryItems.forEach((li) => {
|
|
|
|
var genres = li.media.metadata.genres || []
|
2021-12-02 02:07:03 +01:00
|
|
|
genres.forEach((genre) => {
|
|
|
|
if (genresMap[genre]) genresMap[genre].count++
|
|
|
|
else
|
|
|
|
genresMap[genre] = {
|
|
|
|
genre,
|
|
|
|
count: 1
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return Object.values(genresMap).sort((a, b) => b.count - a.count)
|
|
|
|
},
|
|
|
|
|
2022-03-13 23:33:50 +01:00
|
|
|
getAuthorsWithCount(libraryItems) {
|
2021-12-02 02:07:03 +01:00
|
|
|
var authorsMap = {}
|
2022-03-13 23:33:50 +01:00
|
|
|
libraryItems.forEach((li) => {
|
|
|
|
var authors = li.media.metadata.authors || []
|
2021-12-02 02:07:03 +01:00
|
|
|
authors.forEach((author) => {
|
2022-03-13 23:33:50 +01:00
|
|
|
if (authorsMap[author.id]) authorsMap[author.id].count++
|
2021-12-02 02:07:03 +01:00
|
|
|
else
|
2022-03-13 23:33:50 +01:00
|
|
|
authorsMap[author.id] = {
|
|
|
|
author: author.name,
|
2021-12-02 02:07:03 +01:00
|
|
|
count: 1
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
return Object.values(authorsMap).sort((a, b) => b.count - a.count)
|
|
|
|
},
|
|
|
|
|
2022-03-13 23:33:50 +01:00
|
|
|
getItemDurationStats(libraryItems) {
|
|
|
|
var sorted = sort(libraryItems).desc(li => li.media.duration)
|
|
|
|
var top10 = sorted.slice(0, 10).map(li => ({ title: li.media.metadata.title, duration: li.media.duration })).filter(i => i.duration > 0)
|
2021-12-02 02:07:03 +01:00
|
|
|
var totalDuration = 0
|
2021-12-29 22:53:19 +01:00
|
|
|
var numAudioTracks = 0
|
2022-03-13 23:33:50 +01:00
|
|
|
libraryItems.forEach((li) => {
|
|
|
|
totalDuration += li.media.duration
|
|
|
|
numAudioTracks += (li.media.tracks || []).length
|
2021-12-02 02:07:03 +01:00
|
|
|
})
|
2021-12-29 22:53:19 +01:00
|
|
|
return {
|
|
|
|
totalDuration,
|
|
|
|
numAudioTracks,
|
2022-03-13 23:33:50 +01:00
|
|
|
longestItems: top10
|
2021-12-29 22:53:19 +01:00
|
|
|
}
|
2021-12-02 02:07:03 +01:00
|
|
|
},
|
|
|
|
|
2022-03-13 23:33:50 +01:00
|
|
|
getLibraryItemsTotalSize(libraryItems) {
|
2021-12-02 02:07:03 +01:00
|
|
|
var totalSize = 0
|
2022-03-13 23:33:50 +01:00
|
|
|
libraryItems.forEach((li) => {
|
|
|
|
totalSize += li.media.size
|
2021-12-02 02:07:03 +01:00
|
|
|
})
|
|
|
|
return totalSize
|
|
|
|
},
|
|
|
|
|
2022-03-11 01:45:02 +01:00
|
|
|
getNumIssues(libraryItems) {
|
|
|
|
// TODO: Implement issues
|
|
|
|
return libraryItems.filter(li => li.isMissing).length
|
|
|
|
// return books.filter(ab => {
|
|
|
|
// return ab.numMissingParts || ab.numInvalidParts || ab.isMissing || ab.isInvalid
|
|
|
|
// }).length
|
2021-12-01 03:02:40 +01:00
|
|
|
}
|
|
|
|
}
|