2021-11-21 19:59:32 +01:00
|
|
|
const axios = require('axios')
|
2021-11-21 22:28:57 +01:00
|
|
|
const { stripHtml } = require('string-strip-html')
|
2021-11-21 19:59:32 +01:00
|
|
|
const Logger = require('../Logger')
|
|
|
|
|
|
|
|
class Audible {
|
|
|
|
constructor() { }
|
|
|
|
|
|
|
|
cleanResult(item) {
|
2021-11-23 03:38:40 +01:00
|
|
|
var { title, subtitle, asin, authors, narrators, publisher_name, publisher_summary, release_date, series, product_images, publication_name } = item;
|
2021-11-21 19:59:32 +01:00
|
|
|
|
2021-11-23 03:38:40 +01:00
|
|
|
var primarySeries = this.getPrimarySeries(series, publication_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,
|
|
|
|
publisher: publisher_name,
|
2022-03-14 01:34:31 +01:00
|
|
|
publishedYear: release_date ? release_date.split('-')[0] : null,
|
2021-11-21 19:59:32 +01:00
|
|
|
description: stripHtml(publisher_summary).result,
|
|
|
|
cover: this.getBestImageLink(product_images),
|
|
|
|
asin,
|
2021-11-23 03:38:40 +01:00
|
|
|
series: primarySeries ? primarySeries.title : null,
|
|
|
|
volumeNumber: primarySeries ? primarySeries.sequence : null
|
2021-11-21 19:59:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getBestImageLink(images) {
|
2022-02-16 01:33:33 +01:00
|
|
|
if (!images) return null
|
|
|
|
var keys = Object.keys(images)
|
|
|
|
if (!keys.length) return null
|
|
|
|
return images[keys[keys.length - 1]]
|
2021-11-21 19:59:32 +01:00
|
|
|
}
|
|
|
|
|
2021-11-23 03:38:40 +01:00
|
|
|
getPrimarySeries(series, publication_name) {
|
|
|
|
return (series && series.length > 0) ? series.find((s) => s.title == publication_name) || series[0] : null
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-12-08 01:10:56 +01:00
|
|
|
asinSearch(asin) {
|
2021-12-30 10:57:28 +01:00
|
|
|
var queryObj = {
|
|
|
|
response_groups: 'rating,series,contributors,product_desc,media,product_extended_attrs',
|
|
|
|
image_sizes: '500,1024,2000'
|
|
|
|
};
|
|
|
|
var queryString = (new URLSearchParams(queryObj)).toString();
|
|
|
|
asin = encodeURIComponent(asin);
|
2021-12-07 19:27:30 +01:00
|
|
|
var url = `https://api.audible.com/1.0/catalog/products/${asin}?${queryString}`
|
|
|
|
Logger.debug(`[Audible] ASIN url: ${url}`)
|
2021-12-08 01:10:56 +01:00
|
|
|
return axios.get(url).then((res) => {
|
2021-12-07 19:27:30 +01:00
|
|
|
if (!res || !res.data || !res.data.product) return []
|
|
|
|
return [res.data.product]
|
|
|
|
}).catch(error => {
|
|
|
|
Logger.error('[Audible] search error', error)
|
|
|
|
return []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-11-21 19:59:32 +01:00
|
|
|
async search(title, author) {
|
2021-12-07 19:27:30 +01:00
|
|
|
if (this.isProbablyAsin(title)) {
|
|
|
|
var items = await this.asinSearch(title)
|
|
|
|
if (items.length > 0) return items.map(item => this.cleanResult(item))
|
2021-12-08 01:10:56 +01:00
|
|
|
}
|
2021-12-21 14:02:27 +01:00
|
|
|
|
|
|
|
var queryObj = {
|
|
|
|
response_groups: 'rating,series,contributors,product_desc,media,product_extended_attrs',
|
|
|
|
image_sizes: '500,1024,2000',
|
|
|
|
num_results: '25',
|
|
|
|
products_sort_by: 'Relevance',
|
|
|
|
title: title
|
|
|
|
};
|
|
|
|
if (author) queryObj.author = author
|
|
|
|
var queryString = (new URLSearchParams(queryObj)).toString();
|
2021-11-21 19:59:32 +01:00
|
|
|
var url = `https://api.audible.com/1.0/catalog/products?${queryString}`
|
|
|
|
Logger.debug(`[Audible] Search url: ${url}`)
|
|
|
|
var items = await axios.get(url).then((res) => {
|
|
|
|
if (!res || !res.data || !res.data.products) return []
|
|
|
|
return res.data.products
|
|
|
|
}).catch(error => {
|
|
|
|
Logger.error('[Audible] search error', error)
|
|
|
|
return []
|
|
|
|
})
|
|
|
|
return items.map(item => this.cleanResult(item))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = Audible
|