Skip to content

Commit

Permalink
Fix:Audible book match not falling back to search after failed ASIN #…
Browse files Browse the repository at this point in the history
  • Loading branch information
advplyr committed Aug 20, 2024
1 parent 7a7a779 commit 149bb3e
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions server/providers/Audible.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ class Audible {
* @param {string} asin
* @param {string} region
* @param {number} [timeout] response timeout in ms
* @returns {Promise<Object[]>}
* @returns {Promise<Object>}
*/
asinSearch(asin, region, timeout = this.#responseTimeout) {
if (!asin) return []
if (!asin) return null
if (!timeout || isNaN(timeout)) timeout = this.#responseTimeout

asin = encodeURIComponent(asin.toUpperCase())
Expand All @@ -102,12 +102,12 @@ class Audible {
timeout
})
.then((res) => {
if (!res || !res.data || !res.data.asin) return null
if (!res?.data?.asin) return null
return res.data
})
.catch((error) => {
Logger.error('[Audible] ASIN search error', error)
return []
return null
})
}

Expand All @@ -127,16 +127,18 @@ class Audible {
}
if (!timeout || isNaN(timeout)) timeout = this.#responseTimeout

let items
let items = []
if (asin && isValidASIN(asin.toUpperCase())) {
items = [await this.asinSearch(asin, region, timeout)]
const item = await this.asinSearch(asin, region, timeout)
if (item) items.push(item)
}

if (!items && isValidASIN(title.toUpperCase())) {
items = [await this.asinSearch(title, region, timeout)]
if (!items.length && isValidASIN(title.toUpperCase())) {
const item = await this.asinSearch(title, region, timeout)
if (item) items.push(item)
}

if (!items) {
if (!items.length) {
const queryObj = {
num_results: '10',
products_sort_by: 'Relevance',
Expand All @@ -160,7 +162,7 @@ class Audible {
return []
})
}
return items?.map((item) => this.cleanResult(item)) || []
return items.filter(Boolean).map((item) => this.cleanResult(item)) || []
}
}

Expand Down

0 comments on commit 149bb3e

Please sign in to comment.