From 8dfbb9aec52965ddd5c2b4073d0ff7c784c020e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Nyi=CC=81ri?= Date: Thu, 12 Aug 2021 13:50:22 +0200 Subject: [PATCH] Add: Playlists as libraries --- dist/plex-meets-homeassistant.js | 31 +++++++++++++++++++++++++++++++ src/editor.ts | 7 +++++++ src/modules/Plex.ts | 18 ++++++++++++++++++ src/plex-meets-homeassistant.ts | 11 +++++++++++ 4 files changed, 67 insertions(+) diff --git a/dist/plex-meets-homeassistant.js b/dist/plex-meets-homeassistant.js index 753597a..60e25c3 100644 --- a/dist/plex-meets-homeassistant.js +++ b/dist/plex-meets-homeassistant.js @@ -18675,6 +18675,7 @@ class Plex { this.requestTimeout = 10000; this.sections = []; this.collections = false; + this.playlists = []; this.init = async () => { await Promise.all([this.getSections(), this.getClients(), this.getServerID()]); }; @@ -18732,6 +18733,17 @@ class Plex { } 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) => { const url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/collections`); const collectionsData = await axios.get(url, { @@ -18782,6 +18794,9 @@ class Plex { this.getCollectionData = async (collectionKey) => { return this.getChildren(collectionKey); }; + this.getPlaylistData = async (playlistKey) => { + return this.getChildren(playlistKey); + }; this.getSectionDataWithoutProcessing = async (sectionID) => { const bulkItems = 50; let url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/all`); @@ -19572,6 +19587,7 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement { this.scriptEntities = []; this.sections = []; this.collections = []; + this.playlists = []; this.clients = {}; this.entitiesRegistry = false; 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.sections = await this.plex.getSections(); this.collections = await this.plex.getCollections(); + this.playlists = await this.plex.getPlaylists(); this.clients = await this.plex.getClients(); this.plexValidSection.style.display = 'none'; this.plexValidSection.innerHTML = ''; @@ -19985,6 +20002,10 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement { lodash.forEach(this.collections, (collection) => { 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.value = this.config.libraryName; let libraryType = ''; @@ -21149,6 +21170,16 @@ class PlexMeetsHomeAssistant extends HTMLElement { if (!lodash.isNil(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) { this.error = `Library name ${this.config.libraryName} does not exist.`; } diff --git a/src/editor.ts b/src/editor.ts index 2a786bf..f4428f1 100644 --- a/src/editor.ts +++ b/src/editor.ts @@ -58,6 +58,8 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement { collections: Array> = []; + playlists: Array> = []; + clients: Record = {}; entitiesRegistry: false | Array> = 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.sections = await this.plex.getSections(); this.collections = await this.plex.getCollections(); + this.playlists = await this.plex.getPlaylists(); this.clients = await this.plex.getClients(); this.plexValidSection.style.display = 'none'; @@ -498,6 +501,10 @@ class PlexMeetsHomeAssistantEditor extends HTMLElement { _.forEach(this.collections, (collection: Record) => { libraryItems.appendChild(addDropdownItem(collection.title)); }); + libraryItems.appendChild(addDropdownItem('Playlists', true)); + _.forEach(this.playlists, (playlist: Record) => { + libraryItems.appendChild(addDropdownItem(playlist.title)); + }); this.libraryName.disabled = false; this.libraryName.value = this.config.libraryName; diff --git a/src/modules/Plex.ts b/src/modules/Plex.ts index cde1fa9..cfefa4e 100644 --- a/src/modules/Plex.ts +++ b/src/modules/Plex.ts @@ -23,6 +23,8 @@ class Plex { collections: Array> | false = false; + playlists: Array> = []; + constructor( ip: string, port: number | false = false, @@ -101,6 +103,18 @@ class Plex { return this.collections; }; + getPlaylists = async (): Promise>> => { + 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>> => { const url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/collections`); const collectionsData = await axios.get(url, { @@ -165,6 +179,10 @@ class Plex { return this.getChildren(collectionKey); }; + getPlaylistData = async (playlistKey: string): Promise => { + return this.getChildren(playlistKey); + }; + private getSectionDataWithoutProcessing = async (sectionID: number): Promise => { const bulkItems = 50; let url = this.authorizeURL(`${this.getBasicURL()}/library/sections/${sectionID}/all`); diff --git a/src/plex-meets-homeassistant.ts b/src/plex-meets-homeassistant.ts index a51eeed..e19ef08 100644 --- a/src/plex-meets-homeassistant.ts +++ b/src/plex-meets-homeassistant.ts @@ -437,6 +437,17 @@ class PlexMeetsHomeAssistant extends HTMLElement { this.data[collectionToGet.title] = await this.plex.getCollectionData(collectionToGet.key); } + const playlists = await this.plex.getPlaylists(); + let playlistToGet: Record = {}; + _.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) { this.error = `Library name ${this.config.libraryName} does not exist.`; }