Fix #23: Some Plex servers refusing to return all metadata for section in one call

pull/30/head 2.0.9
Juraj Nyíri 3 years ago
parent 82e00702bc
commit c64d5ad94d

@ -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));
};

@ -85,18 +85,60 @@ class Plex {
return this.sections;
};
getSectionsData = async (): Promise<any> => {
const sections = await this.getSections();
const sectionsRequests: Array<Promise<any>> = [];
_.forEach(sections, section => {
let url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${section.key}/all`);
getSectionData = async (sectionID: number): Promise<any> => {
const bulkItems = 50;
let url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/all`);
url += `&sort=${this.sort}`;
let result: Record<string, any> = {};
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<Promise<any>> = [];
while (startOfItems < totalSize) {
sectionsRequests.push(
axios.get(url, {
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<any> => {
const sections = await this.getSections();
const sectionsRequests: Array<Promise<any>> = [];
_.forEach(sections, section => {
sectionsRequests.push(this.getSectionData(section.key));
});
return this.exportSectionsData(await Promise.all(sectionsRequests));
};

Loading…
Cancel
Save