Add #28: Collections into UI editor

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

@ -18674,6 +18674,7 @@ class Plex {
this.clients = []; this.clients = [];
this.requestTimeout = 10000; this.requestTimeout = 10000;
this.sections = []; this.sections = [];
this.collections = false;
this.init = async () => { this.init = async () => {
await Promise.all([this.getSections(), this.getClients(), this.getServerID()]); await Promise.all([this.getSections(), this.getClients(), this.getServerID()]);
}; };
@ -18713,6 +18714,31 @@ class Plex {
} }
return this.sections; return this.sections;
}; };
this.getCollections = async () => {
if (!lodash.isArray(this.collections)) {
const sections = await this.getSections();
const collectionRequests = [];
lodash.forEach(sections, section => {
collectionRequests.push(this.getCollection(section.key));
});
const allResults = await Promise.all(collectionRequests);
const collections = [];
lodash.forEach(allResults, result => {
lodash.forEach(result, collection => {
collections.push(collection);
});
});
this.collections = collections;
}
return this.collections;
};
this.getCollection = async (sectionID) => {
const url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/collections`);
const collectionsData = await axios.get(url, {
timeout: this.requestTimeout
});
return lodash.isNil(collectionsData.data.MediaContainer.Metadata) ? [] : collectionsData.data.MediaContainer.Metadata;
};
this.getSectionData = async (sectionID) => { this.getSectionData = async (sectionID) => {
return this.exportSectionsData([await this.getSectionDataWithoutProcessing(sectionID)]); return this.exportSectionsData([await this.getSectionDataWithoutProcessing(sectionID)]);
}; };
@ -19505,6 +19531,7 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
this.entities = []; this.entities = [];
this.scriptEntities = []; this.scriptEntities = [];
this.sections = []; this.sections = [];
this.collections = [];
this.clients = {}; this.clients = {};
this.entitiesRegistry = false; this.entitiesRegistry = false;
this.plexValidSection = document.createElement('div'); this.plexValidSection = document.createElement('div');
@ -19590,9 +19617,12 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
} }
}; };
this.render = async () => { this.render = async () => {
const addDropdownItem = (text) => { const addDropdownItem = (text, disabled = false) => {
const libraryItem = document.createElement('paper-item'); const libraryItem = document.createElement('paper-item');
libraryItem.innerHTML = text; libraryItem.innerHTML = text;
if (disabled) {
libraryItem.disabled = true;
}
return libraryItem; return libraryItem;
}; };
const createEntitiesDropdown = (selected, changeHandler) => { const createEntitiesDropdown = (selected, changeHandler) => {
@ -19692,6 +19722,7 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
this.content.appendChild(this.token); this.content.appendChild(this.token);
this.libraryName.innerHTML = ''; this.libraryName.innerHTML = '';
const libraryItems = document.createElement('paper-listbox'); const libraryItems = document.createElement('paper-listbox');
libraryItems.appendChild(addDropdownItem('Smart Libraries', true));
libraryItems.appendChild(addDropdownItem('Continue Watching')); libraryItems.appendChild(addDropdownItem('Continue Watching'));
libraryItems.appendChild(addDropdownItem('Deck')); libraryItems.appendChild(addDropdownItem('Deck'));
libraryItems.appendChild(addDropdownItem('Recently Added')); libraryItems.appendChild(addDropdownItem('Recently Added'));
@ -19706,6 +19737,7 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
this.appendChild(this.content); this.appendChild(this.content);
this.plex = new Plex(this.config.ip, this.plexPort, this.config.token, this.plexProtocol, this.config.sort); this.plex = new Plex(this.config.ip, this.plexPort, this.config.token, this.plexProtocol, this.config.sort);
this.sections = await this.plex.getSections(); this.sections = await this.plex.getSections();
this.collections = await this.plex.getCollections();
this.clients = await this.plex.getClients(); this.clients = await this.plex.getClients();
this.plexValidSection.style.display = 'none'; this.plexValidSection.style.display = 'none';
this.plexValidSection.innerHTML = ''; this.plexValidSection.innerHTML = '';
@ -19905,9 +19937,14 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
this.runAfter.value = this.config.runAfter; this.runAfter.value = this.config.runAfter;
this.plexValidSection.appendChild(this.runAfter); this.plexValidSection.appendChild(this.runAfter);
if (!lodash.isEmpty(this.sections)) { if (!lodash.isEmpty(this.sections)) {
libraryItems.appendChild(addDropdownItem('Libraries', true));
lodash.forEach(this.sections, (section) => { lodash.forEach(this.sections, (section) => {
libraryItems.appendChild(addDropdownItem(section.title)); libraryItems.appendChild(addDropdownItem(section.title));
}); });
libraryItems.appendChild(addDropdownItem('Collections', true));
lodash.forEach(this.collections, (collection) => {
libraryItems.appendChild(addDropdownItem(collection.title));
});
this.libraryName.disabled = false; this.libraryName.disabled = false;
this.libraryName.value = this.config.libraryName; this.libraryName.value = this.config.libraryName;
let libraryType = ''; let libraryType = '';

@ -56,6 +56,8 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
sections: Array<Record<string, any>> = []; sections: Array<Record<string, any>> = [];
collections: Array<Record<string, any>> = [];
clients: Record<string, any> = {}; clients: Record<string, any> = {};
entitiesRegistry: false | Array<Record<string, any>> = false; entitiesRegistry: false | Array<Record<string, any>> = false;
@ -148,9 +150,12 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
}; };
render = async (): Promise<void> => { render = async (): Promise<void> => {
const addDropdownItem = (text: string): HTMLElement => { const addDropdownItem = (text: string, disabled = false): HTMLElement => {
const libraryItem: any = document.createElement('paper-item'); const libraryItem: any = document.createElement('paper-item');
libraryItem.innerHTML = text; libraryItem.innerHTML = text;
if (disabled) {
libraryItem.disabled = true;
}
return libraryItem; return libraryItem;
}; };
const createEntitiesDropdown = (selected: string, changeHandler: Function): HTMLElement | false => { const createEntitiesDropdown = (selected: string, changeHandler: Function): HTMLElement | false => {
@ -259,6 +264,8 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
this.libraryName.innerHTML = ''; this.libraryName.innerHTML = '';
const libraryItems: any = document.createElement('paper-listbox'); const libraryItems: any = document.createElement('paper-listbox');
libraryItems.appendChild(addDropdownItem('Smart Libraries', true));
libraryItems.appendChild(addDropdownItem('Continue Watching')); libraryItems.appendChild(addDropdownItem('Continue Watching'));
libraryItems.appendChild(addDropdownItem('Deck')); libraryItems.appendChild(addDropdownItem('Deck'));
libraryItems.appendChild(addDropdownItem('Recently Added')); libraryItems.appendChild(addDropdownItem('Recently Added'));
@ -275,6 +282,7 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
this.plex = new Plex(this.config.ip, this.plexPort, this.config.token, this.plexProtocol, this.config.sort); this.plex = new Plex(this.config.ip, this.plexPort, this.config.token, this.plexProtocol, this.config.sort);
this.sections = await this.plex.getSections(); this.sections = await this.plex.getSections();
this.collections = await this.plex.getCollections();
this.clients = await this.plex.getClients(); this.clients = await this.plex.getClients();
this.plexValidSection.style.display = 'none'; this.plexValidSection.style.display = 'none';
@ -482,9 +490,14 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
this.plexValidSection.appendChild(this.runAfter); this.plexValidSection.appendChild(this.runAfter);
if (!_.isEmpty(this.sections)) { if (!_.isEmpty(this.sections)) {
libraryItems.appendChild(addDropdownItem('Libraries', true));
_.forEach(this.sections, (section: Record<string, any>) => { _.forEach(this.sections, (section: Record<string, any>) => {
libraryItems.appendChild(addDropdownItem(section.title)); libraryItems.appendChild(addDropdownItem(section.title));
}); });
libraryItems.appendChild(addDropdownItem('Collections', true));
_.forEach(this.collections, (collection: Record<string, any>) => {
libraryItems.appendChild(addDropdownItem(collection.title));
});
this.libraryName.disabled = false; this.libraryName.disabled = false;
this.libraryName.value = this.config.libraryName; this.libraryName.value = this.config.libraryName;

@ -21,6 +21,8 @@ class Plex {
sections: Array<Record<string, any>> = []; sections: Array<Record<string, any>> = [];
collections: Array<Record<string, any>> | false = false;
constructor( constructor(
ip: string, ip: string,
port: number | false = false, port: number | false = false,
@ -80,6 +82,33 @@ class Plex {
return this.sections; return this.sections;
}; };
getCollections = async (): Promise<Array<Record<string, any>>> => {
if (!_.isArray(this.collections)) {
const sections = await this.getSections();
const collectionRequests: Array<Promise<any>> = [];
_.forEach(sections, section => {
collectionRequests.push(this.getCollection(section.key));
});
const allResults = await Promise.all(collectionRequests);
const collections: Array<Record<string, any>> = [];
_.forEach(allResults, result => {
_.forEach(result, collection => {
collections.push(collection);
});
});
this.collections = collections;
}
return this.collections;
};
getCollection = async (sectionID: number): Promise<Array<Record<string, any>>> => {
const url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/collections`);
const collectionsData = await axios.get(url, {
timeout: this.requestTimeout
});
return _.isNil(collectionsData.data.MediaContainer.Metadata) ? [] : collectionsData.data.MediaContainer.Metadata;
};
getSectionData = async (sectionID: number): Promise<any> => { getSectionData = async (sectionID: number): Promise<any> => {
return this.exportSectionsData([await this.getSectionDataWithoutProcessing(sectionID)]); return this.exportSectionsData([await this.getSectionDataWithoutProcessing(sectionID)]);
}; };

Loading…
Cancel
Save