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'
|
|
|
|
}
|
|
|
|
|
|
|
|
authorASINsRequest(name) {
|
2021-12-30 10:57:28 +01:00
|
|
|
name = encodeURIComponent(name);
|
2021-11-18 02:19:24 +01:00
|
|
|
return axios.get(`${this.baseUrl}/authors?name=${name}`).then((res) => {
|
|
|
|
return res.data || []
|
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[Audnexus] Author ASIN request failed for ${name}`, error)
|
|
|
|
return []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
authorRequest(asin) {
|
2021-12-30 10:57:28 +01:00
|
|
|
asin = encodeURIComponent(asin);
|
2021-11-18 02:19:24 +01:00
|
|
|
return axios.get(`${this.baseUrl}/authors/${asin}`).then((res) => {
|
|
|
|
return res.data
|
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[Audnexus] Author request failed for ${asin}`, error)
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-05-08 03:01:29 +02:00
|
|
|
async findAuthorByName(name, maxLevenshtein = 3) {
|
2021-11-18 02:19:24 +01:00
|
|
|
Logger.debug(`[Audnexus] Looking up author by name ${name}`)
|
|
|
|
var asins = await this.authorASINsRequest(name)
|
|
|
|
var matchingAsin = asins.find(obj => levenshteinDistance(obj.name, name) <= maxLevenshtein)
|
|
|
|
if (!matchingAsin) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
var author = await this.authorRequest(matchingAsin.asin)
|
|
|
|
if (!author) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
asin: author.asin,
|
|
|
|
description: author.description,
|
|
|
|
image: author.image,
|
|
|
|
name: author.name
|
|
|
|
}
|
|
|
|
}
|
2022-05-11 00:03:41 +02:00
|
|
|
|
|
|
|
async getChaptersByASIN(asin) {
|
|
|
|
Logger.debug(`[Audnexus] Get chapters for ASIN ${asin}`)
|
|
|
|
return axios.get(`${this.baseUrl}/books/${asin}/chapters`).then((res) => {
|
|
|
|
return res.data
|
|
|
|
}).catch((error) => {
|
|
|
|
Logger.error(`[Audnexus] Chapter ASIN request failed for ${asin}`, error)
|
|
|
|
return null
|
|
|
|
})
|
|
|
|
}
|
2021-11-18 02:19:24 +01:00
|
|
|
}
|
|
|
|
module.exports = Audnexus
|