WIP Refactor

pull/16/head
Juraj Nyíri 4 years ago
parent d123fdb3cf
commit 2ab5c840c7

@ -18662,6 +18662,21 @@ class Plex {
const url = `${this.protocol}://${this.ip}:${this.port}/library/sections?X-Plex-Token=${this.token}`; const url = `${this.protocol}://${this.ip}:${this.port}/library/sections?X-Plex-Token=${this.token}`;
return (await axios.get(url)).data.MediaContainer.Directory; return (await axios.get(url)).data.MediaContainer.Directory;
}; };
this.getSectionsData = async () => {
const sections = await this.getSections();
const sectionsRequests = [];
lodash.forEach(sections, section => {
sectionsRequests.push(axios.get(`${this.protocol}://${this.ip}:${this.port}/library/sections/${section.key}/all?X-Plex-Token=${this.token}`));
});
return this.exportSectionsData(await Promise.all(sectionsRequests));
};
this.exportSectionsData = (sectionsData) => {
const processedData = [];
lodash.forEach(sectionsData, sectionData => {
processedData.push(sectionData.data.MediaContainer);
});
return processedData;
};
this.ip = ip; this.ip = ip;
this.port = port; this.port = port;
this.token = token; this.token = token;
@ -18669,8 +18684,10 @@ class Plex {
} }
} }
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const escapeHtml = (unsafe) => { const escapeHtml = (unsafe) => {
return unsafe return unsafe
.toString()
.replace(/&/g, '&') .replace(/&/g, '&')
.replace(/</g, '&lt;') .replace(/</g, '&lt;')
.replace(/>/g, '&gt;') .replace(/>/g, '&gt;')
@ -18755,6 +18772,7 @@ class PlexMeetsHomeAssistant extends HTMLElement {
this.loadCustomStyles(); this.loadCustomStyles();
}; };
this.render = (hass) => { this.render = (hass) => {
console.log('render');
this.previousPositions = []; this.previousPositions = [];
// todo: find a better way to detect resize... // todo: find a better way to detect resize...
// todo: uncomment // todo: uncomment
@ -18785,29 +18803,29 @@ class PlexMeetsHomeAssistant extends HTMLElement {
this.renderPage(hass); this.renderPage(hass);
}; };
this.loadInitialData = async (hass) => { this.loadInitialData = async (hass) => {
const plex = new Plex(this.config.ip, this.config.port, this.config.token, this.plexProtocol);
const [plexInfo, plexSections] = await Promise.all([plex.getServerInfo(), plex.getSections()]);
console.log(plexInfo);
console.log(plexSections);
this.loading = true; this.loading = true;
this.renderPage(hass); this.renderPage(hass);
/* const plex = new Plex(this.config.ip, this.config.port, this.config.token, this.plexProtocol);
const parser = new DOMParser(); try {
const sectionsDetails: Array<any> = []; const [plexInfo, plexSections] = await Promise.all([plex.getServerInfo(), plex.getSectionsData()]);
Promise.all([serverRequest, sectionsRequest]) // eslint-disable-next-line @typescript-eslint/camelcase
.then((data: Array<any>) => { this.data.serverID = plexInfo;
const serverData = parser.parseFromString(data[0], 'text/xml'); lodash.forEach(plexSections, section => {
const sectionsData = parser.parseFromString(data[1], 'text/xml'); this.data[section.title1] = section.Metadata;
const directories = sectionsData.getElementsByTagName('Directory');
// eslint-disable-next-line array-callback-return
Array.from(directories).some(directory => {
const sectionID = (directory.attributes as Record<string, any>).key.textContent;
const url = `${this.plexProtocol}://${this.config.ip}:${this.config.port}/library/sections/${sectionID}/all?X-Plex-Token=${this.config.token}`;
sectionsDetails.push(this.getData(url));
}); });
if (this.data[this.config.libraryName] === undefined) {
this.error = `Library name ${this.config.libraryName} does not exist.`;
}
this.loading = false;
this.render(hass);
}
catch (err) {
// todo: proper timeout here
this.error = `Plex server did not respond.`;
this.renderPage(hass);
}
/*
Promise.all(sectionsDetails)
// eslint-disable-next-line no-shadow // eslint-disable-next-line no-shadow
.then(sectionsData => { .then(sectionsData => {
// eslint-disable-next-line array-callback-return // eslint-disable-next-line array-callback-return
@ -18869,13 +18887,13 @@ class PlexMeetsHomeAssistant extends HTMLElement {
// todo: run also on resize // todo: run also on resize
this.calculatePositions = () => { this.calculatePositions = () => {
// todo: figure out why loop is needed here and do it properly // todo: figure out why loop is needed here and do it properly
/*
const setLeftOffsetsInterval = setInterval(() => { const setLeftOffsetsInterval = setInterval(() => {
this.movieElems = this.getElementsByClassName('movieElem'); this.movieElems = this.getElementsByClassName('movieElem');
for (let i = 0; i < this.movieElems.length; i + 1) { for (let i = 0; i < this.movieElems.length; i + 1) {
if (this.movieElems[i].offsetLeft === 0) { if (this.movieElems[i].offsetLeft === 0) {
break; break;
} } else {
else {
clearInterval(setLeftOffsetsInterval); clearInterval(setLeftOffsetsInterval);
} }
this.movieElems[i].style.left = `${this.movieElems[i].offsetLeft}px`; this.movieElems[i].style.left = `${this.movieElems[i].offsetLeft}px`;
@ -18884,6 +18902,7 @@ class PlexMeetsHomeAssistant extends HTMLElement {
this.movieElems[i].dataset.top = this.movieElems[i].offsetTop; this.movieElems[i].dataset.top = this.movieElems[i].offsetTop;
} }
}, 10); }, 10);
*/
}; };
this.minimizeAll = () => { this.minimizeAll = () => {
for (let i = 0; i < this.movieElems.length; i + 1) { for (let i = 0; i < this.movieElems.length; i + 1) {

@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/no-explicit-any */ /* eslint-disable @typescript-eslint/no-explicit-any */
import axios from 'axios'; import axios from 'axios';
import _ from 'lodash';
class Plex { class Plex {
ip: string; ip: string;
@ -26,6 +27,27 @@ class Plex {
const url = `${this.protocol}://${this.ip}:${this.port}/library/sections?X-Plex-Token=${this.token}`; const url = `${this.protocol}://${this.ip}:${this.port}/library/sections?X-Plex-Token=${this.token}`;
return (await axios.get(url)).data.MediaContainer.Directory; return (await axios.get(url)).data.MediaContainer.Directory;
}; };
getSectionsData = async (): Promise<any> => {
const sections = await this.getSections();
const sectionsRequests: Array<Promise<any>> = [];
_.forEach(sections, section => {
sectionsRequests.push(
axios.get(
`${this.protocol}://${this.ip}:${this.port}/library/sections/${section.key}/all?X-Plex-Token=${this.token}`
)
);
});
return this.exportSectionsData(await Promise.all(sectionsRequests));
};
private exportSectionsData = (sectionsData: Array<any>): Array<any> => {
const processedData: Array<any> = [];
_.forEach(sectionsData, sectionData => {
processedData.push(sectionData.data.MediaContainer);
});
return processedData;
};
} }
export default Plex; export default Plex;

@ -118,6 +118,7 @@ class PlexMeetsHomeAssistant extends HTMLElement {
} }
render = (hass: HomeAssistant): void => { render = (hass: HomeAssistant): void => {
console.log('render');
this.previousPositions = []; this.previousPositions = [];
// todo: find a better way to detect resize... // todo: find a better way to detect resize...
@ -150,32 +151,32 @@ class PlexMeetsHomeAssistant extends HTMLElement {
}; };
loadInitialData = async (hass: HomeAssistant): Promise<void> => { loadInitialData = async (hass: HomeAssistant): Promise<void> => {
const plex = new Plex(this.config.ip, this.config.port, this.config.token, this.plexProtocol); this.loading = true;
this.renderPage(hass);
const [plexInfo, plexSections] = await Promise.all([plex.getServerInfo(), plex.getSections()]); 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;
});
console.log(plexInfo); if (this.data[this.config.libraryName] === undefined) {
console.log(plexSections); this.error = `Library name ${this.config.libraryName} does not exist.`;
}
this.loading = true; this.loading = false;
this.render(hass);
} catch (err) {
// todo: proper timeout here
this.error = `Plex server did not respond.`;
this.renderPage(hass); this.renderPage(hass);
/* }
const parser = new DOMParser();
const sectionsDetails: Array<any> = [];
Promise.all([serverRequest, sectionsRequest])
.then((data: Array<any>) => {
const serverData = parser.parseFromString(data[0], 'text/xml');
const sectionsData = parser.parseFromString(data[1], 'text/xml');
const directories = sectionsData.getElementsByTagName('Directory');
// eslint-disable-next-line array-callback-return /*
Array.from(directories).some(directory => {
const sectionID = (directory.attributes as Record<string, any>).key.textContent;
const url = `${this.plexProtocol}://${this.config.ip}:${this.config.port}/library/sections/${sectionID}/all?X-Plex-Token=${this.config.token}`;
sectionsDetails.push(this.getData(url));
});
Promise.all(sectionsDetails)
// eslint-disable-next-line no-shadow // eslint-disable-next-line no-shadow
.then(sectionsData => { .then(sectionsData => {
// eslint-disable-next-line array-callback-return // eslint-disable-next-line array-callback-return
@ -238,6 +239,7 @@ class PlexMeetsHomeAssistant extends HTMLElement {
// todo: run also on resize // todo: run also on resize
calculatePositions = () => { calculatePositions = () => {
// todo: figure out why loop is needed here and do it properly // todo: figure out why loop is needed here and do it properly
/*
const setLeftOffsetsInterval = setInterval(() => { const setLeftOffsetsInterval = setInterval(() => {
this.movieElems = this.getElementsByClassName('movieElem'); this.movieElems = this.getElementsByClassName('movieElem');
for (let i = 0; i < this.movieElems.length; i + 1) { for (let i = 0; i < this.movieElems.length; i + 1) {
@ -252,6 +254,7 @@ class PlexMeetsHomeAssistant extends HTMLElement {
this.movieElems[i].dataset.top = this.movieElems[i].offsetTop; this.movieElems[i].dataset.top = this.movieElems[i].offsetTop;
} }
}, 10); }, 10);
*/
}; };
minimizeAll = () => { minimizeAll = () => {

@ -1,5 +1,7 @@
const escapeHtml = (unsafe: string): string => { // eslint-disable-next-line @typescript-eslint/no-explicit-any
const escapeHtml = (unsafe: any): string => {
return unsafe return unsafe
.toString()
.replace(/&/g, '&amp;') .replace(/&/g, '&amp;')
.replace(/</g, '&lt;') .replace(/</g, '&lt;')
.replace(/>/g, '&gt;') .replace(/>/g, '&gt;')

Loading…
Cancel
Save