From 8e66af961dfc211a39c9155e9d58791b5ec674bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Nyi=CC=81ri?= Date: Wed, 5 May 2021 13:21:07 +0200 Subject: [PATCH] Add: Get seasons for plex --- dist/plex-meets-homeassistant.js | 39 +++++++++++++++++++++----------- src/modules/Plex.ts | 5 ++++ src/plex-meets-homeassistant.ts | 39 ++++++++++++++++++++------------ 3 files changed, 55 insertions(+), 28 deletions(-) diff --git a/dist/plex-meets-homeassistant.js b/dist/plex-meets-homeassistant.js index f11d52d..322ab4d 100644 --- a/dist/plex-meets-homeassistant.js +++ b/dist/plex-meets-homeassistant.js @@ -18670,6 +18670,10 @@ class Plex { }); return this.exportSectionsData(await Promise.all(sectionsRequests)); }; + this.getLibraryData = async (id) => { + const url = `${this.protocol}://${this.ip}:${this.port}/library/metadata/${id}/children?X-Plex-Token=${this.token}`; + return (await axios.get(url)).data.MediaContainer.Metadata; + }; this.exportSectionsData = (sectionsData) => { const processedData = []; lodash.forEach(sectionsData, sectionData => { @@ -19149,6 +19153,7 @@ class PlexMeetsHomeAssistant extends HTMLElement { constructor() { super(...arguments); this.plexProtocol = 'http'; + this.plex = undefined; this.movieElems = []; this.detailElem = undefined; this.data = {}; @@ -19162,19 +19167,23 @@ class PlexMeetsHomeAssistant extends HTMLElement { this.loadInitialData = async (hass) => { this.loading = true; this.renderPage(hass); - const plex = new Plex(this.config.ip, this.config.port, this.config.token, this.plexProtocol); try { - const [plexInfo, plexSections] = await Promise.all([plex.getServerInfo(), plex.getSectionsData()]); - // eslint-disable-next-line @typescript-eslint/camelcase - this.data.serverID = plexInfo; - lodash.forEach(plexSections, section => { - this.data[section.title1] = section.Metadata; - }); - if (this.data[this.config.libraryName] === undefined) { - this.error = `Library name ${this.config.libraryName} does not exist.`; + if (this.plex) { + const [plexInfo, plexSections] = await Promise.all([this.plex.getServerInfo(), this.plex.getSectionsData()]); + // eslint-disable-next-line @typescript-eslint/camelcase + this.data.serverID = plexInfo; + lodash.forEach(plexSections, section => { + this.data[section.title1] = section.Metadata; + }); + if (this.data[this.config.libraryName] === undefined) { + this.error = `Library name ${this.config.libraryName} does not exist.`; + } + this.loading = false; + this.render(hass); + } + else { + throw Error('Plex not initialized.'); } - this.loading = false; - this.render(hass); } catch (err) { // todo: proper timeout here @@ -19313,13 +19322,12 @@ class PlexMeetsHomeAssistant extends HTMLElement { this.detailElem.style.visibility = 'hidden'; } }; - this.showDetails = (data) => { + this.showDetails = async (data) => { const doc = document.documentElement; const top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0); if (this.detailElem) { this.detailElem.style.transition = '0s'; this.detailElem.style.top = `${top - 1000}px`; - console.log(this.detailElem.style.top); setTimeout(() => { if (this.detailElem) { this.detailElem.style.visibility = 'visible'; @@ -19349,6 +19357,10 @@ class PlexMeetsHomeAssistant extends HTMLElement { } }, 200); } + if (this.plex) { + const seasonsData = await this.plex.getLibraryData(data.key.split('/')[3]); + console.log(seasonsData); + } }; this.showBackground = () => { const contentbg = this.getElementsByClassName('contentbg'); @@ -19467,6 +19479,7 @@ class PlexMeetsHomeAssistant extends HTMLElement { if (config.maxCount) { this.maxCount = config.maxCount; } + this.plex = new Plex(this.config.ip, this.config.port, this.config.token, this.plexProtocol); }; this.getCardSize = () => { return 3; diff --git a/src/modules/Plex.ts b/src/modules/Plex.ts index 01c146f..7cb3f2e 100644 --- a/src/modules/Plex.ts +++ b/src/modules/Plex.ts @@ -41,6 +41,11 @@ class Plex { return this.exportSectionsData(await Promise.all(sectionsRequests)); }; + getLibraryData = async (id: number): Promise => { + const url = `${this.protocol}://${this.ip}:${this.port}/library/metadata/${id}/children?X-Plex-Token=${this.token}`; + return (await axios.get(url)).data.MediaContainer.Metadata; + }; + private exportSectionsData = (sectionsData: Array): Array => { const processedData: Array = []; _.forEach(sectionsData, sectionData => { diff --git a/src/plex-meets-homeassistant.ts b/src/plex-meets-homeassistant.ts index 6761548..2838a92 100644 --- a/src/plex-meets-homeassistant.ts +++ b/src/plex-meets-homeassistant.ts @@ -10,6 +10,8 @@ import style from './modules/style'; class PlexMeetsHomeAssistant extends HTMLElement { plexProtocol: 'http' | 'https' = 'http'; + plex: Plex | undefined = undefined; + movieElems: any = []; detailElem: HTMLElement | undefined = undefined; @@ -49,22 +51,24 @@ class PlexMeetsHomeAssistant extends HTMLElement { loadInitialData = async (hass: HomeAssistant): Promise => { this.loading = true; this.renderPage(hass); - - const plex = new Plex(this.config.ip, this.config.port, this.config.token, this.plexProtocol); try { - const [plexInfo, plexSections] = await Promise.all([plex.getServerInfo(), plex.getSectionsData()]); - // eslint-disable-next-line @typescript-eslint/camelcase - this.data.serverID = plexInfo; - _.forEach(plexSections, section => { - this.data[section.title1] = section.Metadata; - }); + if (this.plex) { + const [plexInfo, plexSections] = await Promise.all([this.plex.getServerInfo(), this.plex.getSectionsData()]); + // eslint-disable-next-line @typescript-eslint/camelcase + this.data.serverID = plexInfo; + _.forEach(plexSections, section => { + this.data[section.title1] = section.Metadata; + }); - if (this.data[this.config.libraryName] === undefined) { - this.error = `Library name ${this.config.libraryName} does not exist.`; - } + if (this.data[this.config.libraryName] === undefined) { + this.error = `Library name ${this.config.libraryName} does not exist.`; + } - this.loading = false; - this.render(hass); + this.loading = false; + this.render(hass); + } else { + throw Error('Plex not initialized.'); + } } catch (err) { // todo: proper timeout here this.error = `Plex server did not respond.
Details of the error: ${escapeHtml(err.message)}`; @@ -216,13 +220,12 @@ class PlexMeetsHomeAssistant extends HTMLElement { } }; - showDetails = (data: any): void => { + showDetails = async (data: any): Promise => { const doc = document.documentElement; const top = (window.pageYOffset || doc.scrollTop) - (doc.clientTop || 0); if (this.detailElem) { this.detailElem.style.transition = '0s'; this.detailElem.style.top = `${top - 1000}px`; - console.log(this.detailElem.style.top); setTimeout(() => { if (this.detailElem) { @@ -258,6 +261,10 @@ class PlexMeetsHomeAssistant extends HTMLElement { } }, 200); } + if (this.plex) { + const seasonsData = await this.plex.getLibraryData(data.key.split('/')[3]); + console.log(seasonsData); + } }; showBackground = (): void => { @@ -395,6 +402,8 @@ class PlexMeetsHomeAssistant extends HTMLElement { if (config.maxCount) { this.maxCount = config.maxCount; } + + this.plex = new Plex(this.config.ip, this.config.port, this.config.token, this.plexProtocol); }; getCardSize = (): number => {