Fix:Add timeout to provider matching default to 30s #3000

This commit is contained in:
advplyr 2024-05-25 16:32:02 -05:00
parent 30d3e41542
commit 6fa49e0aab
9 changed files with 633 additions and 444 deletions

View file

@ -28,19 +28,24 @@ const htmlSanitizer = require('../utils/htmlSanitizer')
*/
class iTunes {
constructor() { }
#responseTimeout = 30000
constructor() {}
/**
* @see https://developer.apple.com/library/archive/documentation/AudioVideo/Conceptual/iTuneSearchAPI/Searching.html
*
* @param {iTunesSearchParams} options
*
* @param {iTunesSearchParams} options
* @param {number} [timeout] response timeout in ms
* @returns {Promise<Object[]>}
*/
search(options) {
search(options, timeout = this.#responseTimeout) {
if (!options.term) {
Logger.error('[iTunes] Invalid search options - no term')
return []
}
if (!timeout || isNaN(timeout)) timeout = this.#responseTimeout
const query = {
term: options.term,
media: options.media,
@ -49,12 +54,18 @@ class iTunes {
limit: options.limit,
country: options.country
}
return axios.get('https://itunes.apple.com/search', { params: query }).then((response) => {
return response.data.results || []
}).catch((error) => {
Logger.error(`[iTunes] search request error`, error)
return []
})
return axios
.get('https://itunes.apple.com/search', {
params: query,
timeout
})
.then((response) => {
return response.data.results || []
})
.catch((error) => {
Logger.error(`[iTunes] search request error`, error)
return []
})
}
// Example cover art: https://is1-ssl.mzstatic.com/image/thumb/Music118/v4/cb/ea/73/cbea739b-ff3b-11c4-fb93-7889fbec7390/9781598874983_cover.jpg/100x100bb.jpg
@ -65,20 +76,22 @@ class iTunes {
return data.artworkUrl600
}
// Should already be sorted from small to large
var artworkSizes = Object.keys(data).filter(key => key.startsWith('artworkUrl')).map(key => {
return {
url: data[key],
size: Number(key.replace('artworkUrl', ''))
}
})
var artworkSizes = Object.keys(data)
.filter((key) => key.startsWith('artworkUrl'))
.map((key) => {
return {
url: data[key],
size: Number(key.replace('artworkUrl', ''))
}
})
if (!artworkSizes.length) return null
// Return next biggest size > 600
var nextBestSize = artworkSizes.find(size => size.size > 600)
var nextBestSize = artworkSizes.find((size) => size.size > 600)
if (nextBestSize) return nextBestSize.url
// Find square artwork
var squareArtwork = artworkSizes.find(size => size.url.includes(`${size.size}x${size.size}bb`))
var squareArtwork = artworkSizes.find((size) => size.url.includes(`${size.size}x${size.size}bb`))
// Square cover replace with 600x600bb
if (squareArtwork) {
@ -106,15 +119,21 @@ class iTunes {
}
}
searchAudiobooks(term) {
return this.search({ term, entity: 'audiobook', media: 'audiobook' }).then((results) => {
/**
*
* @param {string} term
* @param {number} [timeout] response timeout in ms
* @returns {Promise<Object[]>}
*/
searchAudiobooks(term, timeout = this.#responseTimeout) {
return this.search({ term, entity: 'audiobook', media: 'audiobook' }, timeout).then((results) => {
return results.map(this.cleanAudiobook.bind(this))
})
}
/**
*
* @param {Object} data
*
* @param {Object} data
* @returns {iTunesPodcastSearchResult}
*/
cleanPodcast(data) {
@ -136,13 +155,14 @@ class iTunes {
}
/**
*
* @param {string} term
* @param {{country:string}} options
*
* @param {string} term
* @param {{country:string}} options
* @param {number} [timeout] response timeout in ms
* @returns {Promise<iTunesPodcastSearchResult[]>}
*/
searchPodcasts(term, options = {}) {
return this.search({ term, entity: 'podcast', media: 'podcast', ...options }).then((results) => {
searchPodcasts(term, options = {}, timeout = this.#responseTimeout) {
return this.search({ term, entity: 'podcast', media: 'podcast', ...options }, timeout).then((results) => {
return results.map(this.cleanPodcast.bind(this))
})
}