Author API requests to use region from library provider

This commit is contained in:
advplyr 2023-04-16 15:53:46 -05:00
parent a3e63e03d2
commit e271e89835
6 changed files with 59 additions and 25 deletions

View file

@ -7,10 +7,12 @@ class Audnexus {
this.baseUrl = 'https://api.audnex.us'
}
authorASINsRequest(name) {
name = encodeURIComponent(name);
var regionQuery = region ? `&region=${region}` : ''
return axios.get(`${this.baseUrl}/authors?name=${name}${regionQuery}`).then((res) => {
authorASINsRequest(name, region) {
name = encodeURIComponent(name)
const regionQuery = region ? `&region=${region}` : ''
const authorRequestUrl = `${this.baseUrl}/authors?name=${name}${regionQuery}`
Logger.info(`[Audnexus] Searching for author "${authorRequestUrl}"`)
return axios.get(authorRequestUrl).then((res) => {
return res.data || []
}).catch((error) => {
Logger.error(`[Audnexus] Author ASIN request failed for ${name}`, error)
@ -18,10 +20,12 @@ class Audnexus {
})
}
authorRequest(asin) {
asin = encodeURIComponent(asin);
var regionQuery = region ? `?region=${region}` : ''
return axios.get(`${this.baseUrl}/authors/${asin}${regionQuery}`).then((res) => {
authorRequest(asin, region) {
asin = encodeURIComponent(asin)
const regionQuery = region ? `?region=${region}` : ''
const authorRequestUrl = `${this.baseUrl}/authors/${asin}${regionQuery}`
Logger.info(`[Audnexus] Searching for author "${authorRequestUrl}"`)
return axios.get(authorRequestUrl).then((res) => {
return res.data
}).catch((error) => {
Logger.error(`[Audnexus] Author request failed for ${asin}`, error)
@ -29,8 +33,8 @@ class Audnexus {
})
}
async findAuthorByASIN(asin) {
var author = await this.authorRequest(asin)
async findAuthorByASIN(asin, region) {
const author = await this.authorRequest(asin, region)
if (!author) {
return null
}
@ -42,14 +46,14 @@ class Audnexus {
}
}
async findAuthorByName(name, maxLevenshtein = 3) {
async findAuthorByName(name, region, maxLevenshtein = 3) {
Logger.debug(`[Audnexus] Looking up author by name ${name}`)
var asins = await this.authorASINsRequest(name)
var matchingAsin = asins.find(obj => levenshteinDistance(obj.name, name) <= maxLevenshtein)
const asins = await this.authorASINsRequest(name, region)
const matchingAsin = asins.find(obj => levenshteinDistance(obj.name, name) <= maxLevenshtein)
if (!matchingAsin) {
return null
}
var author = await this.authorRequest(matchingAsin.asin)
const author = await this.authorRequest(matchingAsin.asin)
if (!author) {
return null
}