Skip to content

Commit

Permalink
Update: Plex port no longer mandatory
Browse files Browse the repository at this point in the history
  • Loading branch information
JurajNyiri committed Jun 15, 2021
1 parent 40846d3 commit 72255a0
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 65 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ More images [at the end of the readme](https://github.com/JurajNyiri/PlexMeetsHo

**ip**: Enter ip address of plex server. You can also enter hostname without protocol or port.

**port**: Enter port of your plex sever.

**libraryName**: Name of the library you wish to render.

_Available special libraries:_
Expand All @@ -49,6 +47,8 @@ _Available special libraries:_
- **plexPlayer**: Name or machine ID of your plex client. Use this if you do not have devices above. See [detailed instructions](https://github.com/JurajNyiri/PlexMeetsHomeAssistant#all-other-plex-clients).
- **cast**: Entity id of your media_player configured via [Google Cast](https://www.home-assistant.io/integrations/cast/). See [detailed instructions](https://github.com/JurajNyiri/PlexMeetsHomeAssistant#google-cast).

**port**: _Optional_ Port of your plex sever.

**protocol**: _Optional_ Protocol to use for Plex. Defaults to "http".

**maxCount**: _Optional_ Maximum number of items to display in card.
Expand Down
41 changes: 22 additions & 19 deletions dist/plex-meets-homeassistant.js
Original file line number Diff line number Diff line change
Expand Up @@ -18669,7 +18669,7 @@ var axios = axios_1;

/* eslint-disable @typescript-eslint/no-explicit-any */
class Plex {
constructor(ip, port = 32400, token, protocol = 'http', sort = 'titleSort:asc') {
constructor(ip, port = false, token, protocol = 'http', sort = 'titleSort:asc') {
this.serverInfo = {};
this.clients = [];
this.requestTimeout = 5000;
Expand Down Expand Up @@ -18791,7 +18791,7 @@ class Plex {
return onDeckData;
};
this.getBasicURL = () => {
return `${this.protocol}://${this.ip}:${this.port}`;
return `${this.protocol}://${this.ip}${this.port === false ? '' : `:${this.port}`}`;
};
this.authorizeURL = (url) => {
if (!lodash.includes(url, 'X-Plex-Token')) {
Expand Down Expand Up @@ -19191,11 +19191,11 @@ const findTrailerURL = (movieData) => {
}
return foundURL;
};
const createEpisodesView = (playController, plexProtocol, ip, port, token, data) => {
const createEpisodesView = (playController, plex, data) => {
const episodeContainer = document.createElement('div');
episodeContainer.className = 'episodeContainer';
episodeContainer.style.width = `${CSS_STYLE.episodeWidth}px`;
const episodeThumbURL = `${plexProtocol}://${ip}:${port}/photo/:/transcode?width=${CSS_STYLE.episodeWidth}&height=${CSS_STYLE.episodeHeight}&minSize=1&upscale=1&url=${data.thumb}&X-Plex-Token=${token}`;
const episodeThumbURL = plex.authorizeURL(`${plex.getBasicURL()}/photo/:/transcode?width=${CSS_STYLE.episodeWidth}&height=${CSS_STYLE.episodeHeight}&minSize=1&upscale=1&url=${data.thumb}`);
const episodeElem = document.createElement('div');
episodeElem.className = 'episodeElem';
episodeElem.style.width = `${CSS_STYLE.episodeWidth}px`;
Expand Down Expand Up @@ -19915,6 +19915,7 @@ class PlexMeetsHomeAssistant extends HTMLElement {
constructor() {
super(...arguments);
this.plexProtocol = 'http';
this.plexPort = false;
this.detailsShown = false;
this.runBefore = '';
this.playTrailer = true;
Expand Down Expand Up @@ -20656,12 +20657,12 @@ class PlexMeetsHomeAssistant extends HTMLElement {
this.seasonsElem.style.top = `${top + 2000}px`;
}
lodash.forEach(seasonsData, seasonData => {
if (this.seasonsElem) {
if (this.seasonsElem && this.plex) {
this.seasonsElemHidden = false;
const seasonContainer = document.createElement('div');
seasonContainer.className = 'seasonContainer';
seasonContainer.style.width = `${CSS_STYLE.width}px`;
const thumbURL = `${this.plexProtocol}://${this.config.ip}:${this.config.port}/photo/:/transcode?width=${CSS_STYLE.expandedWidth}&height=${CSS_STYLE.expandedHeight}&minSize=1&upscale=1&url=${seasonData.thumb}&X-Plex-Token=${this.config.token}`;
const thumbURL = `${this.plex.getBasicURL()}/photo/:/transcode?width=${CSS_STYLE.expandedWidth}&height=${CSS_STYLE.expandedHeight}&minSize=1&upscale=1&url=${seasonData.thumb}&X-Plex-Token=${this.config.token}`;
const seasonElem = document.createElement('div');
seasonElem.className = 'seasonElem';
seasonElem.style.width = `${CSS_STYLE.width}px`;
Expand Down Expand Up @@ -20741,8 +20742,8 @@ class PlexMeetsHomeAssistant extends HTMLElement {
this.episodesElem.style.transition = `0s`;
this.episodesElem.style.top = `${top + 2000}px`;
lodash.forEach(episodesData, episodeData => {
if (this.episodesElem && this.playController) {
this.episodesElem.append(createEpisodesView(this.playController, this.plexProtocol, this.config.ip, this.config.port, this.config.token, episodeData));
if (this.episodesElem && this.playController && this.plex) {
this.episodesElem.append(createEpisodesView(this.playController, this.plex, episodeData));
}
});
clearInterval(this.episodesLoadTimeout);
Expand Down Expand Up @@ -20820,8 +20821,8 @@ class PlexMeetsHomeAssistant extends HTMLElement {
this.episodesElem.style.transition = `0s`;
this.episodesElem.style.top = `${top + 2000}px`;
lodash.forEach(extras, extrasData => {
if (this.episodesElem && this.playController) {
this.episodesElem.append(createEpisodesView(this.playController, this.plexProtocol, this.config.ip, this.config.port, this.config.token, extrasData));
if (this.episodesElem && this.playController && this.plex) {
this.episodesElem.append(createEpisodesView(this.playController, this.plex, extrasData));
}
});
clearInterval(this.episodesLoadTimeout);
Expand Down Expand Up @@ -20913,11 +20914,13 @@ class PlexMeetsHomeAssistant extends HTMLElement {
};
this.getMovieElement = (data, hasAdditionalData = false) => {
let thumbURL = '';
if (lodash.isEqual(data.type, 'episode')) {
thumbURL = `${this.plexProtocol}://${this.config.ip}:${this.config.port}/photo/:/transcode?width=${CSS_STYLE.expandedWidth}&height=${CSS_STYLE.expandedHeight}&minSize=1&upscale=1&url=${data.grandparentThumb}&X-Plex-Token=${this.config.token}`;
}
else {
thumbURL = `${this.plexProtocol}://${this.config.ip}:${this.config.port}/photo/:/transcode?width=${CSS_STYLE.expandedWidth}&height=${CSS_STYLE.expandedHeight}&minSize=1&upscale=1&url=${data.thumb}&X-Plex-Token=${this.config.token}`;
if (this.plex) {
if (lodash.isEqual(data.type, 'episode')) {
thumbURL = `${this.plex.getBasicURL()}/photo/:/transcode?width=${CSS_STYLE.expandedWidth}&height=${CSS_STYLE.expandedHeight}&minSize=1&upscale=1&url=${data.grandparentThumb}&X-Plex-Token=${this.config.token}`;
}
else {
thumbURL = `${this.plex.getBasicURL()}/photo/:/transcode?width=${CSS_STYLE.expandedWidth}&height=${CSS_STYLE.expandedHeight}&minSize=1&upscale=1&url=${data.thumb}&X-Plex-Token=${this.config.token}`;
}
}
const container = document.createElement('div');
container.className = 'container';
Expand Down Expand Up @@ -21031,16 +21034,16 @@ class PlexMeetsHomeAssistant extends HTMLElement {
if (!config.ip) {
throw new Error('You need to define a ip');
}
if (!config.port) {
throw new Error('You need to define a port');
}
if (!config.libraryName) {
throw new Error('You need to define a libraryName');
}
this.config = config;
if (config.protocol) {
this.plexProtocol = config.protocol;
}
if (config.port) {
this.plexPort = config.port;
}
if (config.maxCount) {
this.maxCount = config.maxCount;
}
Expand All @@ -21053,7 +21056,7 @@ class PlexMeetsHomeAssistant extends HTMLElement {
if (!lodash.isNil(config.playTrailer)) {
this.playTrailer = config.playTrailer;
}
this.plex = new Plex(this.config.ip, this.config.port, 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.getCardSize = () => {
return 3;
Expand Down
12 changes: 9 additions & 3 deletions src/modules/Plex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import _ from 'lodash';
class Plex {
ip: string;

port: number;
port: number | false;

token: string;

Expand All @@ -21,7 +21,13 @@ class Plex {

sections: Array<Record<string, any>> = [];

constructor(ip: string, port = 32400, token: string, protocol: 'http' | 'https' = 'http', sort = 'titleSort:asc') {
constructor(
ip: string,
port: number | false = false,
token: string,
protocol: 'http' | 'https' = 'http',
sort = 'titleSort:asc'
) {
this.ip = ip;
this.port = port;
this.token = token;
Expand Down Expand Up @@ -172,7 +178,7 @@ class Plex {
};

getBasicURL = (): string => {
return `${this.protocol}://${this.ip}:${this.port}`;
return `${this.protocol}://${this.ip}${this.port === false ? '' : `:${this.port}`}`;
};

authorizeURL = (url: string): string => {
Expand Down
15 changes: 6 additions & 9 deletions src/modules/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,15 @@ const findTrailerURL = (movieData: Record<string, any>): string => {
return foundURL;
};

const createEpisodesView = (
playController: PlayController,
plexProtocol: string,
ip: string,
port: string,
token: string,
data: Record<string, any>
): HTMLElement => {
const createEpisodesView = (playController: PlayController, plex: Plex, data: Record<string, any>): HTMLElement => {
const episodeContainer = document.createElement('div');
episodeContainer.className = 'episodeContainer';
episodeContainer.style.width = `${CSS_STYLE.episodeWidth}px`;
const episodeThumbURL = `${plexProtocol}://${ip}:${port}/photo/:/transcode?width=${CSS_STYLE.episodeWidth}&height=${CSS_STYLE.episodeHeight}&minSize=1&upscale=1&url=${data.thumb}&X-Plex-Token=${token}`;
const episodeThumbURL = plex.authorizeURL(
`${plex.getBasicURL()}/photo/:/transcode?width=${CSS_STYLE.episodeWidth}&height=${
CSS_STYLE.episodeHeight
}&minSize=1&upscale=1&url=${data.thumb}`
);

const episodeElem = document.createElement('div');
episodeElem.className = 'episodeElem';
Expand Down
56 changes: 24 additions & 32 deletions src/plex-meets-homeassistant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import style from './modules/style';
class PlexMeetsHomeAssistant extends HTMLElement {
plexProtocol: 'http' | 'https' = 'http';

plexPort: number | false = false;

detailsShown = false;

runBefore = '';
Expand Down Expand Up @@ -905,12 +907,14 @@ class PlexMeetsHomeAssistant extends HTMLElement {
}

_.forEach(seasonsData, seasonData => {
if (this.seasonsElem) {
if (this.seasonsElem && this.plex) {
this.seasonsElemHidden = false;
const seasonContainer = document.createElement('div');
seasonContainer.className = 'seasonContainer';
seasonContainer.style.width = `${CSS_STYLE.width}px`;
const thumbURL = `${this.plexProtocol}://${this.config.ip}:${this.config.port}/photo/:/transcode?width=${CSS_STYLE.expandedWidth}&height=${CSS_STYLE.expandedHeight}&minSize=1&upscale=1&url=${seasonData.thumb}&X-Plex-Token=${this.config.token}`;
const thumbURL = `${this.plex.getBasicURL()}/photo/:/transcode?width=${CSS_STYLE.expandedWidth}&height=${
CSS_STYLE.expandedHeight
}&minSize=1&upscale=1&url=${seasonData.thumb}&X-Plex-Token=${this.config.token}`;

const seasonElem = document.createElement('div');
seasonElem.className = 'seasonElem';
Expand Down Expand Up @@ -1004,17 +1008,8 @@ class PlexMeetsHomeAssistant extends HTMLElement {
this.episodesElem.style.transition = `0s`;
this.episodesElem.style.top = `${top + 2000}px`;
_.forEach(episodesData, episodeData => {
if (this.episodesElem && this.playController) {
this.episodesElem.append(
createEpisodesView(
this.playController,
this.plexProtocol,
this.config.ip,
this.config.port,
this.config.token,
episodeData
)
);
if (this.episodesElem && this.playController && this.plex) {
this.episodesElem.append(createEpisodesView(this.playController, this.plex, episodeData));
}
});
clearInterval(this.episodesLoadTimeout);
Expand Down Expand Up @@ -1099,17 +1094,8 @@ class PlexMeetsHomeAssistant extends HTMLElement {
this.episodesElem.style.top = `${top + 2000}px`;

_.forEach(extras, extrasData => {
if (this.episodesElem && this.playController) {
this.episodesElem.append(
createEpisodesView(
this.playController,
this.plexProtocol,
this.config.ip,
this.config.port,
this.config.token,
extrasData
)
);
if (this.episodesElem && this.playController && this.plex) {
this.episodesElem.append(createEpisodesView(this.playController, this.plex, extrasData));
}
});

Expand Down Expand Up @@ -1211,10 +1197,16 @@ class PlexMeetsHomeAssistant extends HTMLElement {

getMovieElement = (data: any, hasAdditionalData = false): HTMLDivElement => {
let thumbURL = '';
if (_.isEqual(data.type, 'episode')) {
thumbURL = `${this.plexProtocol}://${this.config.ip}:${this.config.port}/photo/:/transcode?width=${CSS_STYLE.expandedWidth}&height=${CSS_STYLE.expandedHeight}&minSize=1&upscale=1&url=${data.grandparentThumb}&X-Plex-Token=${this.config.token}`;
} else {
thumbURL = `${this.plexProtocol}://${this.config.ip}:${this.config.port}/photo/:/transcode?width=${CSS_STYLE.expandedWidth}&height=${CSS_STYLE.expandedHeight}&minSize=1&upscale=1&url=${data.thumb}&X-Plex-Token=${this.config.token}`;
if (this.plex) {
if (_.isEqual(data.type, 'episode')) {
thumbURL = `${this.plex.getBasicURL()}/photo/:/transcode?width=${CSS_STYLE.expandedWidth}&height=${
CSS_STYLE.expandedHeight
}&minSize=1&upscale=1&url=${data.grandparentThumb}&X-Plex-Token=${this.config.token}`;
} else {
thumbURL = `${this.plex.getBasicURL()}/photo/:/transcode?width=${CSS_STYLE.expandedWidth}&height=${
CSS_STYLE.expandedHeight
}&minSize=1&upscale=1&url=${data.thumb}&X-Plex-Token=${this.config.token}`;
}
}

const container = document.createElement('div');
Expand Down Expand Up @@ -1345,16 +1337,16 @@ class PlexMeetsHomeAssistant extends HTMLElement {
if (!config.ip) {
throw new Error('You need to define a ip');
}
if (!config.port) {
throw new Error('You need to define a port');
}
if (!config.libraryName) {
throw new Error('You need to define a libraryName');
}
this.config = config;
if (config.protocol) {
this.plexProtocol = config.protocol;
}
if (config.port) {
this.plexPort = config.port;
}
if (config.maxCount) {
this.maxCount = config.maxCount;
}
Expand All @@ -1368,7 +1360,7 @@ class PlexMeetsHomeAssistant extends HTMLElement {
this.playTrailer = config.playTrailer;
}

this.plex = new Plex(this.config.ip, this.config.port, 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);
};

getCardSize = (): number => {
Expand Down

0 comments on commit 72255a0

Please sign in to comment.