mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-12-24 04:39:40 +00:00
fix; HTTP/429 when requesting authors information, resolves #1570
This commit is contained in:
parent
565ff36d4e
commit
4e6b75d650
3 changed files with 111 additions and 38 deletions
|
|
@ -1,78 +1,123 @@
|
|||
const axios = require('axios')
|
||||
const { levenshteinDistance } = require('../utils/index')
|
||||
const Logger = require('../Logger')
|
||||
const { RateLimiter } = require('limiter');
|
||||
|
||||
class Audnexus {
|
||||
static _instance = null;
|
||||
|
||||
constructor() {
|
||||
// ensures Audnexus class is singleton
|
||||
if (Audnexus._instance) {
|
||||
return Audnexus._instance
|
||||
}
|
||||
|
||||
this.baseUrl = 'https://api.audnex.us'
|
||||
|
||||
// @see https://github.com/laxamentumtech/audnexus#-deployment-
|
||||
this.limiter = new RateLimiter({
|
||||
tokensPerInterval: 100,
|
||||
fireImmediately: true,
|
||||
interval: 'minute',
|
||||
})
|
||||
|
||||
Audnexus._instance = this
|
||||
}
|
||||
|
||||
authorASINsRequest(name, region) {
|
||||
name = encodeURIComponent(name)
|
||||
const regionQuery = region ? `®ion=${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)
|
||||
return []
|
||||
})
|
||||
|
||||
return this._processRequest(() => axios.get(authorRequestUrl))
|
||||
.then((res) => res.data || [])
|
||||
.catch((error) => {
|
||||
Logger.error(`[Audnexus] Author ASIN request failed for ${name}`, error)
|
||||
return []
|
||||
})
|
||||
}
|
||||
|
||||
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)
|
||||
return null
|
||||
})
|
||||
|
||||
return this._processRequest(() => axios.get(authorRequestUrl))
|
||||
.then((res) => res.data)
|
||||
.catch((error) => {
|
||||
Logger.error(`[Audnexus] Author request failed for ${asin}`, error)
|
||||
return null
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Process a request with a rate limiter
|
||||
*
|
||||
* @param {*} request
|
||||
* @returns
|
||||
*/
|
||||
async _processRequest(request) {
|
||||
const remainingTokens = await this.limiter.removeTokens(1)
|
||||
Logger.info(`[Audnexus] Attempting request with ${remainingTokens} remaining tokens and ${this.limiter.tokensThisInterval} this interval`)
|
||||
|
||||
if (remainingTokens >= 1) {
|
||||
return request()
|
||||
}
|
||||
|
||||
// 100 tokens(requests) per minute give a refresh of ~1.67 per second,
|
||||
// so a 10 second wait will yield ~16.7 additional tokens
|
||||
Logger.info('[Audnexus] Sleeping for 10 seconds')
|
||||
await new Promise(resolve => setTimeout(resolve, 10000))
|
||||
|
||||
return this._processRequest(request)
|
||||
}
|
||||
|
||||
async findAuthorByASIN(asin, region) {
|
||||
const author = await this.authorRequest(asin, region)
|
||||
if (!author) {
|
||||
return null
|
||||
}
|
||||
return {
|
||||
asin: author.asin,
|
||||
description: author.description,
|
||||
image: author.image || null,
|
||||
name: author.name
|
||||
}
|
||||
|
||||
return author ?
|
||||
{
|
||||
asin: author.asin,
|
||||
description: author.description,
|
||||
image: author.image || null,
|
||||
name: author.name
|
||||
} : null
|
||||
}
|
||||
|
||||
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) {
|
||||
return null
|
||||
}
|
||||
|
||||
const author = await this.authorRequest(matchingAsin.asin)
|
||||
if (!author) {
|
||||
return null
|
||||
}
|
||||
return {
|
||||
asin: author.asin,
|
||||
description: author.description,
|
||||
image: author.image || null,
|
||||
name: author.name
|
||||
}
|
||||
return author ?
|
||||
{
|
||||
description: author.description,
|
||||
image: author.image || null,
|
||||
asin: author.asin,
|
||||
name: author.name
|
||||
} : null
|
||||
}
|
||||
|
||||
getChaptersByASIN(asin, region) {
|
||||
Logger.debug(`[Audnexus] Get chapters for ASIN ${asin}/${region}`)
|
||||
return axios.get(`${this.baseUrl}/books/${asin}/chapters?region=${region}`).then((res) => {
|
||||
return res.data
|
||||
}).catch((error) => {
|
||||
Logger.error(`[Audnexus] Chapter ASIN request failed for ${asin}/${region}`, error)
|
||||
return null
|
||||
})
|
||||
|
||||
return this._processRequest(() => axios.get(`${this.baseUrl}/books/${asin}/chapters?region=${region}`))
|
||||
.then((res) => res.data)
|
||||
.catch((error) => {
|
||||
Logger.error(`[Audnexus] Chapter ASIN request failed for ${asin}/${region}`, error)
|
||||
return null
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Audnexus
|
||||
Loading…
Add table
Add a link
Reference in a new issue