From 8296198500809e5583d7c49cf4e30f6e635a42b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Nyi=CC=81ri?= Date: Tue, 4 Jan 2022 19:14:05 +0100 Subject: [PATCH] Update: Refactor playlist generator --- dist/plex-meets-homeassistant.js | 12 ++++++------ src/modules/PlayController.ts | 27 +++++++++++++++++++++------ 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/dist/plex-meets-homeassistant.js b/dist/plex-meets-homeassistant.js index a477f03..479ab81 100644 --- a/dist/plex-meets-homeassistant.js +++ b/dist/plex-meets-homeassistant.js @@ -19510,7 +19510,7 @@ class PlayController { await this.playViaKodi(entity.value, data, data.type); break; case 'vlcTelnet': - await this.playViaVLCTelnet(entity.value, data.Media[0].Part[0].key, data.type); + await this.playViaVLCTelnet(entity.value, data, data.type); break; case 'androidtv': if (lodash.isEqual(data.type, 'epg')) { @@ -19617,8 +19617,8 @@ class PlayController { await this.hass.callService(this.runAfter[0], this.runAfter[1], {}); } }; - this.plexPlayerCreateQueue = async (movieID, plex) => { - const url = `${plex.getBasicURL()}/playQueues?type=video&shuffle=0&repeat=0&continuous=1&own=1&uri=server://${await plex.getServerID()}/com.plexapp.plugins.library/library/metadata/${movieID}`; + this.plexPlayerCreateQueue = async (key, plex, type, shuffle = false, repeat = false, continuous = false) => { + const url = `${plex.getBasicURL()}/playQueues?type=${type}&shuffle=${shuffle ? '1' : '0'}&repeat=${repeat ? '1' : '0'}&continuous=${continuous ? '1' : '0'}&own=1&uri=server://${await plex.getServerID()}/com.plexapp.plugins.library${key}`; const plexResponse = await axios({ method: 'post', url, @@ -19641,7 +19641,7 @@ class PlayController { if (lodash.isObject(entity) && !lodash.isNil(entity.plex)) { plex = entity.plex; } - const { playQueueID, playQueueSelectedMetadataItemID } = await this.plexPlayerCreateQueue(movieID, this.plex); + const { playQueueID, playQueueSelectedMetadataItemID } = await this.plexPlayerCreateQueue(`/library/metadata/${movieID}`, this.plex, 'video'); let url = plex.getBasicURL(); url += `/player/playback/playMedia`; url += `?type=video`; @@ -19756,7 +19756,7 @@ class PlayController { throw Error(`Plex type ${type} is not supported in Kodi.`); } }; - this.playViaVLCTelnet = async (entityName, mediaLink, type) => { + this.playViaVLCTelnet = async (entityName, data, type) => { switch (type) { case 'track': this.hass.callService('media_player', 'play_media', { @@ -19765,7 +19765,7 @@ class PlayController { // eslint-disable-next-line @typescript-eslint/camelcase media_content_type: 'music', // eslint-disable-next-line @typescript-eslint/camelcase - media_content_id: this.plex.authorizeURL(`${this.plex.getBasicURL()}${mediaLink}`) + media_content_id: this.plex.authorizeURL(`${this.plex.getBasicURL()}${data.Media[0].Part[0].key}`) }); break; default: diff --git a/src/modules/PlayController.ts b/src/modules/PlayController.ts index 86e8768..7b3a14f 100644 --- a/src/modules/PlayController.ts +++ b/src/modules/PlayController.ts @@ -157,7 +157,7 @@ class PlayController { await this.playViaKodi(entity.value, data, data.type); break; case 'vlcTelnet': - await this.playViaVLCTelnet(entity.value, data.Media[0].Part[0].key, data.type); + await this.playViaVLCTelnet(entity.value, data, data.type); break; case 'androidtv': if (_.isEqual(data.type, 'epg')) { @@ -281,8 +281,19 @@ class PlayController { } }; - private plexPlayerCreateQueue = async (movieID: number, plex: Plex): Promise> => { - const url = `${plex.getBasicURL()}/playQueues?type=video&shuffle=0&repeat=0&continuous=1&own=1&uri=server://${await plex.getServerID()}/com.plexapp.plugins.library/library/metadata/${movieID}`; + private plexPlayerCreateQueue = async ( + key: string, + plex: Plex, + type: string, + shuffle = false, + repeat = false, + continuous = false + ): Promise> => { + const url = `${plex.getBasicURL()}/playQueues?type=${type}&shuffle=${shuffle ? '1' : '0'}&repeat=${ + repeat ? '1' : '0' + }&continuous=${ + continuous ? '1' : '0' + }&own=1&uri=server://${await plex.getServerID()}/com.plexapp.plugins.library${key}`; const plexResponse = await axios({ method: 'post', @@ -308,7 +319,11 @@ class PlayController { if (_.isObject(entity) && !_.isNil(entity.plex)) { plex = entity.plex; } - const { playQueueID, playQueueSelectedMetadataItemID } = await this.plexPlayerCreateQueue(movieID, this.plex); + const { playQueueID, playQueueSelectedMetadataItemID } = await this.plexPlayerCreateQueue( + `/library/metadata/${movieID}`, + this.plex, + 'video' + ); let url = plex.getBasicURL(); url += `/player/playback/playMedia`; @@ -425,7 +440,7 @@ class PlayController { } }; - private playViaVLCTelnet = async (entityName: string, mediaLink: string, type: string): Promise => { + private playViaVLCTelnet = async (entityName: string, data: Record, type: string): Promise => { switch (type) { case 'track': this.hass.callService('media_player', 'play_media', { @@ -434,7 +449,7 @@ class PlayController { // eslint-disable-next-line @typescript-eslint/camelcase media_content_type: 'music', // eslint-disable-next-line @typescript-eslint/camelcase - media_content_id: this.plex.authorizeURL(`${this.plex.getBasicURL()}${mediaLink}`) + media_content_id: this.plex.authorizeURL(`${this.plex.getBasicURL()}${data.Media[0].Part[0].key}`) }); break; default: