mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-01-03 00:06:46 +01:00
Update:Match author use closest name match by levenshtein distance #2624
This commit is contained in:
parent
58598bfcf2
commit
acf75abdf1
@ -15,12 +15,19 @@ class AuthorFinder {
|
||||
return this.audnexus.findAuthorByASIN(asin, region)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} name
|
||||
* @param {string} region
|
||||
* @param {Object} [options={}]
|
||||
* @returns {Promise<import('../providers/Audnexus').AuthorSearchObj>}
|
||||
*/
|
||||
async findAuthorByName(name, region, options = {}) {
|
||||
if (!name) return null
|
||||
const maxLevenshtein = !isNaN(options.maxLevenshtein) ? Number(options.maxLevenshtein) : 3
|
||||
|
||||
const author = await this.audnexus.findAuthorByName(name, region, maxLevenshtein)
|
||||
if (!author || !author.name) {
|
||||
if (!author?.name) {
|
||||
return null
|
||||
}
|
||||
return author
|
||||
|
@ -2,15 +2,30 @@ const axios = require('axios')
|
||||
const { levenshteinDistance } = require('../utils/index')
|
||||
const Logger = require('../Logger')
|
||||
|
||||
/**
|
||||
* @typedef AuthorSearchObj
|
||||
* @property {string} asin
|
||||
* @property {string} description
|
||||
* @property {string} image
|
||||
* @property {string} name
|
||||
*/
|
||||
|
||||
class Audnexus {
|
||||
constructor() {
|
||||
this.baseUrl = 'https://api.audnex.us'
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} name
|
||||
* @param {string} region
|
||||
* @returns {Promise<{asin:string, name:string}[]>}
|
||||
*/
|
||||
authorASINsRequest(name, region) {
|
||||
name = encodeURIComponent(name)
|
||||
const regionQuery = region ? `®ion=${region}` : ''
|
||||
const authorRequestUrl = `${this.baseUrl}/authors?name=${name}${regionQuery}`
|
||||
const searchParams = new URLSearchParams()
|
||||
searchParams.set('name', name)
|
||||
if (region) searchParams.set('region', region)
|
||||
const authorRequestUrl = `${this.baseUrl}/authors?${searchParams.toString()}`
|
||||
Logger.info(`[Audnexus] Searching for author "${authorRequestUrl}"`)
|
||||
return axios.get(authorRequestUrl).then((res) => {
|
||||
return res.data || []
|
||||
@ -20,6 +35,12 @@ class Audnexus {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} asin
|
||||
* @param {string} region
|
||||
* @returns {Promise<AuthorSearchObj>}
|
||||
*/
|
||||
authorRequest(asin, region) {
|
||||
asin = encodeURIComponent(asin)
|
||||
const regionQuery = region ? `?region=${region}` : ''
|
||||
@ -33,6 +54,12 @@ class Audnexus {
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} asin
|
||||
* @param {string} region
|
||||
* @returns {Promise<AuthorSearchObj>}
|
||||
*/
|
||||
async findAuthorByASIN(asin, region) {
|
||||
const author = await this.authorRequest(asin, region)
|
||||
if (!author) {
|
||||
@ -46,14 +73,28 @@ class Audnexus {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {string} name
|
||||
* @param {string} region
|
||||
* @param {number} maxLevenshtein
|
||||
* @returns {Promise<AuthorSearchObj>}
|
||||
*/
|
||||
async findAuthorByName(name, region, maxLevenshtein = 3) {
|
||||
Logger.debug(`[Audnexus] Looking up author by name ${name}`)
|
||||
const asins = await this.authorASINsRequest(name, region)
|
||||
const matchingAsin = asins.find(obj => levenshteinDistance(obj.name, name) <= maxLevenshtein)
|
||||
if (!matchingAsin) {
|
||||
const authorAsinObjs = await this.authorASINsRequest(name, region)
|
||||
|
||||
let closestMatch = null
|
||||
authorAsinObjs.forEach((authorAsinObj) => {
|
||||
authorAsinObj.levenshteinDistance = levenshteinDistance(authorAsinObj.name, name)
|
||||
if (!closestMatch || closestMatch.levenshteinDistance > authorAsinObj.levenshteinDistance) {
|
||||
closestMatch = authorAsinObj
|
||||
}
|
||||
})
|
||||
if (!closestMatch || closestMatch.levenshteinDistance > maxLevenshtein) {
|
||||
return null
|
||||
}
|
||||
const author = await this.authorRequest(matchingAsin.asin)
|
||||
const author = await this.authorRequest(closestMatch.asin)
|
||||
if (!author) {
|
||||
return null
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user