diff --git a/dist/plex-meets-homeassistant.js b/dist/plex-meets-homeassistant.js index c22de77..1ec5f6d 100644 --- a/dist/plex-meets-homeassistant.js +++ b/dist/plex-meets-homeassistant.js @@ -18718,15 +18718,48 @@ class Plex { } return this.sections; }; + this.getSectionData = async (sectionID) => { + const bulkItems = 50; + let url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/all`); + url += `&sort=${this.sort}`; + let result = {}; + try { + result = await axios.get(url, { + timeout: this.requestTimeout + }); + } + catch (err) { + // probably hitting limit of items to return, we need to request in parts + if (lodash.includes(err.message, 'Request failed with status code 500')) { + url += `&X-Plex-Container-Start=0&X-Plex-Container-Size=${bulkItems}`; + result = await axios.get(url, { + timeout: this.requestTimeout + }); + const { totalSize } = result.data.MediaContainer; + let startOfItems = bulkItems; + const sectionsRequests = []; + while (startOfItems < totalSize) { + sectionsRequests.push(axios.get(this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/all?sort=${this.sort}&X-Plex-Container-Start=${startOfItems}&X-Plex-Container-Size=${bulkItems}`), { + timeout: this.requestTimeout + })); + startOfItems += bulkItems; + } + const allResults = await Promise.all(sectionsRequests); + lodash.forEach(allResults, multiResult => { + result.data.MediaContainer.Metadata = lodash.concat(result.data.MediaContainer.Metadata, multiResult.data.MediaContainer.Metadata); + }); + } + else { + throw err; + } + } + return result; + }; this.getSectionsData = async () => { const sections = await this.getSections(); const sectionsRequests = []; lodash.forEach(sections, section => { - let url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${section.key}/all`); - url += `&sort=${this.sort}`; - sectionsRequests.push(axios.get(url, { - timeout: this.requestTimeout - })); + sectionsRequests.push(this.getSectionData(section.key)); }); return this.exportSectionsData(await Promise.all(sectionsRequests)); }; diff --git a/src/modules/Plex.ts b/src/modules/Plex.ts index 2bdea2f..83639af 100644 --- a/src/modules/Plex.ts +++ b/src/modules/Plex.ts @@ -85,17 +85,59 @@ class Plex { return this.sections; }; + getSectionData = async (sectionID: number): Promise => { + const bulkItems = 50; + let url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/all`); + url += `&sort=${this.sort}`; + let result: Record = {}; + try { + result = await axios.get(url, { + timeout: this.requestTimeout + }); + } catch (err) { + // probably hitting limit of items to return, we need to request in parts + if (_.includes(err.message, 'Request failed with status code 500')) { + url += `&X-Plex-Container-Start=0&X-Plex-Container-Size=${bulkItems}`; + result = await axios.get(url, { + timeout: this.requestTimeout + }); + const { totalSize } = result.data.MediaContainer; + let startOfItems = bulkItems; + const sectionsRequests: Array> = []; + while (startOfItems < totalSize) { + sectionsRequests.push( + axios.get( + this.authorizeURL( + `${this.getBasicURL()}/library/sections/${sectionID}/all?sort=${ + this.sort + }&X-Plex-Container-Start=${startOfItems}&X-Plex-Container-Size=${bulkItems}` + ), + { + timeout: this.requestTimeout + } + ) + ); + startOfItems += bulkItems; + } + const allResults = await Promise.all(sectionsRequests); + _.forEach(allResults, multiResult => { + result.data.MediaContainer.Metadata = _.concat( + result.data.MediaContainer.Metadata, + multiResult.data.MediaContainer.Metadata + ); + }); + } else { + throw err; + } + } + return result; + }; + getSectionsData = async (): Promise => { const sections = await this.getSections(); const sectionsRequests: Array> = []; _.forEach(sections, section => { - let url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${section.key}/all`); - url += `&sort=${this.sort}`; - sectionsRequests.push( - axios.get(url, { - timeout: this.requestTimeout - }) - ); + sectionsRequests.push(this.getSectionData(section.key)); }); return this.exportSectionsData(await Promise.all(sectionsRequests)); };