Add: Playlists as libraries

pull/30/head
Juraj Nyíri 3 years ago
parent e3dab9e5c6
commit 8dfbb9aec5

@ -18675,6 +18675,7 @@ class Plex {
this.requestTimeout = 10000; this.requestTimeout = 10000;
this.sections = []; this.sections = [];
this.collections = false; this.collections = false;
this.playlists = [];
this.init = async () => { this.init = async () => {
await Promise.all([this.getSections(), this.getClients(), this.getServerID()]); await Promise.all([this.getSections(), this.getClients(), this.getServerID()]);
}; };
@ -18732,6 +18733,17 @@ class Plex {
} }
return this.collections; return this.collections;
}; };
this.getPlaylists = async () => {
if (lodash.isEmpty(this.playlists)) {
this.playlists = [];
const url = this.authorizeURL(`${this.getBasicURL()}/playlists`);
const playlistsData = await axios.get(url, {
timeout: this.requestTimeout
});
this.playlists = playlistsData.data.MediaContainer.Metadata;
}
return this.playlists;
};
this.getCollection = async (sectionID) => { this.getCollection = async (sectionID) => {
const url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/collections`); const url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/collections`);
const collectionsData = await axios.get(url, { const collectionsData = await axios.get(url, {
@ -18782,6 +18794,9 @@ class Plex {
this.getCollectionData = async (collectionKey) => { this.getCollectionData = async (collectionKey) => {
return this.getChildren(collectionKey); return this.getChildren(collectionKey);
}; };
this.getPlaylistData = async (playlistKey) => {
return this.getChildren(playlistKey);
};
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`);
@ -19572,6 +19587,7 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
this.scriptEntities = []; this.scriptEntities = [];
this.sections = []; this.sections = [];
this.collections = []; this.collections = [];
this.playlists = [];
this.clients = {}; this.clients = {};
this.entitiesRegistry = false; this.entitiesRegistry = false;
this.plexValidSection = document.createElement('div'); this.plexValidSection = document.createElement('div');
@ -19778,6 +19794,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.collections = await this.plex.getCollections();
this.playlists = await this.plex.getPlaylists();
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 = '';
@ -19985,6 +20002,10 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
lodash.forEach(this.collections, (collection) => { lodash.forEach(this.collections, (collection) => {
libraryItems.appendChild(addDropdownItem(collection.title)); libraryItems.appendChild(addDropdownItem(collection.title));
}); });
libraryItems.appendChild(addDropdownItem('Playlists', true));
lodash.forEach(this.playlists, (playlist) => {
libraryItems.appendChild(addDropdownItem(playlist.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 = '';
@ -21149,6 +21170,16 @@ class PlexMeetsHomeAssistant extends HTMLElement {
if (!lodash.isNil(collectionToGet.key)) { if (!lodash.isNil(collectionToGet.key)) {
this.data[collectionToGet.title] = await this.plex.getCollectionData(collectionToGet.key); this.data[collectionToGet.title] = await this.plex.getCollectionData(collectionToGet.key);
} }
const playlists = await this.plex.getPlaylists();
let playlistToGet = {};
lodash.forEach(playlists, playlist => {
if (this.plex && lodash.isEqual(playlist.title, this.config.libraryName)) {
playlistToGet = playlist;
}
});
if (!lodash.isNil(playlistToGet.key)) {
this.data[playlistToGet.title] = await this.plex.getPlaylistData(playlistToGet.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.`;
} }

@ -58,6 +58,8 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
collections: Array<Record<string, any>> = []; collections: Array<Record<string, any>> = [];
playlists: 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;
@ -283,6 +285,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.collections = await this.plex.getCollections();
this.playlists = await this.plex.getPlaylists();
this.clients = await this.plex.getClients(); this.clients = await this.plex.getClients();
this.plexValidSection.style.display = 'none'; this.plexValidSection.style.display = 'none';
@ -498,6 +501,10 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement {
_.forEach(this.collections, (collection: Record<string, any>) => { _.forEach(this.collections, (collection: Record<string, any>) => {
libraryItems.appendChild(addDropdownItem(collection.title)); libraryItems.appendChild(addDropdownItem(collection.title));
}); });
libraryItems.appendChild(addDropdownItem('Playlists', true));
_.forEach(this.playlists, (playlist: Record<string, any>) => {
libraryItems.appendChild(addDropdownItem(playlist.title));
});
this.libraryName.disabled = false; this.libraryName.disabled = false;
this.libraryName.value = this.config.libraryName; this.libraryName.value = this.config.libraryName;

@ -23,6 +23,8 @@ class Plex {
collections: Array<Record<string, any>> | false = false; collections: Array<Record<string, any>> | false = false;
playlists: Array<Record<string, any>> = [];
constructor( constructor(
ip: string, ip: string,
port: number | false = false, port: number | false = false,
@ -101,6 +103,18 @@ class Plex {
return this.collections; return this.collections;
}; };
getPlaylists = async (): Promise<Array<Record<string, any>>> => {
if (_.isEmpty(this.playlists)) {
this.playlists = [];
const url = this.authorizeURL(`${this.getBasicURL()}/playlists`);
const playlistsData = await axios.get(url, {
timeout: this.requestTimeout
});
this.playlists = playlistsData.data.MediaContainer.Metadata;
}
return this.playlists;
};
getCollection = async (sectionID: number): Promise<Array<Record<string, any>>> => { getCollection = async (sectionID: number): Promise<Array<Record<string, any>>> => {
const url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/collections`); const url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/collections`);
const collectionsData = await axios.get(url, { const collectionsData = await axios.get(url, {
@ -165,6 +179,10 @@ class Plex {
return this.getChildren(collectionKey); return this.getChildren(collectionKey);
}; };
getPlaylistData = async (playlistKey: string): Promise<any> => {
return this.getChildren(playlistKey);
};
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`);

@ -437,6 +437,17 @@ class PlexMeetsHomeAssistant extends HTMLElement {
this.data[collectionToGet.title] = await this.plex.getCollectionData(collectionToGet.key); this.data[collectionToGet.title] = await this.plex.getCollectionData(collectionToGet.key);
} }
const playlists = await this.plex.getPlaylists();
let playlistToGet: Record<string, any> = {};
_.forEach(playlists, playlist => {
if (this.plex && _.isEqual(playlist.title, this.config.libraryName)) {
playlistToGet = playlist;
}
});
if (!_.isNil(playlistToGet.key)) {
this.data[playlistToGet.title] = await this.plex.getPlaylistData(playlistToGet.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