2024-05-25 23:32:02 +02:00
|
|
|
const axios = require('axios').default
|
2022-05-28 02:41:40 +02:00
|
|
|
const htmlSanitizer = require('../utils/htmlSanitizer')
|
2021-11-21 19:59:32 +01:00
|
|
|
const Logger = require('../Logger')
|
2024-06-09 20:43:03 +02:00
|
|
|
const { isValidASIN } = require('../utils/index')
|
2021-11-21 19:59:32 +01:00
|
|
|
|
|
|
|
class Audible {
|
2024-05-25 23:32:02 +02:00
|
|
|
#responseTimeout = 30000
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.regionMap = {
|
|
|
|
us: '.com',
|
|
|
|
ca: '.ca',
|
|
|
|
uk: '.co.uk',
|
|
|
|
au: '.com.au',
|
|
|
|
fr: '.fr',
|
|
|
|
de: '.de',
|
|
|
|
jp: '.co.jp',
|
|
|
|
it: '.it',
|
|
|
|
in: '.in',
|
|
|
|
es: '.es'
|
2022-10-08 00:18:28 +02:00
|
|
|
}
|
2024-05-25 23:32:02 +02:00
|
|
|
}
|
2021-11-21 19:59:32 +01:00
|
|
|
|
2024-05-25 23:32:02 +02:00
|
|
|
/**
|
|
|
|
* Audible will sometimes send sequences with "Book 1" or "2, Dramatized Adaptation"
|
|
|
|
* @see https://github.com/advplyr/audiobookshelf/issues/2380
|
|
|
|
* @see https://github.com/advplyr/audiobookshelf/issues/1339
|
|
|
|
*
|
|
|
|
* @param {string} seriesName
|
|
|
|
* @param {string} sequence
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
cleanSeriesSequence(seriesName, sequence) {
|
|
|
|
if (!sequence) return ''
|
|
|
|
// match any number with optional decimal (e.g, 1 or 1.5 or .5)
|
|
|
|
let numberFound = sequence.match(/\.\d+|\d+(?:\.\d+)?/)
|
|
|
|
let updatedSequence = numberFound ? numberFound[0] : sequence
|
|
|
|
if (sequence !== updatedSequence) {
|
|
|
|
Logger.debug(`[Audible] Series "${seriesName}" sequence was cleaned from "${sequence}" to "${updatedSequence}"`)
|
2023-12-07 00:31:36 +01:00
|
|
|
}
|
2024-05-25 23:32:02 +02:00
|
|
|
return updatedSequence
|
|
|
|
}
|
2023-12-07 00:31:36 +01:00
|
|
|
|
2024-05-25 23:32:02 +02:00
|
|
|
cleanResult(item) {
|
|
|
|
const { title, subtitle, asin, authors, narrators, publisherName, summary, releaseDate, image, genres, seriesPrimary, seriesSecondary, language, runtimeLengthMin, formatType } = item
|
2021-11-21 19:59:32 +01:00
|
|
|
|
2024-05-25 23:32:02 +02:00
|
|
|
const series = []
|
|
|
|
if (seriesPrimary) {
|
|
|
|
series.push({
|
|
|
|
series: seriesPrimary.name,
|
|
|
|
sequence: this.cleanSeriesSequence(seriesPrimary.name, seriesPrimary.position || '')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if (seriesSecondary) {
|
|
|
|
series.push({
|
|
|
|
series: seriesSecondary.name,
|
|
|
|
sequence: this.cleanSeriesSequence(seriesSecondary.name, seriesSecondary.position || '')
|
|
|
|
})
|
|
|
|
}
|
2022-05-23 04:56:51 +02:00
|
|
|
|
2024-05-25 23:32:02 +02:00
|
|
|
const genresFiltered = genres ? genres.filter((g) => g.type == 'genre').map((g) => g.name) : []
|
|
|
|
const tagsFiltered = genres ? genres.filter((g) => g.type == 'tag').map((g) => g.name) : []
|
2021-11-21 19:59:32 +01:00
|
|
|
|
2024-05-25 23:32:02 +02:00
|
|
|
return {
|
|
|
|
title,
|
|
|
|
subtitle: subtitle || null,
|
|
|
|
author: authors ? authors.map(({ name }) => name).join(', ') : null,
|
|
|
|
narrator: narrators ? narrators.map(({ name }) => name).join(', ') : null,
|
|
|
|
publisher: publisherName,
|
|
|
|
publishedYear: releaseDate ? releaseDate.split('-')[0] : null,
|
|
|
|
description: summary ? htmlSanitizer.stripAllTags(summary) : null,
|
|
|
|
cover: image,
|
|
|
|
asin,
|
|
|
|
genres: genresFiltered.length ? genresFiltered : null,
|
|
|
|
tags: tagsFiltered.length ? tagsFiltered.join(', ') : null,
|
|
|
|
series: series.length ? series : null,
|
|
|
|
language: language ? language.charAt(0).toUpperCase() + language.slice(1) : null,
|
|
|
|
duration: runtimeLengthMin && !isNaN(runtimeLengthMin) ? Number(runtimeLengthMin) : 0,
|
|
|
|
region: item.region || null,
|
|
|
|
rating: item.rating || null,
|
|
|
|
abridged: formatType === 'abridged'
|
2021-11-21 19:59:32 +01:00
|
|
|
}
|
2024-05-25 23:32:02 +02:00
|
|
|
}
|
2021-11-21 19:59:32 +01:00
|
|
|
|
2024-05-25 23:32:02 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} asin
|
|
|
|
* @param {string} region
|
|
|
|
* @param {number} [timeout] response timeout in ms
|
|
|
|
* @returns {Promise<Object[]>}
|
|
|
|
*/
|
|
|
|
asinSearch(asin, region, timeout = this.#responseTimeout) {
|
|
|
|
if (!asin) return []
|
|
|
|
if (!timeout || isNaN(timeout)) timeout = this.#responseTimeout
|
2021-12-07 19:27:30 +01:00
|
|
|
|
2024-05-25 23:32:02 +02:00
|
|
|
asin = encodeURIComponent(asin.toUpperCase())
|
|
|
|
var regionQuery = region ? `?region=${region}` : ''
|
|
|
|
var url = `https://api.audnex.us/books/${asin}${regionQuery}`
|
|
|
|
Logger.debug(`[Audible] ASIN url: ${url}`)
|
|
|
|
return axios
|
|
|
|
.get(url, {
|
|
|
|
timeout
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
if (!res || !res.data || !res.data.asin) return null
|
|
|
|
return res.data
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
Logger.error('[Audible] ASIN search error', error)
|
|
|
|
return []
|
|
|
|
})
|
|
|
|
}
|
2022-10-08 00:18:28 +02:00
|
|
|
|
2024-05-25 23:32:02 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {string} title
|
|
|
|
* @param {string} author
|
|
|
|
* @param {string} asin
|
|
|
|
* @param {string} region
|
|
|
|
* @param {number} [timeout] response timeout in ms
|
|
|
|
* @returns {Promise<Object[]>}
|
|
|
|
*/
|
|
|
|
async search(title, author, asin, region, timeout = this.#responseTimeout) {
|
|
|
|
if (region && !this.regionMap[region]) {
|
|
|
|
Logger.error(`[Audible] search: Invalid region ${region}`)
|
|
|
|
region = ''
|
|
|
|
}
|
|
|
|
if (!timeout || isNaN(timeout)) timeout = this.#responseTimeout
|
2021-12-21 14:02:27 +01:00
|
|
|
|
2024-05-25 23:32:02 +02:00
|
|
|
let items
|
2024-06-09 20:43:03 +02:00
|
|
|
if (asin && isValidASIN(asin.toUpperCase())) {
|
2024-05-25 23:32:02 +02:00
|
|
|
items = [await this.asinSearch(asin, region, timeout)]
|
|
|
|
}
|
|
|
|
|
2024-06-09 20:43:03 +02:00
|
|
|
if (!items && isValidASIN(title.toUpperCase())) {
|
2024-05-25 23:32:02 +02:00
|
|
|
items = [await this.asinSearch(title, region, timeout)]
|
|
|
|
}
|
2021-12-21 14:02:27 +01:00
|
|
|
|
2024-05-25 23:32:02 +02:00
|
|
|
if (!items) {
|
|
|
|
const queryObj = {
|
|
|
|
num_results: '10',
|
|
|
|
products_sort_by: 'Relevance',
|
|
|
|
title: title
|
|
|
|
}
|
|
|
|
if (author) queryObj.author = author
|
|
|
|
const queryString = new URLSearchParams(queryObj).toString()
|
|
|
|
const tld = region ? this.regionMap[region] : '.com'
|
|
|
|
const url = `https://api.audible${tld}/1.0/catalog/products?${queryString}`
|
|
|
|
Logger.debug(`[Audible] Search url: ${url}`)
|
|
|
|
items = await axios
|
|
|
|
.get(url, {
|
|
|
|
timeout
|
|
|
|
})
|
|
|
|
.then((res) => {
|
|
|
|
if (!res?.data?.products) return null
|
|
|
|
return Promise.all(res.data.products.map((result) => this.asinSearch(result.asin, region, timeout)))
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
Logger.error('[Audible] query search error', error)
|
|
|
|
return []
|
|
|
|
})
|
2021-11-21 19:59:32 +01:00
|
|
|
}
|
2024-05-25 23:32:02 +02:00
|
|
|
return items?.map((item) => this.cleanResult(item)) || []
|
|
|
|
}
|
2021-11-21 19:59:32 +01:00
|
|
|
}
|
|
|
|
|
2024-05-25 23:32:02 +02:00
|
|
|
module.exports = Audible
|