2024-05-25 23:32:02 +02:00
|
|
|
const axios = require('axios').default
|
2021-11-18 02:19:24 +01:00
|
|
|
const { levenshteinDistance } = require('../utils/index')
|
|
|
|
const Logger = require('../Logger')
|
|
|
|
|
2024-02-18 20:06:51 +01:00
|
|
|
/**
|
|
|
|
* @typedef AuthorSearchObj
|
|
|
|
* @property {string} asin
|
|
|
|
* @property {string} description
|
|
|
|
* @property {string} image
|
|
|
|
* @property {string} name
|
|
|
|
*/
|
|
|
|
|
2021-11-18 02:19:24 +01:00
|
|
|
class Audnexus {
|
|
|
|
constructor() {
|
|
|
|
this.baseUrl = 'https://api.audnex.us'
|
|
|
|
}
|
|
|
|
|
2024-02-18 20:06:51 +01:00
|
|
|
/**
|
2024-05-25 23:32:02 +02:00
|
|
|
*
|
|
|
|
* @param {string} name
|
|
|
|
* @param {string} region
|
|
|
|
* @returns {Promise<{asin:string, name:string}[]>}
|
2024-02-18 20:06:51 +01:00
|
|
|
*/
|
2023-04-16 22:53:46 +02:00
|
|
|
authorASINsRequest(name, region) {
|
2024-02-18 20:06:51 +01:00
|
|
|
const searchParams = new URLSearchParams()
|
|
|
|
searchParams.set('name', name)
|
|
|
|
if (region) searchParams.set('region', region)
|
|
|
|
const authorRequestUrl = `${this.baseUrl}/authors?${searchParams.toString()}`
|
2023-04-16 22:53:46 +02:00
|
|
|
Logger.info(`[Audnexus] Searching for author "${authorRequestUrl}"`)
|
2024-05-25 23:32:02 +02:00
|
|
|
return axios
|
|
|
|
.get(authorRequestUrl)
|
|
|
|
.then((res) => {
|
|
|
|
return res.data || []
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
Logger.error(`[Audnexus] Author ASIN request failed for ${name}`, error)
|
|
|
|
return []
|
|
|
|
})
|
2021-11-18 02:19:24 +01:00
|
|
|
}
|
|
|
|
|
2024-02-18 20:06:51 +01:00
|
|
|
/**
|
2024-05-25 23:32:02 +02:00
|
|
|
*
|
|
|
|
* @param {string} asin
|
|
|
|
* @param {string} region
|
2024-02-18 20:06:51 +01:00
|
|
|
* @returns {Promise<AuthorSearchObj>}
|
|
|
|
*/
|
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}`
|
|
|
|
Logger.info(`[Audnexus] Searching for author "${authorRequestUrl}"`)
|
2024-05-25 23:32:02 +02:00
|
|
|
return axios
|
|
|
|
.get(authorRequestUrl)
|
|
|
|
.then((res) => {
|
|
|
|
return res.data
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
Logger.error(`[Audnexus] Author request failed for ${asin}`, error)
|
|
|
|
return null
|
|
|
|
})
|
2021-11-18 02:19:24 +01:00
|
|
|
}
|
|
|
|
|
2024-02-18 20:06:51 +01:00
|
|
|
/**
|
2024-05-25 23:32:02 +02:00
|
|
|
*
|
|
|
|
* @param {string} asin
|
|
|
|
* @param {string} region
|
2024-02-18 20:06:51 +01:00
|
|
|
* @returns {Promise<AuthorSearchObj>}
|
|
|
|
*/
|
2023-04-16 22:53:46 +02:00
|
|
|
async findAuthorByASIN(asin, region) {
|
|
|
|
const author = await this.authorRequest(asin, region)
|
2022-05-14 01:11:54 +02:00
|
|
|
if (!author) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
asin: author.asin,
|
|
|
|
description: author.description,
|
2022-10-08 00:18:28 +02:00
|
|
|
image: author.image || null,
|
2022-05-14 01:11:54 +02:00
|
|
|
name: author.name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-18 20:06:51 +01:00
|
|
|
/**
|
2024-05-25 23:32:02 +02:00
|
|
|
*
|
|
|
|
* @param {string} name
|
|
|
|
* @param {string} region
|
|
|
|
* @param {number} maxLevenshtein
|
2024-02-18 20:06:51 +01:00
|
|
|
* @returns {Promise<AuthorSearchObj>}
|
|
|
|
*/
|
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}`)
|
2024-02-18 20:06:51 +01:00
|
|
|
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) {
|
2021-11-18 02:19:24 +01:00
|
|
|
return null
|
|
|
|
}
|
2024-02-18 20:06:51 +01:00
|
|
|
const author = await this.authorRequest(closestMatch.asin)
|
2021-11-18 02:19:24 +01:00
|
|
|
if (!author) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
asin: author.asin,
|
|
|
|
description: author.description,
|
2022-10-08 00:18:28 +02:00
|
|
|
image: author.image || null,
|
2021-11-18 02:19:24 +01:00
|
|
|
name: author.name
|
|
|
|
}
|
|
|
|
}
|
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}`)
|
2024-05-25 23:32:02 +02:00
|
|
|
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
|
|
|
|
})
|
2022-05-11 00:03:41 +02:00
|
|
|
}
|
2021-11-18 02:19:24 +01:00
|
|
|
}
|
2024-05-25 23:32:02 +02:00
|
|
|
module.exports = Audnexus
|