From 35f3b5863f7529b81383163f2de5d9e0ffb58660 Mon Sep 17 00:00:00 2001 From: advplyr Date: Wed, 20 Apr 2022 18:05:09 -0500 Subject: [PATCH] Add library match all back updated to support v2 models --- .../components/tables/library/LibraryItem.vue | 10 ++- server/Db.js | 3 + server/controllers/LibraryController.js | 6 +- server/controllers/LibraryItemController.js | 2 +- server/routers/ApiRouter.js | 2 +- server/scanner/Scanner.js | 82 ++++++++++--------- 6 files changed, 60 insertions(+), 45 deletions(-) diff --git a/client/components/tables/library/LibraryItem.vue b/client/components/tables/library/LibraryItem.vue index 3fb6aaf6..1cfa7274 100644 --- a/client/components/tables/library/LibraryItem.vue +++ b/client/components/tables/library/LibraryItem.vue @@ -10,7 +10,7 @@ Scan Force Re-Scan - + Match Books edit delete @@ -48,12 +48,18 @@ export default { }, libraryScan() { return this.$store.getters['scanners/getLibraryScan'](this.library.id) + }, + mediaType() { + return this.library.mediaType + }, + isBookLibrary() { + return this.mediaType === 'book' } }, methods: { matchAll() { this.$axios - .$post(`/api/libraries/${this.library.id}/matchbooks`) + .$post(`/api/libraries/${this.library.id}/matchall`) .then(() => { console.log('Starting scan for matches') }) diff --git a/server/Db.js b/server/Db.js index d3ac3938..efcc77b2 100644 --- a/server/Db.js +++ b/server/Db.js @@ -200,6 +200,9 @@ class Db { getLibraryItem(id) { return this.libraryItems.find(li => li.id === id) } + getLibraryItemsInLibrary(libraryId) { + return this.libraryItems.filter(li => li.libraryId === libraryId) + } getPlaybackSession(id) { return this.sessionsDb.select((pb) => pb.id == id).then((results) => { if (results.data.length) { diff --git a/server/controllers/LibraryController.js b/server/controllers/LibraryController.js index dba55dba..8e93bac2 100644 --- a/server/controllers/LibraryController.js +++ b/server/controllers/LibraryController.js @@ -546,12 +546,12 @@ class LibraryController { res.json(Object.values(authors)) } - async matchBooks(req, res) { + async matchAll(req, res) { if (!req.user.isRoot) { - Logger.error(`[LibraryController] Non-root user attempted to match library books`, req.user) + Logger.error(`[LibraryController] Non-root user attempted to match library items`, req.user) return res.sendStatus(403) } - this.scanner.matchLibraryBooks(req.library) + this.scanner.matchLibraryItems(req.library) res.sendStatus(200) } diff --git a/server/controllers/LibraryItemController.js b/server/controllers/LibraryItemController.js index db8b0412..60090508 100644 --- a/server/controllers/LibraryItemController.js +++ b/server/controllers/LibraryItemController.js @@ -247,7 +247,7 @@ class LibraryItemController { var libraryItem = req.libraryItem var options = req.body || {} - var matchResult = await this.scanner.quickMatchBook(libraryItem, options) + var matchResult = await this.scanner.quickMatchLibraryItem(libraryItem, options) res.json(matchResult) } diff --git a/server/routers/ApiRouter.js b/server/routers/ApiRouter.js index dbf05c11..17d1ad1e 100644 --- a/server/routers/ApiRouter.js +++ b/server/routers/ApiRouter.js @@ -65,7 +65,7 @@ class ApiRouter { this.router.get('/libraries/:id/search', LibraryController.middleware.bind(this), LibraryController.search.bind(this)) this.router.get('/libraries/:id/stats', LibraryController.middleware.bind(this), LibraryController.stats.bind(this)) this.router.get('/libraries/:id/authors', LibraryController.middleware.bind(this), LibraryController.getAuthors.bind(this)) - this.router.post('/libraries/:id/matchbooks', LibraryController.middleware.bind(this), LibraryController.matchBooks.bind(this)) + this.router.post('/libraries/:id/matchall', LibraryController.middleware.bind(this), LibraryController.matchAll.bind(this)) this.router.get('/libraries/:id/scan', LibraryController.middleware.bind(this), LibraryController.scan.bind(this)) // Root only this.router.post('/libraries/order', LibraryController.reorder.bind(this)) diff --git a/server/scanner/Scanner.js b/server/scanner/Scanner.js index b638d71e..bf2dd695 100644 --- a/server/scanner/Scanner.js +++ b/server/scanner/Scanner.js @@ -616,7 +616,7 @@ class Scanner { return false } - async quickMatchBook(libraryItem, options = {}) { + async quickMatchLibraryItem(libraryItem, options = {}) { var provider = options.provider || 'google' var searchTitle = options.title || libraryItem.media.metadata.title var searchAuthor = options.author || libraryItem.media.metadata.authorName @@ -698,49 +698,55 @@ class Scanner { } } - // TODO: Redo quick match full library - async matchLibraryBooks(library) { - // if (this.isLibraryScanning(library.id)) { - // Logger.error(`[Scanner] Already scanning ${library.id}`) - // return - // } + async matchLibraryItems(library) { + if (library.mediaType === 'podcast') { + Logger.error(`[Scanner] matchLibraryItems: Match all not supported for podcasts yet`) + return + } - // const provider = library.provider || 'google' - // var audiobooksInLibrary = this.db.audiobooks.filter(ab => ab.libraryId === library.id) - // if (!audiobooksInLibrary.length) { - // return - // } + if (this.isLibraryScanning(library.id)) { + Logger.error(`[Scanner] matchLibraryItems: Already scanning ${library.id}`) + return + } - // var libraryScan = new LibraryScan() - // libraryScan.setData(library, null, 'match') - // this.librariesScanning.push(libraryScan.getScanEmitData) - // this.emitter('scan_start', libraryScan.getScanEmitData) + var itemsInLibrary = this.db.getLibraryItemsInLibrary(library.id) + if (!itemsInLibrary.length) { + Logger.error(`[Scanner] matchLibraryItems: Library has no items ${library.id}`) + return + } - // Logger.info(`[Scanner] Starting library match books scan ${libraryScan.id} for ${libraryScan.libraryName}`) + const provider = library.provider - // for (let i = 0; i < audiobooksInLibrary.length; i++) { - // var audiobook = audiobooksInLibrary[i] - // Logger.debug(`[Scanner] Quick matching "${audiobook.title}" (${i + 1} of ${audiobooksInLibrary.length})`) - // var result = await this.quickMatchBook(audiobook, { provider }) - // if (result.warning) { - // Logger.warn(`[Scanner] Match warning ${result.warning} for audiobook "${audiobook.title}"`) - // } else if (result.updated) { - // libraryScan.resultsUpdated++ - // } + var libraryScan = new LibraryScan() + libraryScan.setData(library, null, 'match') + this.librariesScanning.push(libraryScan.getScanEmitData) + this.emitter('scan_start', libraryScan.getScanEmitData) - // if (this.cancelLibraryScan[libraryScan.libraryId]) { - // Logger.info(`[Scanner] Library match scan canceled for "${libraryScan.libraryName}"`) - // delete this.cancelLibraryScan[libraryScan.libraryId] - // var scanData = libraryScan.getScanEmitData - // scanData.results = false - // this.emitter('scan_complete', scanData) - // this.librariesScanning = this.librariesScanning.filter(ls => ls.id !== library.id) - // return - // } - // } + Logger.info(`[Scanner] matchLibraryItems: Starting library match scan ${libraryScan.id} for ${libraryScan.libraryName}`) - // this.librariesScanning = this.librariesScanning.filter(ls => ls.id !== library.id) - // this.emitter('scan_complete', libraryScan.getScanEmitData) + for (let i = 0; i < itemsInLibrary.length; i++) { + var libraryItem = itemsInLibrary[i] + Logger.debug(`[Scanner] matchLibraryItems: Quick matching "${libraryItem.media.metadata.title}" (${i + 1} of ${itemsInLibrary.length})`) + var result = await this.quickMatchLibraryItem(libraryItem, { provider }) + if (result.warning) { + Logger.warn(`[Scanner] matchLibraryItems: Match warning ${result.warning} for library item "${libraryItem.media.metadata.title}"`) + } else if (result.updated) { + libraryScan.resultsUpdated++ + } + + if (this.cancelLibraryScan[libraryScan.libraryId]) { + Logger.info(`[Scanner] matchLibraryItems: Library match scan canceled for "${libraryScan.libraryName}"`) + delete this.cancelLibraryScan[libraryScan.libraryId] + var scanData = libraryScan.getScanEmitData + scanData.results = false + this.emitter('scan_complete', scanData) + this.librariesScanning = this.librariesScanning.filter(ls => ls.id !== library.id) + return + } + } + + this.librariesScanning = this.librariesScanning.filter(ls => ls.id !== library.id) + this.emitter('scan_complete', libraryScan.getScanEmitData) } } module.exports = Scanner \ No newline at end of file