Add: Ability to play media from shared servers via plexPlayer

pull/16/head
Juraj Nyíri 3 years ago
parent a410779873
commit d199719708

@ -18672,7 +18672,7 @@ class Plex {
constructor(ip, port = false, token, protocol = 'http', sort = 'titleSort:asc') { constructor(ip, port = false, token, protocol = 'http', sort = 'titleSort:asc') {
this.serverInfo = {}; this.serverInfo = {};
this.clients = []; this.clients = [];
this.requestTimeout = 5000; this.requestTimeout = 10000;
this.sections = []; this.sections = [];
this.init = async () => { this.init = async () => {
await this.getClients(); await this.getClients();
@ -18893,7 +18893,6 @@ class PlayController {
return foundResult; return foundResult;
}; };
this.play = async (data, instantPlay = false) => { this.play = async (data, instantPlay = false) => {
console.log(data);
if (lodash.isArray(this.runBefore)) { if (lodash.isArray(this.runBefore)) {
await this.hass.callService(this.runBefore[0], this.runBefore[1], {}); await this.hass.callService(this.runBefore[0], this.runBefore[1], {});
} }
@ -18918,13 +18917,13 @@ class PlayController {
await this.hass.callService(this.runAfter[0], this.runAfter[1], {}); await this.hass.callService(this.runAfter[0], this.runAfter[1], {});
} }
}; };
this.plexPlayerCreateQueue = async (movieID) => { this.plexPlayerCreateQueue = async (movieID, plex) => {
const url = `${this.plex.protocol}://${this.plex.ip}:${this.plex.port}/playQueues?type=video&shuffle=0&repeat=0&continuous=1&own=1&uri=server://${await this.plex.getServerID()}/com.plexapp.plugins.library/library/metadata/${movieID}`; 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}`;
const plexResponse = await axios({ const plexResponse = await axios({
method: 'post', method: 'post',
url, url,
headers: { headers: {
'X-Plex-Token': this.plex.token, 'X-Plex-Token': plex.token,
'X-Plex-Client-Identifier': 'PlexMeetsHomeAssistant' 'X-Plex-Client-Identifier': 'PlexMeetsHomeAssistant'
} }
}); });
@ -18936,10 +18935,27 @@ class PlayController {
playQueueSelectedMetadataItemID: plexResponse.data.MediaContainer.playQueueSelectedMetadataItemID playQueueSelectedMetadataItemID: plexResponse.data.MediaContainer.playQueueSelectedMetadataItemID
}; };
}; };
this.playViaPlexPlayer = async (entityName, movieID) => { this.playViaPlexPlayer = async (entity, movieID) => {
const machineID = this.getPlexPlayerMachineIdentifier(entityName); const machineID = this.getPlexPlayerMachineIdentifier(entity);
const { playQueueID, playQueueSelectedMetadataItemID } = await this.plexPlayerCreateQueue(movieID); let { plex } = this;
const url = `${this.plex.protocol}://${this.plex.ip}:${this.plex.port}/player/playback/playMedia?address=${this.plex.ip}&commandID=1&containerKey=/playQueues/${playQueueID}?window=100%26own=1&key=/library/metadata/${playQueueSelectedMetadataItemID}&machineIdentifier=${await this.plex.getServerID()}&offset=0&port=${this.plex.port}&token=${this.plex.token}&type=video&protocol=${this.plex.protocol}`; if (lodash.isObject(entity) && !lodash.isNil(entity.plex)) {
plex = entity.plex;
}
const { playQueueID, playQueueSelectedMetadataItemID } = await this.plexPlayerCreateQueue(movieID, this.plex);
let url = plex.getBasicURL();
url += `/player/playback/playMedia`;
url += `?type=video`;
url += `&commandID=1`;
url += `&providerIdentifier=com.plexapp.plugins.library`;
url += `&containerKey=/playQueues/${playQueueID}`;
url += `&key=/library/metadata/${playQueueSelectedMetadataItemID}`;
url += `&offset=0`;
url += `&machineIdentifier=${await this.plex.getServerID()}`;
url += `&protocol=${this.plex.protocol}`;
url += `&address=${this.plex.ip}`;
url += `&port=${this.plex.port}`;
url += `&token=${this.plex.token}`;
url = plex.authorizeURL(url);
try { try {
const plexResponse = await axios({ const plexResponse = await axios({
method: 'post', method: 'post',
@ -19113,7 +19129,6 @@ class PlayController {
plex = entity.plex; plex = entity.plex;
} }
} }
console.log(plex.clients);
lodash.forEach(plex.clients, plexClient => { lodash.forEach(plex.clients, plexClient => {
if (lodash.isEqual(plexClient.machineIdentifier, entityName) || if (lodash.isEqual(plexClient.machineIdentifier, entityName) ||
lodash.isEqual(plexClient.product, entityName) || lodash.isEqual(plexClient.product, entityName) ||
@ -20186,13 +20201,6 @@ class PlexMeetsHomeAssistant extends HTMLElement {
} }
this.renderNewElementsIfNeeded(); this.renderNewElementsIfNeeded();
}); });
if (this.hassObj && this.plex) {
const entityConfig = JSON.parse(JSON.stringify(this.config.entity)); // todo: find a nicer solution
this.playController = new PlayController(this.hassObj, this.plex, entityConfig, this.runBefore, this.runAfter);
if (this.playController) {
await this.playController.init();
}
}
if (this.card) { if (this.card) {
this.previousPageWidth = this.card.offsetWidth; this.previousPageWidth = this.card.offsetWidth;
} }
@ -20200,6 +20208,13 @@ class PlexMeetsHomeAssistant extends HTMLElement {
this.renderPage(); this.renderPage();
try { try {
if (this.plex) { if (this.plex) {
if (this.hassObj) {
const entityConfig = JSON.parse(JSON.stringify(this.config.entity)); // todo: find a nicer solution
this.playController = new PlayController(this.hassObj, this.plex, entityConfig, this.runBefore, this.runAfter);
if (this.playController) {
await this.playController.init();
}
}
await this.plex.init(); await this.plex.init();
try { try {
const onDeck = await this.plex.getOnDeck(); const onDeck = await this.plex.getOnDeck();

@ -96,7 +96,6 @@ class PlayController {
}; };
play = async (data: Record<string, any>, instantPlay = false): Promise<void> => { play = async (data: Record<string, any>, instantPlay = false): Promise<void> => {
console.log(data);
if (_.isArray(this.runBefore)) { if (_.isArray(this.runBefore)) {
await this.hass.callService(this.runBefore[0], this.runBefore[1], {}); await this.hass.callService(this.runBefore[0], this.runBefore[1], {});
} }
@ -122,19 +121,18 @@ class PlayController {
} }
}; };
private plexPlayerCreateQueue = async (movieID: number): Promise<Record<string, number>> => { private plexPlayerCreateQueue = async (movieID: number, plex: Plex): Promise<Record<string, number>> => {
const url = `${this.plex.protocol}://${this.plex.ip}:${ 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.plex.port
}/playQueues?type=video&shuffle=0&repeat=0&continuous=1&own=1&uri=server://${await this.plex.getServerID()}/com.plexapp.plugins.library/library/metadata/${movieID}`;
const plexResponse = await axios({ const plexResponse = await axios({
method: 'post', method: 'post',
url, url,
headers: { headers: {
'X-Plex-Token': this.plex.token, 'X-Plex-Token': plex.token,
'X-Plex-Client-Identifier': 'PlexMeetsHomeAssistant' 'X-Plex-Client-Identifier': 'PlexMeetsHomeAssistant'
} }
}); });
if (plexResponse.status !== 200) { if (plexResponse.status !== 200) {
throw Error('Error reaching Plex to generate queue'); throw Error('Error reaching Plex to generate queue');
} }
@ -144,15 +142,30 @@ class PlayController {
}; };
}; };
private playViaPlexPlayer = async (entityName: string, movieID: number): Promise<void> => { private playViaPlexPlayer = async (entity: string | Record<string, any>, movieID: number): Promise<void> => {
const machineID = this.getPlexPlayerMachineIdentifier(entityName); const machineID = this.getPlexPlayerMachineIdentifier(entity);
const { playQueueID, playQueueSelectedMetadataItemID } = await this.plexPlayerCreateQueue(movieID); let { plex } = this;
if (_.isObject(entity) && !_.isNil(entity.plex)) {
plex = entity.plex;
}
const { playQueueID, playQueueSelectedMetadataItemID } = await this.plexPlayerCreateQueue(movieID, this.plex);
let url = plex.getBasicURL();
url += `/player/playback/playMedia`;
url += `?type=video`;
url += `&commandID=1`;
url += `&providerIdentifier=com.plexapp.plugins.library`;
url += `&containerKey=/playQueues/${playQueueID}`;
url += `&key=/library/metadata/${playQueueSelectedMetadataItemID}`;
url += `&offset=0`;
url += `&machineIdentifier=${await this.plex.getServerID()}`;
url += `&protocol=${this.plex.protocol}`;
url += `&address=${this.plex.ip}`;
url += `&port=${this.plex.port}`;
url += `&token=${this.plex.token}`;
url = plex.authorizeURL(url);
const url = `${this.plex.protocol}://${this.plex.ip}:${this.plex.port}/player/playback/playMedia?address=${
this.plex.ip
}&commandID=1&containerKey=/playQueues/${playQueueID}?window=100%26own=1&key=/library/metadata/${playQueueSelectedMetadataItemID}&machineIdentifier=${await this.plex.getServerID()}&offset=0&port=${
this.plex.port
}&token=${this.plex.token}&type=video&protocol=${this.plex.protocol}`;
try { try {
const plexResponse = await axios({ const plexResponse = await axios({
method: 'post', method: 'post',
@ -349,7 +362,6 @@ class PlayController {
} }
} }
console.log(plex.clients);
_.forEach(plex.clients, plexClient => { _.forEach(plex.clients, plexClient => {
if ( if (
_.isEqual(plexClient.machineIdentifier, entityName) || _.isEqual(plexClient.machineIdentifier, entityName) ||

@ -15,7 +15,7 @@ class Plex {
clients: Array<Record<string, any>> = []; clients: Array<Record<string, any>> = [];
requestTimeout = 5000; requestTimeout = 10000;
sort: string; sort: string;

@ -203,14 +203,6 @@ class PlexMeetsHomeAssistant extends HTMLElement {
this.renderNewElementsIfNeeded(); this.renderNewElementsIfNeeded();
}); });
if (this.hassObj && this.plex) {
const entityConfig: Record<string, any> = JSON.parse(JSON.stringify(this.config.entity)); // todo: find a nicer solution
this.playController = new PlayController(this.hassObj, this.plex, entityConfig, this.runBefore, this.runAfter);
if (this.playController) {
await this.playController.init();
}
}
if (this.card) { if (this.card) {
this.previousPageWidth = this.card.offsetWidth; this.previousPageWidth = this.card.offsetWidth;
} }
@ -219,6 +211,19 @@ class PlexMeetsHomeAssistant extends HTMLElement {
this.renderPage(); this.renderPage();
try { try {
if (this.plex) { if (this.plex) {
if (this.hassObj) {
const entityConfig: Record<string, any> = JSON.parse(JSON.stringify(this.config.entity)); // todo: find a nicer solution
this.playController = new PlayController(
this.hassObj,
this.plex,
entityConfig,
this.runBefore,
this.runAfter
);
if (this.playController) {
await this.playController.init();
}
}
await this.plex.init(); await this.plex.init();
try { try {

Loading…
Cancel
Save