fix; HTTP/429 when requesting authors information, resolves #1570

This commit is contained in:
jfrazx 2023-10-05 13:48:55 -07:00
parent 565ff36d4e
commit 4e6b75d650
No known key found for this signature in database
GPG Key ID: 7E72C3BCC0F85A7B
3 changed files with 111 additions and 38 deletions

29
package-lock.json generated
View File

@ -13,6 +13,7 @@
"express": "^4.17.1", "express": "^4.17.1",
"graceful-fs": "^4.2.10", "graceful-fs": "^4.2.10",
"htmlparser2": "^8.0.1", "htmlparser2": "^8.0.1",
"limiter": "^2.1.0",
"node-tone": "^1.0.1", "node-tone": "^1.0.1",
"nodemailer": "^6.9.2", "nodemailer": "^6.9.2",
"sequelize": "^6.32.1", "sequelize": "^6.32.1",
@ -1308,6 +1309,19 @@
"integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==",
"optional": true "optional": true
}, },
"node_modules/just-performance": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/just-performance/-/just-performance-4.3.0.tgz",
"integrity": "sha512-L7RjvtJsL0QO8xFs5wEoDDzzJwoiowRw6Rn/GnvldlchS2JQr9wFYPiwZcDfrbbujEKqKN0tvENdbjXdYhDp5Q=="
},
"node_modules/limiter": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/limiter/-/limiter-2.1.0.tgz",
"integrity": "sha512-361TYz6iay6n+9KvUUImqdLuFigK+K79qrUtBsXhJTLdH4rIt/r1y8r1iozwh8KbZNpujbFTSh74mJ7bwbAMOw==",
"dependencies": {
"just-performance": "4.3.0"
}
},
"node_modules/lodash": { "node_modules/lodash": {
"version": "4.17.21", "version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
@ -3673,6 +3687,19 @@
"integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==", "integrity": "sha512-RHxMLp9lnKHGHRng9QFhRCMbYAcVpn69smSGcq3f36xjgVVWThj4qqLbTLlq7Ssj8B+fIQ1EuCEGI2lKsyQeIw==",
"optional": true "optional": true
}, },
"just-performance": {
"version": "4.3.0",
"resolved": "https://registry.npmjs.org/just-performance/-/just-performance-4.3.0.tgz",
"integrity": "sha512-L7RjvtJsL0QO8xFs5wEoDDzzJwoiowRw6Rn/GnvldlchS2JQr9wFYPiwZcDfrbbujEKqKN0tvENdbjXdYhDp5Q=="
},
"limiter": {
"version": "2.1.0",
"resolved": "https://registry.npmjs.org/limiter/-/limiter-2.1.0.tgz",
"integrity": "sha512-361TYz6iay6n+9KvUUImqdLuFigK+K79qrUtBsXhJTLdH4rIt/r1y8r1iozwh8KbZNpujbFTSh74mJ7bwbAMOw==",
"requires": {
"just-performance": "4.3.0"
}
},
"lodash": { "lodash": {
"version": "4.17.21", "version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
@ -4672,4 +4699,4 @@
"integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A==" "integrity": "sha512-3wdGidZyq5PB084XLES5TpOSRA3wjXAlIWMhum2kRcv/41Sn2emQ0dycQW4uZXLejwKvg6EsvbdlVL+FYEct7A=="
} }
} }
} }

View File

@ -34,6 +34,7 @@
"express": "^4.17.1", "express": "^4.17.1",
"graceful-fs": "^4.2.10", "graceful-fs": "^4.2.10",
"htmlparser2": "^8.0.1", "htmlparser2": "^8.0.1",
"limiter": "^2.1.0",
"node-tone": "^1.0.1", "node-tone": "^1.0.1",
"nodemailer": "^6.9.2", "nodemailer": "^6.9.2",
"sequelize": "^6.32.1", "sequelize": "^6.32.1",
@ -44,4 +45,4 @@
"devDependencies": { "devDependencies": {
"nodemon": "^2.0.20" "nodemon": "^2.0.20"
} }
} }

View File

@ -1,78 +1,123 @@
const axios = require('axios') const axios = require('axios')
const { levenshteinDistance } = require('../utils/index') const { levenshteinDistance } = require('../utils/index')
const Logger = require('../Logger') const Logger = require('../Logger')
const { RateLimiter } = require('limiter');
class Audnexus { class Audnexus {
static _instance = null;
constructor() { constructor() {
// ensures Audnexus class is singleton
if (Audnexus._instance) {
return Audnexus._instance
}
this.baseUrl = 'https://api.audnex.us' 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) { authorASINsRequest(name, region) {
name = encodeURIComponent(name) name = encodeURIComponent(name)
const regionQuery = region ? `&region=${region}` : '' const regionQuery = region ? `&region=${region}` : ''
const authorRequestUrl = `${this.baseUrl}/authors?name=${name}${regionQuery}` const authorRequestUrl = `${this.baseUrl}/authors?name=${name}${regionQuery}`
Logger.info(`[Audnexus] Searching for author "${authorRequestUrl}"`) Logger.info(`[Audnexus] Searching for author "${authorRequestUrl}"`)
return axios.get(authorRequestUrl).then((res) => {
return res.data || [] return this._processRequest(() => axios.get(authorRequestUrl))
}).catch((error) => { .then((res) => res.data || [])
Logger.error(`[Audnexus] Author ASIN request failed for ${name}`, error) .catch((error) => {
return [] Logger.error(`[Audnexus] Author ASIN request failed for ${name}`, error)
}) return []
})
} }
authorRequest(asin, region) { authorRequest(asin, region) {
asin = encodeURIComponent(asin) asin = encodeURIComponent(asin)
const regionQuery = region ? `?region=${region}` : '' const regionQuery = region ? `?region=${region}` : ''
const authorRequestUrl = `${this.baseUrl}/authors/${asin}${regionQuery}` const authorRequestUrl = `${this.baseUrl}/authors/${asin}${regionQuery}`
Logger.info(`[Audnexus] Searching for author "${authorRequestUrl}"`) Logger.info(`[Audnexus] Searching for author "${authorRequestUrl}"`)
return axios.get(authorRequestUrl).then((res) => {
return res.data return this._processRequest(() => axios.get(authorRequestUrl))
}).catch((error) => { .then((res) => res.data)
Logger.error(`[Audnexus] Author request failed for ${asin}`, error) .catch((error) => {
return null 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) { async findAuthorByASIN(asin, region) {
const author = await this.authorRequest(asin, region) const author = await this.authorRequest(asin, region)
if (!author) {
return null return author ?
} {
return { asin: author.asin,
asin: author.asin, description: author.description,
description: author.description, image: author.image || null,
image: author.image || null, name: author.name
name: author.name } : null
}
} }
async findAuthorByName(name, region, maxLevenshtein = 3) { async findAuthorByName(name, region, maxLevenshtein = 3) {
Logger.debug(`[Audnexus] Looking up author by name ${name}`) Logger.debug(`[Audnexus] Looking up author by name ${name}`)
const asins = await this.authorASINsRequest(name, region) const asins = await this.authorASINsRequest(name, region)
const matchingAsin = asins.find(obj => levenshteinDistance(obj.name, name) <= maxLevenshtein) const matchingAsin = asins.find(obj => levenshteinDistance(obj.name, name) <= maxLevenshtein)
if (!matchingAsin) { if (!matchingAsin) {
return null return null
} }
const author = await this.authorRequest(matchingAsin.asin) const author = await this.authorRequest(matchingAsin.asin)
if (!author) { return author ?
return null {
} description: author.description,
return { image: author.image || null,
asin: author.asin, asin: author.asin,
description: author.description, name: author.name
image: author.image || null, } : null
name: author.name
}
} }
getChaptersByASIN(asin, region) { getChaptersByASIN(asin, region) {
Logger.debug(`[Audnexus] Get chapters for ASIN ${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 return this._processRequest(() => axios.get(`${this.baseUrl}/books/${asin}/chapters?region=${region}`))
}).catch((error) => { .then((res) => res.data)
Logger.error(`[Audnexus] Chapter ASIN request failed for ${asin}/${region}`, error) .catch((error) => {
return null Logger.error(`[Audnexus] Chapter ASIN request failed for ${asin}/${region}`, error)
}) return null
})
} }
} }
module.exports = Audnexus module.exports = Audnexus