mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-02-01 00:18:14 +01:00
Add jsdocs to BookFinder search functions
This commit is contained in:
parent
81a9b8d158
commit
61c48602e8
@ -52,21 +52,19 @@ class BookFinder {
|
|||||||
cleanTitleForCompares(title) {
|
cleanTitleForCompares(title) {
|
||||||
if (!title) return ''
|
if (!title) return ''
|
||||||
// Remove subtitle if there (i.e. "Cool Book: Coolest Ever" becomes "Cool Book")
|
// Remove subtitle if there (i.e. "Cool Book: Coolest Ever" becomes "Cool Book")
|
||||||
var stripped = this.stripSubtitle(title)
|
let stripped = this.stripSubtitle(title)
|
||||||
|
|
||||||
// Remove text in paranthesis (i.e. "Ender's Game (Ender's Saga)" becomes "Ender's Game")
|
// Remove text in paranthesis (i.e. "Ender's Game (Ender's Saga)" becomes "Ender's Game")
|
||||||
var cleaned = stripped.replace(/ *\([^)]*\) */g, "")
|
let cleaned = stripped.replace(/ *\([^)]*\) */g, "")
|
||||||
|
|
||||||
// Remove single quotes (i.e. "Ender's Game" becomes "Enders Game")
|
// Remove single quotes (i.e. "Ender's Game" becomes "Enders Game")
|
||||||
cleaned = cleaned.replace(/'/g, '')
|
cleaned = cleaned.replace(/'/g, '')
|
||||||
cleaned = this.replaceAccentedChars(cleaned)
|
return this.replaceAccentedChars(cleaned)
|
||||||
return cleaned
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cleanAuthorForCompares(author) {
|
cleanAuthorForCompares(author) {
|
||||||
if (!author) return ''
|
if (!author) return ''
|
||||||
var cleaned = this.replaceAccentedChars(author)
|
return this.replaceAccentedChars(author)
|
||||||
return cleaned
|
|
||||||
}
|
}
|
||||||
|
|
||||||
filterSearchResults(books, title, author, maxTitleDistance, maxAuthorDistance) {
|
filterSearchResults(books, title, author, maxTitleDistance, maxAuthorDistance) {
|
||||||
@ -210,12 +208,23 @@ class BookFinder {
|
|||||||
candidates.add(candidate)
|
candidates.add(candidate)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search for books including fuzzy searches
|
||||||
|
*
|
||||||
|
* @param {string} provider
|
||||||
|
* @param {string} title
|
||||||
|
* @param {string} author
|
||||||
|
* @param {string} isbn
|
||||||
|
* @param {string} asin
|
||||||
|
* @param {{titleDistance:number, authorDistance:number, maxFuzzySearches:number}} options
|
||||||
|
* @returns {Promise<Object[]>}
|
||||||
|
*/
|
||||||
async search(provider, title, author, isbn, asin, options = {}) {
|
async search(provider, title, author, isbn, asin, options = {}) {
|
||||||
var books = []
|
let books = []
|
||||||
const maxTitleDistance = !isNaN(options.titleDistance) ? Number(options.titleDistance) : 4
|
const maxTitleDistance = !isNaN(options.titleDistance) ? Number(options.titleDistance) : 4
|
||||||
const maxAuthorDistance = !isNaN(options.authorDistance) ? Number(options.authorDistance) : 4
|
const maxAuthorDistance = !isNaN(options.authorDistance) ? Number(options.authorDistance) : 4
|
||||||
const maxFuzzySearches = !isNaN(options.maxFuzzySearches) ? Number(options.maxFuzzySearches) : 5
|
const maxFuzzySearches = !isNaN(options.maxFuzzySearches) ? Number(options.maxFuzzySearches) : 5
|
||||||
var numFuzzySearches = 0
|
let numFuzzySearches = 0
|
||||||
|
|
||||||
if (!title)
|
if (!title)
|
||||||
return books
|
return books
|
||||||
@ -228,8 +237,8 @@ class BookFinder {
|
|||||||
author = author.trim().toLowerCase()
|
author = author.trim().toLowerCase()
|
||||||
|
|
||||||
// Now run up to maxFuzzySearches fuzzy searches
|
// Now run up to maxFuzzySearches fuzzy searches
|
||||||
var candidates = new Set()
|
let candidates = new Set()
|
||||||
var cleanedAuthor = this.cleanAuthorForCompares(author)
|
let cleanedAuthor = this.cleanAuthorForCompares(author)
|
||||||
this.addTitleCandidate(title, candidates)
|
this.addTitleCandidate(title, candidates)
|
||||||
|
|
||||||
// remove parentheses and their contents, and replace with a separator
|
// remove parentheses and their contents, and replace with a separator
|
||||||
@ -256,8 +265,7 @@ class BookFinder {
|
|||||||
if (lengthDiff) return lengthDiff
|
if (lengthDiff) return lengthDiff
|
||||||
return b.localeCompare(a)
|
return b.localeCompare(a)
|
||||||
})
|
})
|
||||||
Logger.debug(`[BookFinder] Found ${candidates.length} fuzzy title candidates`)
|
Logger.debug(`[BookFinder] Found ${candidates.length} fuzzy title candidates`, candidates)
|
||||||
Logger.debug(candidates)
|
|
||||||
for (const candidate of candidates) {
|
for (const candidate of candidates) {
|
||||||
if (++numFuzzySearches > maxFuzzySearches) return books
|
if (++numFuzzySearches > maxFuzzySearches) return books
|
||||||
books = await this.runSearch(candidate, cleanedAuthor, provider, asin, maxTitleDistance, maxAuthorDistance)
|
books = await this.runSearch(candidate, cleanedAuthor, provider, asin, maxTitleDistance, maxAuthorDistance)
|
||||||
@ -283,10 +291,21 @@ class BookFinder {
|
|||||||
return books
|
return books
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Search for books
|
||||||
|
*
|
||||||
|
* @param {string} title
|
||||||
|
* @param {string} author
|
||||||
|
* @param {string} provider
|
||||||
|
* @param {string} asin only used for audible providers
|
||||||
|
* @param {number} maxTitleDistance only used for openlibrary provider
|
||||||
|
* @param {number} maxAuthorDistance only used for openlibrary provider
|
||||||
|
* @returns {Promise<Object[]>}
|
||||||
|
*/
|
||||||
async runSearch(title, author, provider, asin, maxTitleDistance, maxAuthorDistance) {
|
async runSearch(title, author, provider, asin, maxTitleDistance, maxAuthorDistance) {
|
||||||
Logger.debug(`Book Search: title: "${title}", author: "${author || ''}", provider: ${provider}`)
|
Logger.debug(`Book Search: title: "${title}", author: "${author || ''}", provider: ${provider}`)
|
||||||
|
|
||||||
var books = []
|
let books = []
|
||||||
|
|
||||||
if (provider === 'google') {
|
if (provider === 'google') {
|
||||||
books = await this.getGoogleBooksResults(title, author)
|
books = await this.getGoogleBooksResults(title, author)
|
||||||
|
Loading…
Reference in New Issue
Block a user