audiobookshelf/server/providers/Audible.js

85 lines
3.3 KiB
JavaScript
Raw Normal View History

2021-11-21 19:59:32 +01:00
const axios = require('axios')
const htmlSanitizer = require('../utils/htmlSanitizer')
2021-11-21 19:59:32 +01:00
const Logger = require('../Logger')
class Audible {
constructor() { }
cleanResult(item) {
var { title, subtitle, asin, authors, narrators, publisherName, summary, releaseDate, image, genres, seriesPrimary, seriesSecondary, language, runtimeLengthMin } = item
2021-11-21 19:59:32 +01:00
var series = []
2022-05-28 20:58:52 +02:00
if (seriesPrimary) series.push(seriesPrimary)
if (seriesSecondary) series.push(seriesSecondary)
var genresFiltered = genres ? genres.filter(g => g.type == "genre") : []
var tagsFiltered = genres ? genres.filter(g => g.type == "tag") : []
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,
publisher: publisherName,
publishedYear: releaseDate ? releaseDate.split('-')[0] : null,
2022-05-28 20:58:52 +02:00
description: summary ? htmlSanitizer.stripAllTags(summary) : null,
cover: image,
2021-11-21 19:59:32 +01:00
asin,
genres: genresFiltered.length > 0 ? genresFiltered.map(({ name }) => name).join(', ') : null,
tags: tagsFiltered.length > 0 ? tagsFiltered.map(({ name }) => name).join(', ') : null,
2022-05-28 20:58:52 +02:00
series: series != [] ? series.map(({ name, position }) => ({ series: name, volumeNumber: position })) : null,
language: language ? language.charAt(0).toUpperCase() + language.slice(1) : null,
duration: runtimeLengthMin && !isNaN(runtimeLengthMin) ? Number(runtimeLengthMin) : 0
2021-11-21 19:59:32 +01:00
}
}
2021-12-07 19:27:30 +01:00
isProbablyAsin(title) {
return /^[0-9A-Z]{10}$/.test(title)
2021-12-07 19:27:30 +01:00
}
asinSearch(asin) {
asin = encodeURIComponent(asin);
var url = `https://api.audnex.us/books/${asin}`
2021-12-07 19:27:30 +01:00
Logger.debug(`[Audible] ASIN url: ${url}`)
return axios.get(url).then((res) => {
if (!res || !res.data || !res.data.asin) return null
return res.data
2021-12-07 19:27:30 +01:00
}).catch(error => {
Logger.error('[Audible] ASIN search error', error)
2021-12-07 19:27:30 +01:00
return []
})
}
2022-05-28 20:58:52 +02:00
async search(title, author, asin) {
var items
2022-05-28 20:58:52 +02:00
if (asin) {
items = [await this.asinSearch(asin)]
}
if (!items && this.isProbablyAsin(title)) {
items = [await this.asinSearch(title)]
}
2022-05-28 20:58:52 +02:00
if (!items) {
var queryObj = {
num_results: '10',
products_sort_by: 'Relevance',
title: title
};
if (author) queryObj.author = author
var queryString = (new URLSearchParams(queryObj)).toString();
var url = `https://api.audible.com/1.0/catalog/products?${queryString}`
Logger.debug(`[Audible] Search url: ${url}`)
items = await axios.get(url).then((res) => {
if (!res || !res.data || !res.data.products) return null
return Promise.all(res.data.products.map(result => this.asinSearch(result.asin)))
}).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