From acf75abdf1921ae4a82381cc02732f422147c381 Mon Sep 17 00:00:00 2001 From: advplyr Date: Sun, 18 Feb 2024 13:06:51 -0600 Subject: [PATCH] Update:Match author use closest name match by levenshtein distance #2624 --- server/finders/AuthorFinder.js | 9 +++++- server/providers/Audnexus.js | 55 +++++++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 8 deletions(-) diff --git a/server/finders/AuthorFinder.js b/server/finders/AuthorFinder.js index 69aa724d..8dbe0097 100644 --- a/server/finders/AuthorFinder.js +++ b/server/finders/AuthorFinder.js @@ -15,12 +15,19 @@ class AuthorFinder { return this.audnexus.findAuthorByASIN(asin, region) } + /** + * + * @param {string} name + * @param {string} region + * @param {Object} [options={}] + * @returns {Promise} + */ 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 diff --git a/server/providers/Audnexus.js b/server/providers/Audnexus.js index b74d1d13..f5034bff 100644 --- a/server/providers/Audnexus.js +++ b/server/providers/Audnexus.js @@ -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} + */ 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} + */ 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} + */ 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 }