2021-11-18 02:19:24 +01:00
|
|
|
const axios = require('axios')
|
|
|
|
const { levenshteinDistance } = require('../utils/index')
|
|
|
|
const Logger = require('../Logger')
|
2024-01-17 03:31:29 +01:00
|
|
|
const { RateLimiter } = require('limiter')
|
2021-11-18 02:19:24 +01:00
|
|
|
|
|
|
|
class Audnexus {
|
2024-01-17 03:31:29 +01:00
|
|
|
static _instance = null
|
2023-10-05 22:48:55 +02:00
|
|
|
|
2021-11-18 02:19:24 +01:00
|
|
|
constructor() {
|
2023-10-05 22:48:55 +02:00
|
|
|
// ensures Audnexus class is singleton
|
|
|
|
if (Audnexus._instance) {
|
|
|
|
return Audnexus._instance
|
|
|
|
}
|
|
|
|
|
2021-11-18 02:19:24 +01:00
|
|
|
this.baseUrl = 'https://api.audnex.us'
|
2023-10-05 22:48:55 +02:00
|
|
|
|
|
|
|
// @see https://github.com/laxamentumtech/audnexus#-deployment-
|
|
|
|
this.limiter = new RateLimiter({
|
|
|
|
tokensPerInterval: 100,
|
|
|
|
fireImmediately: true,
|
|
|
|
interval: 'minute',
|
|
|
|
})
|
|
|
|
|
|
|
|
Audnexus._instance = this
|
2021-11-18 02:19:24 +01:00
|
|
|
}
|
|
|
|
|
2023-04-16 22:53:46 +02:00
|
|
|
authorASINsRequest(name, region) {
|
|
|
|
name = encodeURIComponent(name)
|
|
|
|
const regionQuery = region ? `®ion=${region}` : ''
|
|
|
|
const authorRequestUrl = `${this.baseUrl}/authors?name=${name}${regionQuery}`
|
2023-10-05 22:48:55 +02:00
|
|
|
|
2023-04-16 22:53:46 +02:00
|
|
|
Logger.info(`[Audnexus] Searching for author "${authorRequestUrl}"`)
|
2023-10-05 22:48:55 +02:00
|
|
|
|
|
|
|
return this._processRequest(() => axios.get(authorRequestUrl))
|
|
|
|
.then((res) => res.data || [])
|
|
|
|
.catch((error) => {
|
|
|
|
Logger.error(`[Audnexus] Author ASIN request failed for ${name}`, error)
|
|
|
|
return []
|
|
|
|
})
|
2021-11-18 02:19:24 +01:00
|
|
|
}
|
|
|
|
|
2023-04-16 22:53:46 +02:00
|
|
|
authorRequest(asin, region) {
|
|
|
|
asin = encodeURIComponent(asin)
|
|
|
|
const regionQuery = region ? `?region=${region}` : ''
|
|
|
|
const authorRequestUrl = `${this.baseUrl}/authors/${asin}${regionQuery}`
|
2023-10-05 22:48:55 +02:00
|
|
|
|
2023-04-16 22:53:46 +02:00
|
|
|
Logger.info(`[Audnexus] Searching for author "${authorRequestUrl}"`)
|
2023-10-05 22:48:55 +02:00
|
|
|
|
|
|
|
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)
|
2021-11-18 02:19:24 +01:00
|
|
|
}
|
|
|
|
|
2023-04-16 22:53:46 +02:00
|
|
|
async findAuthorByASIN(asin, region) {
|
|
|
|
const author = await this.authorRequest(asin, region)
|
2023-10-05 22:48:55 +02:00
|
|
|
|
|
|
|
return author ?
|
|
|
|
{
|
|
|
|
asin: author.asin,
|
|
|
|
description: author.description,
|
|
|
|
image: author.image || null,
|
|
|
|
name: author.name
|
|
|
|
} : null
|
2022-05-14 01:11:54 +02:00
|
|
|
}
|
|
|
|
|
2023-04-16 22:53:46 +02:00
|
|
|
async findAuthorByName(name, region, maxLevenshtein = 3) {
|
2021-11-18 02:19:24 +01:00
|
|
|
Logger.debug(`[Audnexus] Looking up author by name ${name}`)
|
2023-10-05 22:48:55 +02:00
|
|
|
|
2023-04-16 22:53:46 +02:00
|
|
|
const asins = await this.authorASINsRequest(name, region)
|
|
|
|
const matchingAsin = asins.find(obj => levenshteinDistance(obj.name, name) <= maxLevenshtein)
|
2023-10-05 22:48:55 +02:00
|
|
|
|
2021-11-18 02:19:24 +01:00
|
|
|
if (!matchingAsin) {
|
|
|
|
return null
|
|
|
|
}
|
2023-10-05 22:48:55 +02:00
|
|
|
|
2023-04-16 22:53:46 +02:00
|
|
|
const author = await this.authorRequest(matchingAsin.asin)
|
2023-10-05 22:48:55 +02:00
|
|
|
return author ?
|
|
|
|
{
|
|
|
|
description: author.description,
|
|
|
|
image: author.image || null,
|
|
|
|
asin: author.asin,
|
|
|
|
name: author.name
|
|
|
|
} : null
|
2021-11-18 02:19:24 +01:00
|
|
|
}
|
2022-05-11 00:03:41 +02:00
|
|
|
|
2022-10-15 22:31:07 +02:00
|
|
|
getChaptersByASIN(asin, region) {
|
|
|
|
Logger.debug(`[Audnexus] Get chapters for ASIN ${asin}/${region}`)
|
2023-10-05 22:48:55 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
})
|
2022-05-11 00:03:41 +02:00
|
|
|
}
|
2021-11-18 02:19:24 +01:00
|
|
|
}
|
2023-10-05 22:48:55 +02:00
|
|
|
|
2021-11-18 02:19:24 +01:00
|
|
|
module.exports = Audnexus
|