2022-03-06 23:32:04 +01:00
|
|
|
const OpenLibrary = require('../providers/OpenLibrary')
|
|
|
|
const GoogleBooks = require('../providers/GoogleBooks')
|
|
|
|
const Audible = require('../providers/Audible')
|
2022-03-07 00:26:35 +01:00
|
|
|
const iTunes = require('../providers/iTunes')
|
2022-05-11 00:03:41 +02:00
|
|
|
const Audnexus = require('../providers/Audnexus')
|
2023-02-06 22:25:18 +01:00
|
|
|
const FantLab = require('../providers/FantLab')
|
2022-03-06 23:32:04 +01:00
|
|
|
const Logger = require('../Logger')
|
|
|
|
const { levenshteinDistance } = require('../utils/index')
|
2021-08-18 00:01:11 +02:00
|
|
|
|
|
|
|
class BookFinder {
|
|
|
|
constructor() {
|
|
|
|
this.openLibrary = new OpenLibrary()
|
2021-10-28 21:41:42 +02:00
|
|
|
this.googleBooks = new GoogleBooks()
|
2021-11-21 19:59:32 +01:00
|
|
|
this.audible = new Audible()
|
2022-03-07 00:26:35 +01:00
|
|
|
this.iTunesApi = new iTunes()
|
2022-05-11 00:03:41 +02:00
|
|
|
this.audnexus = new Audnexus()
|
2023-02-06 22:25:18 +01:00
|
|
|
this.fantLab = new FantLab()
|
2021-10-06 04:10:49 +02:00
|
|
|
|
|
|
|
this.verbose = false
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
async findByISBN(isbn) {
|
|
|
|
var book = await this.openLibrary.isbnLookup(isbn)
|
|
|
|
if (book.errorCode) {
|
2021-08-26 02:15:00 +02:00
|
|
|
Logger.error('Book not found')
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
return book
|
|
|
|
}
|
|
|
|
|
2021-08-21 16:15:44 +02:00
|
|
|
stripSubtitle(title) {
|
|
|
|
if (title.includes(':')) {
|
|
|
|
return title.split(':')[0].trim()
|
|
|
|
} else if (title.includes(' - ')) {
|
|
|
|
return title.split(' - ')[0].trim()
|
|
|
|
}
|
|
|
|
return title
|
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
|
2021-08-25 03:24:40 +02:00
|
|
|
replaceAccentedChars(str) {
|
|
|
|
try {
|
|
|
|
return str.normalize('NFD').replace(/[\u0300-\u036f]/g, "")
|
|
|
|
} catch (error) {
|
|
|
|
Logger.error('[BookFinder] str normalize error', error)
|
|
|
|
return str
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-21 16:15:44 +02:00
|
|
|
cleanTitleForCompares(title) {
|
2021-08-25 03:24:40 +02:00
|
|
|
if (!title) return ''
|
2021-08-21 16:15:44 +02:00
|
|
|
// Remove subtitle if there (i.e. "Cool Book: Coolest Ever" becomes "Cool Book")
|
|
|
|
var stripped = this.stripSubtitle(title)
|
|
|
|
|
|
|
|
// Remove text in paranthesis (i.e. "Ender's Game (Ender's Saga)" becomes "Ender's Game")
|
|
|
|
var cleaned = stripped.replace(/ *\([^)]*\) */g, "")
|
|
|
|
|
|
|
|
// Remove single quotes (i.e. "Ender's Game" becomes "Enders Game")
|
|
|
|
cleaned = cleaned.replace(/'/g, '')
|
2021-08-25 03:24:40 +02:00
|
|
|
cleaned = this.replaceAccentedChars(cleaned)
|
|
|
|
return cleaned.toLowerCase()
|
|
|
|
}
|
|
|
|
|
|
|
|
cleanAuthorForCompares(author) {
|
|
|
|
if (!author) return ''
|
|
|
|
var cleaned = this.replaceAccentedChars(author)
|
2021-08-21 16:15:44 +02:00
|
|
|
return cleaned.toLowerCase()
|
|
|
|
}
|
|
|
|
|
|
|
|
filterSearchResults(books, title, author, maxTitleDistance, maxAuthorDistance) {
|
|
|
|
var searchTitle = this.cleanTitleForCompares(title)
|
2021-08-25 03:24:40 +02:00
|
|
|
var searchAuthor = this.cleanAuthorForCompares(author)
|
2021-08-21 16:15:44 +02:00
|
|
|
return books.map(b => {
|
|
|
|
b.cleanedTitle = this.cleanTitleForCompares(b.title)
|
|
|
|
b.titleDistance = levenshteinDistance(b.cleanedTitle, title)
|
2021-08-26 02:15:00 +02:00
|
|
|
|
|
|
|
// Total length of search (title or both title & author)
|
|
|
|
b.totalPossibleDistance = b.title.length
|
|
|
|
|
2021-08-21 16:15:44 +02:00
|
|
|
if (author) {
|
2021-08-25 03:24:40 +02:00
|
|
|
if (!b.author) {
|
|
|
|
b.authorDistance = author.length
|
|
|
|
} else {
|
2021-08-26 02:15:00 +02:00
|
|
|
b.totalPossibleDistance += b.author.length
|
2021-08-25 03:24:40 +02:00
|
|
|
b.cleanedAuthor = this.cleanAuthorForCompares(b.author)
|
|
|
|
|
|
|
|
var cleanedAuthorDistance = levenshteinDistance(b.cleanedAuthor, searchAuthor)
|
|
|
|
var authorDistance = levenshteinDistance(b.author || '', author)
|
2021-08-26 02:15:00 +02:00
|
|
|
|
2021-08-25 03:24:40 +02:00
|
|
|
// Use best distance
|
2021-08-26 02:15:00 +02:00
|
|
|
b.authorDistance = Math.min(cleanedAuthorDistance, authorDistance)
|
|
|
|
|
|
|
|
// Check book author contains searchAuthor
|
|
|
|
if (searchAuthor.length > 4 && b.cleanedAuthor.includes(searchAuthor)) b.includesAuthor = searchAuthor
|
|
|
|
else if (author.length > 4 && b.author.includes(author)) b.includesAuthor = author
|
2021-08-25 03:24:40 +02:00
|
|
|
}
|
2021-08-21 16:15:44 +02:00
|
|
|
}
|
|
|
|
b.totalDistance = b.titleDistance + (b.authorDistance || 0)
|
|
|
|
|
2021-08-26 02:15:00 +02:00
|
|
|
// Check book title contains the searchTitle
|
|
|
|
if (searchTitle.length > 4 && b.cleanedTitle.includes(searchTitle)) b.includesTitle = searchTitle
|
|
|
|
else if (title.length > 4 && b.title.includes(title)) b.includesTitle = title
|
2021-08-21 16:15:44 +02:00
|
|
|
|
|
|
|
return b
|
|
|
|
}).filter(b => {
|
2021-08-26 02:15:00 +02:00
|
|
|
if (b.includesTitle) { // If search title was found in result title then skip over leven distance check
|
2021-10-06 04:10:49 +02:00
|
|
|
if (this.verbose) Logger.debug(`Exact title was included in "${b.title}", Search: "${b.includesTitle}"`)
|
2021-08-21 16:15:44 +02:00
|
|
|
} else if (b.titleDistance > maxTitleDistance) {
|
2021-10-06 04:10:49 +02:00
|
|
|
if (this.verbose) Logger.debug(`Filtering out search result title distance = ${b.titleDistance}: "${b.cleanedTitle}"/"${searchTitle}"`)
|
2021-08-21 16:15:44 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-08-26 02:15:00 +02:00
|
|
|
if (author) {
|
|
|
|
if (b.includesAuthor) { // If search author was found in result author then skip over leven distance check
|
2021-10-06 04:10:49 +02:00
|
|
|
if (this.verbose) Logger.debug(`Exact author was included in "${b.author}", Search: "${b.includesAuthor}"`)
|
2021-08-26 02:15:00 +02:00
|
|
|
} else if (b.authorDistance > maxAuthorDistance) {
|
2021-10-06 04:10:49 +02:00
|
|
|
if (this.verbose) Logger.debug(`Filtering out search result "${b.author}", author distance = ${b.authorDistance}: "${b.author}"/"${author}"`)
|
2021-08-26 02:15:00 +02:00
|
|
|
return false
|
|
|
|
}
|
2021-08-21 16:15:44 +02:00
|
|
|
}
|
|
|
|
|
2021-08-26 02:15:00 +02:00
|
|
|
// If book total search length < 5 and was not exact match, then filter out
|
|
|
|
if (b.totalPossibleDistance < 5 && b.totalDistance > 0) return false
|
2021-08-21 16:15:44 +02:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async getOpenLibResults(title, author, maxTitleDistance, maxAuthorDistance) {
|
|
|
|
var books = await this.openLibrary.searchTitle(title)
|
2021-10-06 04:10:49 +02:00
|
|
|
if (this.verbose) Logger.debug(`OpenLib Book Search Results: ${books.length || 0}`)
|
2021-08-18 00:01:11 +02:00
|
|
|
if (books.errorCode) {
|
2021-08-21 16:15:44 +02:00
|
|
|
Logger.error(`OpenLib Search Error ${books.errorCode}`)
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
var booksFiltered = this.filterSearchResults(books, title, author, maxTitleDistance, maxAuthorDistance)
|
|
|
|
if (!booksFiltered.length && books.length) {
|
2021-10-06 04:10:49 +02:00
|
|
|
if (this.verbose) Logger.debug(`Search has ${books.length} matches, but no close title matches`)
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
2021-08-21 16:15:44 +02:00
|
|
|
return booksFiltered
|
|
|
|
}
|
|
|
|
|
2021-12-08 01:42:56 +01:00
|
|
|
async getGoogleBooksResults(title, author) {
|
2021-10-28 21:41:42 +02:00
|
|
|
var books = await this.googleBooks.search(title, author)
|
|
|
|
if (this.verbose) Logger.debug(`GoogleBooks Book Search Results: ${books.length || 0}`)
|
|
|
|
if (books.errorCode) {
|
|
|
|
Logger.error(`GoogleBooks Search Error ${books.errorCode}`)
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
// Google has good sort
|
|
|
|
return books
|
|
|
|
}
|
|
|
|
|
2023-02-06 22:25:18 +01:00
|
|
|
async getFantLabResults(title, author) {
|
|
|
|
var books = await this.fantLab.search(title, author)
|
|
|
|
if (this.verbose) Logger.debug(`FantLab Book Search Results: ${books.length || 0}`)
|
|
|
|
if (books.errorCode) {
|
|
|
|
Logger.error(`FantLab Search Error ${books.errorCode}`)
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
|
|
|
|
return books
|
|
|
|
}
|
|
|
|
|
2022-03-07 00:26:35 +01:00
|
|
|
async getiTunesAudiobooksResults(title, author) {
|
|
|
|
return this.iTunesApi.searchAudiobooks(title)
|
|
|
|
}
|
|
|
|
|
2022-10-08 00:18:28 +02:00
|
|
|
async getAudibleResults(title, author, asin, provider) {
|
|
|
|
const region = provider.includes('.') ? provider.split('.').pop() : ''
|
|
|
|
const books = await this.audible.search(title, author, asin, region)
|
2021-11-21 19:59:32 +01:00
|
|
|
if (this.verbose) Logger.debug(`Audible Book Search Results: ${books.length || 0}`)
|
|
|
|
if (!books) return []
|
|
|
|
return books
|
|
|
|
}
|
|
|
|
|
2022-05-23 04:56:51 +02:00
|
|
|
async search(provider, title, author, isbn, asin, options = {}) {
|
2021-08-21 16:15:44 +02:00
|
|
|
var books = []
|
|
|
|
var maxTitleDistance = !isNaN(options.titleDistance) ? Number(options.titleDistance) : 4
|
|
|
|
var maxAuthorDistance = !isNaN(options.authorDistance) ? Number(options.authorDistance) : 4
|
2021-12-08 01:10:56 +01:00
|
|
|
Logger.debug(`Book Search: title: "${title}", author: "${author}", provider: ${provider}`)
|
2021-08-21 16:15:44 +02:00
|
|
|
|
2021-10-28 21:41:42 +02:00
|
|
|
if (provider === 'google') {
|
2022-06-30 04:25:59 +02:00
|
|
|
books = await this.getGoogleBooksResults(title, author)
|
2022-10-08 00:18:28 +02:00
|
|
|
} else if (provider.startsWith('audible')) {
|
|
|
|
books = await this.getAudibleResults(title, author, asin, provider)
|
2022-03-07 00:26:35 +01:00
|
|
|
} else if (provider === 'itunes') {
|
2022-06-30 04:19:58 +02:00
|
|
|
books = await this.getiTunesAudiobooksResults(title, author)
|
2021-08-21 16:15:44 +02:00
|
|
|
} else if (provider === 'openlibrary') {
|
|
|
|
books = await this.getOpenLibResults(title, author, maxTitleDistance, maxAuthorDistance)
|
2023-02-06 22:25:18 +01:00
|
|
|
} else if (provider === 'fantlab') {
|
|
|
|
books = await this.getFantLabResults(title, author)
|
|
|
|
}
|
|
|
|
else {
|
2022-07-05 02:14:52 +02:00
|
|
|
books = await this.getGoogleBooksResults(title, author)
|
2021-08-21 16:15:44 +02:00
|
|
|
}
|
|
|
|
|
2022-06-16 22:48:37 +02:00
|
|
|
if (!books.length && !options.currentlyTryingCleaned) {
|
2022-06-17 19:31:31 +02:00
|
|
|
var cleanedTitle = this.cleanTitleForCompares(title)
|
|
|
|
var cleanedAuthor = this.cleanAuthorForCompares(author)
|
|
|
|
if (cleanedTitle == title && cleanedAuthor == author) return books
|
|
|
|
|
2022-06-16 22:48:37 +02:00
|
|
|
Logger.debug(`Book Search, no matches.. checking cleaned title and author`)
|
|
|
|
options.currentlyTryingCleaned = true
|
2022-06-17 19:31:31 +02:00
|
|
|
return this.search(provider, cleanedTitle, cleanedAuthor, isbn, asin, options)
|
2022-06-16 22:48:37 +02:00
|
|
|
}
|
|
|
|
|
2023-02-06 22:25:18 +01:00
|
|
|
if (["google", "audible", "itunes", 'fantlab'].includes(provider)) return books
|
2022-06-16 22:48:37 +02:00
|
|
|
|
2021-08-21 16:15:44 +02:00
|
|
|
return books.sort((a, b) => {
|
|
|
|
return a.totalDistance - b.totalDistance
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
async findCovers(provider, title, author, options = {}) {
|
|
|
|
var searchResults = await this.search(provider, title, author, options)
|
2021-08-26 02:15:00 +02:00
|
|
|
Logger.debug(`[BookFinder] FindCovers search results: ${searchResults.length}`)
|
2021-08-25 03:24:40 +02:00
|
|
|
|
2021-08-21 16:15:44 +02:00
|
|
|
var covers = []
|
|
|
|
searchResults.forEach((result) => {
|
|
|
|
if (result.covers && result.covers.length) {
|
|
|
|
covers = covers.concat(result.covers)
|
|
|
|
}
|
|
|
|
if (result.cover) {
|
|
|
|
covers.push(result.cover)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
return covers
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
2022-05-11 00:03:41 +02:00
|
|
|
|
2022-10-15 22:31:07 +02:00
|
|
|
findChapters(asin, region) {
|
|
|
|
return this.audnexus.getChaptersByASIN(asin, region)
|
2022-05-11 00:03:41 +02:00
|
|
|
}
|
2021-08-18 00:01:11 +02:00
|
|
|
}
|
|
|
|
module.exports = BookFinder
|