2021-11-18 02:19:24 +01:00
|
|
|
const axios = require('axios')
|
|
|
|
const { levenshteinDistance } = require('../utils/index')
|
|
|
|
const Logger = require('../Logger')
|
|
|
|
|
|
|
|
class Audnexus {
|
|
|
|
constructor() {
|
|
|
|
this.baseUrl = 'https://api.audnex.us'
|
|
|
|
}
|
|
|
|
|
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}`
|
|
|
|
Logger.info(`[Audnexus] Searching for author "${authorRequestUrl}"`)
|
|
|
|
return axios.get(authorRequestUrl).then((res) => {
|
2021-11-18 02:19:24 +01:00
|
|
|
return res.data || []
|
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[Audnexus] Author ASIN request failed for ${name}`, error)
|
|
|
|
return []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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}"`)
|
|
|
|
return axios.get(authorRequestUrl).then((res) => {
|
2021-11-18 02:19:24 +01:00
|
|
|
return res.data
|
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[Audnexus] Author request failed for ${asin}`, error)
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-16 22:53:46 +02:00
|
|
|
const asins = await this.authorASINsRequest(name, region)
|
|
|
|
const matchingAsin = asins.find(obj => levenshteinDistance(obj.name, name) <= maxLevenshtein)
|
2021-11-18 02:19:24 +01:00
|
|
|
if (!matchingAsin) {
|
|
|
|
return null
|
|
|
|
}
|
2023-04-16 22:53:46 +02:00
|
|
|
const author = await this.authorRequest(matchingAsin.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}`)
|
|
|
|
return axios.get(`${this.baseUrl}/books/${asin}/chapters?region=${region}`).then((res) => {
|
2022-05-11 00:03:41 +02:00
|
|
|
return res.data
|
|
|
|
}).catch((error) => {
|
2022-10-15 22:31:07 +02:00
|
|
|
Logger.error(`[Audnexus] Chapter ASIN request failed for ${asin}/${region}`, error)
|
2022-05-11 00:03:41 +02:00
|
|
|
return null
|
|
|
|
})
|
|
|
|
}
|
2021-11-18 02:19:24 +01:00
|
|
|
}
|
|
|
|
module.exports = Audnexus
|