From 44cda35b5dc5533e3f01c9c70b3e3e39ec942042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juraj=20Nyi=CC=81ri?= Date: Mon, 3 May 2021 12:15:23 +0200 Subject: [PATCH] Update: Card now gets data on its own --- tmp/tmp.py | 78 -------------------------------- www/content-card-example.js | 88 ++++++++++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 79 deletions(-) delete mode 100644 tmp/tmp.py diff --git a/tmp/tmp.py b/tmp/tmp.py deleted file mode 100644 index 3aedeb4..0000000 --- a/tmp/tmp.py +++ /dev/null @@ -1,78 +0,0 @@ -import requests -import xml.etree.ElementTree as ET -import json -import sys - -if len(sys.argv) < 3: - print("Usage: [ip] [port] [token]") - exit(0) - -ip = sys.argv[1] # Can set to 'localhost' if running on server. -port = sys.argv[2] # Port for Plex Media Server (default is 32400) -plextoken = sys.argv[3] # Change to your Plex-Token - -main_url = "http://{}:{}/?X-Plex-Token={}".format(ip, port, plextoken) - -sections_url = "http://{}:{}/library/sections?X-Plex-Token={}".format( - ip, port, plextoken -) - -serverID = ET.fromstring(requests.get(main_url).text).get("machineIdentifier") - - -results = {} -results["server_id"] = serverID - -sections = ET.fromstring(requests.get(sections_url).text).findall("Directory") -for section in sections: - sectionTitle = section.attrib.get("title") - sectionKey = section.attrib.get("key") - sectionType = section.attrib.get("type") - results[sectionTitle] = [] - print("Getting section " + sectionKey + "...") - print( - "http://{}:{}/library/sections/{}/all?X-Plex-Token={}".format( - ip, port, sectionKey, plextoken - ) - ) - titles = ET.fromstring( - requests.get( - "http://{}:{}/library/sections/{}/all?X-Plex-Token={}".format( - ip, port, sectionKey, plextoken - ) - ).text - ) - if sectionType == "movie": - titles = titles.findall("Video") - elif sectionType == "show": - titles = titles.findall("Directory") - else: - continue - for title in titles: - movieObj = { - "title": title.attrib.get("title"), - "summary": title.attrib.get("summary"), - "key": title.attrib.get("key"), - "guid": title.attrib.get("guid"), - "rating": title.attrib.get("rating"), - "audienceRating": title.attrib.get("audienceRating"), - "year": title.attrib.get("year"), - "thumb": title.attrib.get("thumb"), - "art": title.attrib.get("art"), - } - results[sectionTitle].append(movieObj) - -print("Results saved into plexData.json") -f = open("../www/plexData.json", "w") -f.write(json.dumps(results)) -f.close() - -""" -results = ET.fromstring(movies_r.text) -list = results.findall("Video") - -for movie in list: - print(movie.attrib.get("title"), "-", movie.attrib.get("year")) - - -""" diff --git a/www/content-card-example.js b/www/content-card-example.js index e0667cc..5586364 100644 --- a/www/content-card-example.js +++ b/www/content-card-example.js @@ -7,6 +7,7 @@ class ContentCardExample extends HTMLElement { expandedHeight = 324; movieElems = []; detailElem = undefined; + data = {}; renderPage = (hass) => { const _this = this; @@ -414,8 +415,93 @@ class ContentCardExample extends HTMLElement { this.plexProtocol = config.protocol; } + const serverRequest = this.getData( + this.plexProtocol + + "://" + + this.config.ip + + ":" + + this.config.port + + "/?X-Plex-Token=" + + this.config.token + ); + + const sectionsRequest = this.getData( + this.plexProtocol + + "://" + + this.config.ip + + ":" + + this.config.port + + "/library/sections?X-Plex-Token=" + + this.config.token + ); + //todo: replace this with a proper integration - this.data = JSON.parse(this.getData("/local/plexData.json")); + const parser = new DOMParser(); + const serverData = parser.parseFromString(serverRequest, "text/xml"); + const sectionsData = parser.parseFromString(sectionsRequest, "text/xml"); + const directories = sectionsData.getElementsByTagName("Directory"); + + Array.from(directories).some((directory) => { + const sectionID = directory.attributes.key.textContent; + const sectionTitle = directory.attributes.title.textContent; + const sectionType = directory.attributes.type.textContent; + this.data[sectionTitle] = []; + const sectionRequest = this.getData( + this.plexProtocol + + "://" + + this.config.ip + + ":" + + this.config.port + + "/library/sections/" + + sectionID + + "/all?X-Plex-Token=" + + this.config.token + ); + const sectionData = parser.parseFromString(sectionRequest, "text/xml"); + let titles = []; + if (sectionType == "movie") { + titles = sectionData.getElementsByTagName("Video"); + } else if (sectionType == "show") { + titles = sectionData.getElementsByTagName("Directory"); + } else { + //todo + } + Array.from(titles).some((title) => { + this.data[sectionTitle].push({ + title: title.attributes.title + ? title.attributes.title.textContent + : undefined, + summary: title.attributes.summary + ? title.attributes.summary.textContent + : undefined, + key: title.attributes.key + ? title.attributes.key.textContent + : undefined, + guid: title.attributes.guid + ? title.attributes.guid.textContent + : undefined, + rating: title.attributes.rating + ? title.attributes.rating.textContent + : undefined, + audienceRating: title.attributes.audienceRating + ? title.attributes.audienceRating.textContent + : undefined, + year: title.attributes.year + ? title.attributes.year.textContent + : undefined, + thumb: title.attributes.thumb + ? title.attributes.thumb.textContent + : undefined, + art: title.attributes.art + ? title.attributes.art.textContent + : undefined, + }); + }); + }); + + this.data.server_id = serverData.getElementsByTagName( + "MediaContainer" + )[0].attributes.machineIdentifier.textContent; if (this.data[this.config.libraryName] === undefined) { throw new Error(