Add #28: Getting data for selected collection

pull/30/head
Juraj Nyíri 3 years ago
parent 099e8a029b
commit dd5a9c0e7a

@ -18742,6 +18742,46 @@ class Plex {
this.getSectionData = async (sectionID) => { this.getSectionData = async (sectionID) => {
return this.exportSectionsData([await this.getSectionDataWithoutProcessing(sectionID)]); return this.exportSectionsData([await this.getSectionDataWithoutProcessing(sectionID)]);
}; };
this.getChildren = async (childrenURL) => {
const bulkItems = 50;
let url = this.authorizeURL(`${this.getBasicURL()}${childrenURL}`);
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()}${childrenURL}?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.data.MediaContainer.Metadata;
};
this.getCollectionData = async (collectionKey) => {
return this.getChildren(collectionKey);
};
this.getSectionDataWithoutProcessing = async (sectionID) => { this.getSectionDataWithoutProcessing = async (sectionID) => {
const bulkItems = 50; const bulkItems = 50;
let url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/all`); let url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/all`);
@ -21099,6 +21139,14 @@ class PlexMeetsHomeAssistant extends HTMLElement {
this.data[section.title1] = section.Metadata; this.data[section.title1] = section.Metadata;
}); });
} }
const collections = await this.plex.getCollections();
let collectionToGet = {};
lodash.forEach(collections, collection => {
if (this.plex && lodash.isEqual(collection.title, this.config.libraryName)) {
collectionToGet = collection;
}
});
this.data[collectionToGet.title] = await this.plex.getCollectionData(collectionToGet.key);
if (this.data[this.config.libraryName] === undefined) { if (this.data[this.config.libraryName] === undefined) {
this.error = `Library name ${this.config.libraryName} does not exist.`; this.error = `Library name ${this.config.libraryName} does not exist.`;
} }

@ -113,6 +113,58 @@ class Plex {
return this.exportSectionsData([await this.getSectionDataWithoutProcessing(sectionID)]); return this.exportSectionsData([await this.getSectionDataWithoutProcessing(sectionID)]);
}; };
private getChildren = async (childrenURL: string): Promise<any> => {
const bulkItems = 50;
let url = this.authorizeURL(`${this.getBasicURL()}${childrenURL}`);
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(
this.authorizeURL(
`${this.getBasicURL()}${childrenURL}?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.data.MediaContainer.Metadata;
};
getCollectionData = async (collectionKey: string): Promise<any> => {
return this.getChildren(collectionKey);
};
private getSectionDataWithoutProcessing = async (sectionID: number): Promise<any> => { private getSectionDataWithoutProcessing = async (sectionID: number): Promise<any> => {
const bulkItems = 50; const bulkItems = 50;
let url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/all`); let url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/all`);

@ -426,6 +426,14 @@ class PlexMeetsHomeAssistant extends HTMLElement {
this.data[section.title1] = section.Metadata; this.data[section.title1] = section.Metadata;
}); });
} }
const collections = await this.plex.getCollections();
let collectionToGet: Record<string, any> = {};
_.forEach(collections, collection => {
if (this.plex && _.isEqual(collection.title, this.config.libraryName)) {
collectionToGet = collection;
}
});
this.data[collectionToGet.title] = await this.plex.getCollectionData(collectionToGet.key);
if (this.data[this.config.libraryName] === undefined) { if (this.data[this.config.libraryName] === undefined) {
this.error = `Library name ${this.config.libraryName} does not exist.`; this.error = `Library name ${this.config.libraryName} does not exist.`;

Loading…
Cancel
Save