Update: Card now gets data on its own

pull/16/head
Juraj Nyíri 4 years ago
parent 0ff809ea45
commit 44cda35b5d

@ -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"))
"""

@ -7,6 +7,7 @@ class ContentCardExample extends HTMLElement {
expandedHeight = 324; expandedHeight = 324;
movieElems = []; movieElems = [];
detailElem = undefined; detailElem = undefined;
data = {};
renderPage = (hass) => { renderPage = (hass) => {
const _this = this; const _this = this;
@ -414,8 +415,93 @@ class ContentCardExample extends HTMLElement {
this.plexProtocol = config.protocol; 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 //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) { if (this.data[this.config.libraryName] === undefined) {
throw new Error( throw new Error(

Loading…
Cancel
Save