2021-11-21 19:59:32 +01:00
|
|
|
const axios = require('axios')
|
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')
|
|
|
|
|
|
|
|
class Audible {
|
2022-10-08 00:18:28 +02:00
|
|
|
constructor() {
|
|
|
|
this.regionMap = {
|
|
|
|
'us': '.com',
|
|
|
|
'ca': '.ca',
|
|
|
|
'uk': '.co.uk',
|
2022-11-21 00:17:25 +01:00
|
|
|
'au': '.com.au',
|
2022-10-08 00:18:28 +02:00
|
|
|
'fr': '.fr',
|
|
|
|
'de': '.de',
|
|
|
|
'jp': '.co.jp',
|
|
|
|
'it': '.it',
|
2023-05-14 20:42:29 +02:00
|
|
|
'in': '.in',
|
2022-10-08 00:18:28 +02:00
|
|
|
'es': '.es'
|
|
|
|
}
|
|
|
|
}
|
2021-11-21 19:59:32 +01:00
|
|
|
|
2023-12-07 00:31:36 +01: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 ''
|
|
|
|
let updatedSequence = sequence.replace(/Book /, '').trim()
|
|
|
|
if (updatedSequence.includes(' ')) {
|
|
|
|
updatedSequence = updatedSequence.split(' ').shift().replace(/,$/, '')
|
|
|
|
}
|
|
|
|
if (sequence !== updatedSequence) {
|
|
|
|
Logger.debug(`[Audible] Series "${seriesName}" sequence was cleaned from "${sequence}" to "${updatedSequence}"`)
|
|
|
|
}
|
|
|
|
return updatedSequence
|
|
|
|
}
|
|
|
|
|
2021-11-21 19:59:32 +01:00
|
|
|
cleanResult(item) {
|
2023-03-23 00:05:43 +01:00
|
|
|
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
|
|
|
|
2023-01-07 23:33:20 +01:00
|
|
|
const series = []
|
|
|
|
if (seriesPrimary) {
|
|
|
|
series.push({
|
|
|
|
series: seriesPrimary.name,
|
2023-12-07 00:31:36 +01:00
|
|
|
sequence: this.cleanSeriesSequence(seriesPrimary.name, seriesPrimary.position || '')
|
2023-01-07 23:33:20 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
if (seriesSecondary) {
|
|
|
|
series.push({
|
|
|
|
series: seriesSecondary.name,
|
2023-12-07 00:31:36 +01:00
|
|
|
sequence: this.cleanSeriesSequence(seriesSecondary.name, seriesSecondary.position || '')
|
2023-01-07 23:33:20 +01:00
|
|
|
})
|
|
|
|
}
|
2022-05-23 04:56:51 +02:00
|
|
|
|
2022-10-01 23:51:22 +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
|
|
|
|
|
|
|
return {
|
|
|
|
title,
|
|
|
|
subtitle: subtitle || null,
|
|
|
|
author: authors ? authors.map(({ name }) => name).join(', ') : null,
|
|
|
|
narrator: narrators ? narrators.map(({ name }) => name).join(', ') : null,
|
2022-05-23 04:56:51 +02:00
|
|
|
publisher: publisherName,
|
|
|
|
publishedYear: releaseDate ? releaseDate.split('-')[0] : null,
|
2022-05-28 20:58:52 +02:00
|
|
|
description: summary ? htmlSanitizer.stripAllTags(summary) : null,
|
2022-05-23 04:56:51 +02:00
|
|
|
cover: image,
|
2021-11-21 19:59:32 +01:00
|
|
|
asin,
|
2022-10-01 23:51:22 +02:00
|
|
|
genres: genresFiltered.length ? genresFiltered : null,
|
|
|
|
tags: tagsFiltered.length ? tagsFiltered.join(', ') : null,
|
2023-01-07 23:33:20 +01:00
|
|
|
series: series.length ? series : null,
|
2022-08-04 01:06:25 +02:00
|
|
|
language: language ? language.charAt(0).toUpperCase() + language.slice(1) : null,
|
2022-10-08 00:18:28 +02:00
|
|
|
duration: runtimeLengthMin && !isNaN(runtimeLengthMin) ? Number(runtimeLengthMin) : 0,
|
|
|
|
region: item.region || null,
|
2023-03-23 00:05:43 +01:00
|
|
|
rating: item.rating || null,
|
|
|
|
abridged: formatType === 'abridged'
|
2021-11-21 19:59:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 19:27:30 +01:00
|
|
|
isProbablyAsin(title) {
|
2022-04-16 19:40:10 +02:00
|
|
|
return /^[0-9A-Z]{10}$/.test(title)
|
2021-12-07 19:27:30 +01:00
|
|
|
}
|
|
|
|
|
2022-10-08 00:18:28 +02:00
|
|
|
asinSearch(asin, region) {
|
2023-12-07 00:31:36 +01:00
|
|
|
asin = encodeURIComponent(asin)
|
2022-10-08 00:18:28 +02:00
|
|
|
var regionQuery = region ? `?region=${region}` : ''
|
|
|
|
var url = `https://api.audnex.us/books/${asin}${regionQuery}`
|
2021-12-07 19:27:30 +01:00
|
|
|
Logger.debug(`[Audible] ASIN url: ${url}`)
|
2021-12-08 01:10:56 +01:00
|
|
|
return axios.get(url).then((res) => {
|
2022-05-23 04:56:51 +02:00
|
|
|
if (!res || !res.data || !res.data.asin) return null
|
|
|
|
return res.data
|
2021-12-07 19:27:30 +01:00
|
|
|
}).catch(error => {
|
2022-05-23 04:56:51 +02:00
|
|
|
Logger.error('[Audible] ASIN search error', error)
|
2021-12-07 19:27:30 +01:00
|
|
|
return []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-10-08 00:18:28 +02:00
|
|
|
async search(title, author, asin, region) {
|
|
|
|
if (region && !this.regionMap[region]) {
|
|
|
|
Logger.error(`[Audible] search: Invalid region ${region}`)
|
|
|
|
region = ''
|
|
|
|
}
|
|
|
|
|
2023-01-07 23:33:20 +01:00
|
|
|
let items
|
2022-05-28 20:58:52 +02:00
|
|
|
if (asin) {
|
2022-10-08 00:18:28 +02:00
|
|
|
items = [await this.asinSearch(asin, region)]
|
2022-05-23 04:56:51 +02:00
|
|
|
}
|
2021-12-21 14:02:27 +01:00
|
|
|
|
2022-05-23 04:56:51 +02:00
|
|
|
if (!items && this.isProbablyAsin(title)) {
|
2022-10-08 00:18:28 +02:00
|
|
|
items = [await this.asinSearch(title, region)]
|
2021-12-08 01:10:56 +01:00
|
|
|
}
|
2021-12-21 14:02:27 +01:00
|
|
|
|
2022-05-28 20:58:52 +02:00
|
|
|
if (!items) {
|
2023-01-07 23:33:20 +01:00
|
|
|
const queryObj = {
|
2022-05-23 23:48:11 +02:00
|
|
|
num_results: '10',
|
2022-05-23 04:56:51 +02:00
|
|
|
products_sort_by: 'Relevance',
|
|
|
|
title: title
|
2022-10-08 00:18:28 +02:00
|
|
|
}
|
2022-05-23 04:56:51 +02:00
|
|
|
if (author) queryObj.author = author
|
2022-10-08 00:18:28 +02:00
|
|
|
const queryString = (new URLSearchParams(queryObj)).toString()
|
|
|
|
const tld = region ? this.regionMap[region] : '.com'
|
|
|
|
const url = `https://api.audible${tld}/1.0/catalog/products?${queryString}`
|
2022-05-23 04:56:51 +02:00
|
|
|
Logger.debug(`[Audible] Search url: ${url}`)
|
|
|
|
items = await axios.get(url).then((res) => {
|
|
|
|
if (!res || !res.data || !res.data.products) return null
|
2022-10-08 00:18:28 +02:00
|
|
|
return Promise.all(res.data.products.map(result => this.asinSearch(result.asin, region)))
|
2022-05-23 04:56:51 +02:00
|
|
|
}).catch(error => {
|
|
|
|
Logger.error('[Audible] query search error', error)
|
|
|
|
return []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return items ? items.map(item => this.cleanResult(item)) : []
|
2021-11-21 19:59:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Audible
|