-
Notifications
You must be signed in to change notification settings - Fork 512
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix:Add timeout to provider matching default to 30s #3000
- Loading branch information
Showing
9 changed files
with
630 additions
and
441 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,145 +1,176 @@ | ||
const axios = require('axios') | ||
const axios = require('axios').default | ||
const htmlSanitizer = require('../utils/htmlSanitizer') | ||
const Logger = require('../Logger') | ||
|
||
class Audible { | ||
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' | ||
} | ||
#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' | ||
} | ||
} | ||
|
||
/** | ||
* 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}"`) | ||
} | ||
return updatedSequence | ||
/** | ||
* 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}"`) | ||
} | ||
return updatedSequence | ||
} | ||
|
||
cleanResult(item) { | ||
const { title, subtitle, asin, authors, narrators, publisherName, summary, releaseDate, image, genres, seriesPrimary, seriesSecondary, language, runtimeLengthMin, formatType } = item | ||
cleanResult(item) { | ||
const { title, subtitle, asin, authors, narrators, publisherName, summary, releaseDate, image, genres, seriesPrimary, seriesSecondary, language, runtimeLengthMin, formatType } = item | ||
|
||
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 || '') | ||
}) | ||
} | ||
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 || '') | ||
}) | ||
} | ||
|
||
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) : [] | ||
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) : [] | ||
|
||
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' | ||
} | ||
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' | ||
} | ||
} | ||
|
||
/** | ||
* Test if a search title matches an ASIN. Supports lowercase letters | ||
* | ||
* @param {string} title | ||
* @returns {boolean} | ||
*/ | ||
isProbablyAsin(title) { | ||
return /^[0-9A-Za-z]{10}$/.test(title) | ||
} | ||
/** | ||
* Test if a search title matches an ASIN. Supports lowercase letters | ||
* | ||
* @param {string} title | ||
* @returns {boolean} | ||
*/ | ||
isProbablyAsin(title) { | ||
return /^[0-9A-Za-z]{10}$/.test(title) | ||
} | ||
|
||
asinSearch(asin, region) { | ||
if (!asin) return [] | ||
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).then((res) => { | ||
if (!res || !res.data || !res.data.asin) return null | ||
return res.data | ||
}).catch(error => { | ||
Logger.error('[Audible] ASIN search error', error) | ||
return [] | ||
}) | ||
} | ||
/** | ||
* | ||
* @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 | ||
|
||
async search(title, author, asin, region) { | ||
if (region && !this.regionMap[region]) { | ||
Logger.error(`[Audible] search: Invalid region ${region}`) | ||
region = '' | ||
} | ||
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 [] | ||
}) | ||
} | ||
|
||
let items | ||
if (asin) { | ||
items = [await this.asinSearch(asin, region)] | ||
} | ||
/** | ||
* | ||
* @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 | ||
|
||
if (!items && this.isProbablyAsin(title)) { | ||
items = [await this.asinSearch(title, region)] | ||
} | ||
let items | ||
if (asin) { | ||
items = [await this.asinSearch(asin, region, timeout)] | ||
} | ||
|
||
if (!items && this.isProbablyAsin(title)) { | ||
items = [await this.asinSearch(title, region, timeout)] | ||
} | ||
|
||
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).then((res) => { | ||
if (!res?.data?.products) return null | ||
return Promise.all(res.data.products.map(result => this.asinSearch(result.asin, region))) | ||
}).catch(error => { | ||
Logger.error('[Audible] query search error', error) | ||
return [] | ||
}) | ||
} | ||
return items ? items.map(item => this.cleanResult(item)) : [] | ||
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 [] | ||
}) | ||
} | ||
return items?.map((item) => this.cleanResult(item)) || [] | ||
} | ||
} | ||
|
||
module.exports = Audible | ||
module.exports = Audible |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.