From 2ce64fe19034b5169559be2c5409d4bc6358a404 Mon Sep 17 00:00:00 2001 From: Phasetime <87231024+Phasetime@users.noreply.github.com> Date: Sun, 15 Aug 2021 14:56:01 +0200 Subject: [PATCH 01/15] added scraper for .nfo files from kodi --- scrapers/kodi.py | 74 +++++++++++++++++++++++++++++++++++++++++++++++ scrapers/kodi.yml | 10 +++++++ 2 files changed, 84 insertions(+) create mode 100644 scrapers/kodi.py create mode 100644 scrapers/kodi.yml diff --git a/scrapers/kodi.py b/scrapers/kodi.py new file mode 100644 index 000000000..c2a4ae29d --- /dev/null +++ b/scrapers/kodi.py @@ -0,0 +1,74 @@ +import os +import sys +import json +import sqlite3 +import xml.etree.ElementTree as ET +''' +This script parses kodi nfo files for metadata. The .nfo file must be in the same directory as the video file and must be named exactly alike. +''' + +def query_xml(path, title): + tree=ET.parse(path) + # print(tree.find('title').text, file=sys.stderr) + if title == tree.find('title').text: + debug('Exact match found for ' + title) + else: + ebug('No exact match found for ' + title + ". Matching with " + tree.find('title').text + "!") + + # Extract matadata from xml + res={'title':title} + if tree.find('title') != None: + res['title'] = tree.find('title').text + if tree.find('plot') != None: + res['details'] = tree.find('plot').text + if tree.find('releasedate') != None: + res['date'] = tree.find('releasedate').text + if tree.find('tag') != None: + res['tags']=[{"name":x.text} for x in tree.findall('tag')] + if tree.find('genre') != None: + if res['tags'] is not None: + res['tags'] += [{"name":x.text} for x in tree.findall('genre')] + else: + res['tags'] = [{"name":x.text} for x in tree.findall('genre')] + + print(json.dumps(res)) + exit(0) + +def debug(s): + print(s, file=sys.stderr) + +# Would be nicer with Stash API instead of direct SQlite access +def get_file_path(scene_id): + db_file = "../stash-go.sqlite" + + con = sqlite3.connect(db_file) + cur = con.cursor() + for row in cur.execute("SELECT * FROM scenes where id = " + str(scene_id) + ";"): + #debug_print(row) + filepath = row[1] + con.close() + return filepath + +def lookup_xml(path, title): + # FEATURE: Add more path variantions here to allow for /metadata/ subfolder + if os.path.isfile(path): + query_xml(path, title) + else: + debug("No file found at" + path) + +if sys.argv[1] == "query": + fragment = json.loads(sys.stdin.read()) + + # Assume that .nfo is named exactly like the video file and is at the same location + # WORKAROUND: Read file name from db until filename is given in the fragment + videoFilePath = get_file_path(fragment['id']) + + # Reconstruct file name for .nfo + temp = videoFilePath.split('.') + temp[-1] = 'nfo' + nfoFilePath = '.'.join(temp) + + lookup_xml(nfoFilePath, fragment['title']) + print(json.dumps(fragment)) + +# Last Updated August 15, 2021 diff --git a/scrapers/kodi.yml b/scrapers/kodi.yml new file mode 100644 index 000000000..bde7f59f5 --- /dev/null +++ b/scrapers/kodi.yml @@ -0,0 +1,10 @@ +name: "Kodi XML" +sceneByFragment: + action: script + script: + - python + # use python3 instead if needed + - kodi.py + - query + +# Last Updated February 04, 2021 From be41f82a9b53b9cd265027700936b55d9207bb5a Mon Sep 17 00:00:00 2001 From: Phasetime <87231024+Phasetime@users.noreply.github.com> Date: Sun, 15 Aug 2021 15:18:43 +0200 Subject: [PATCH 02/15] remove redundant lookup_xml(), last updated in py --- scrapers/kodi.py | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/scrapers/kodi.py b/scrapers/kodi.py index c2a4ae29d..1a8076e36 100644 --- a/scrapers/kodi.py +++ b/scrapers/kodi.py @@ -31,8 +31,7 @@ def query_xml(path, title): else: res['tags'] = [{"name":x.text} for x in tree.findall('genre')] - print(json.dumps(res)) - exit(0) + return res def debug(s): print(s, file=sys.stderr) @@ -48,13 +47,6 @@ def get_file_path(scene_id): filepath = row[1] con.close() return filepath - -def lookup_xml(path, title): - # FEATURE: Add more path variantions here to allow for /metadata/ subfolder - if os.path.isfile(path): - query_xml(path, title) - else: - debug("No file found at" + path) if sys.argv[1] == "query": fragment = json.loads(sys.stdin.read()) @@ -68,7 +60,10 @@ def lookup_xml(path, title): temp[-1] = 'nfo' nfoFilePath = '.'.join(temp) - lookup_xml(nfoFilePath, fragment['title']) - print(json.dumps(fragment)) - -# Last Updated August 15, 2021 + if os.path.isfile(nfoFilePath): + res = query_xml(nfoFilePath, fragment['title']) + print(json.dumps(res)) + else: + debug("No file found at" + nfoFilePath) + + exit(0) From 6fd808dc72b951e1a0e48b816502b91cdd5d85fe Mon Sep 17 00:00:00 2001 From: Phasetime <87231024+Phasetime@users.noreply.github.com> Date: Sun, 15 Aug 2021 15:19:08 +0200 Subject: [PATCH 03/15] update last updated in yml --- scrapers/kodi.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scrapers/kodi.yml b/scrapers/kodi.yml index bde7f59f5..7108550d7 100644 --- a/scrapers/kodi.yml +++ b/scrapers/kodi.yml @@ -7,4 +7,4 @@ sceneByFragment: - kodi.py - query -# Last Updated February 04, 2021 +# Last Updated August 15, 2021 From 10742b63aadeb9ad77ca92fef9c4e042677788d6 Mon Sep 17 00:00:00 2001 From: Phasetime <87231024+Phasetime@users.noreply.github.com> Date: Sun, 15 Aug 2021 16:41:01 +0200 Subject: [PATCH 04/15] added performer, art, studio scraping --- scrapers/kodi.py | 83 ++++++++++++++++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 24 deletions(-) diff --git a/scrapers/kodi.py b/scrapers/kodi.py index 1a8076e36..a300445f7 100644 --- a/scrapers/kodi.py +++ b/scrapers/kodi.py @@ -2,34 +2,61 @@ import sys import json import sqlite3 +import mimetypes +import base64 import xml.etree.ElementTree as ET -''' +""" This script parses kodi nfo files for metadata. The .nfo file must be in the same directory as the video file and must be named exactly alike. -''' +""" + +# If you want to ingest image data from the nfo you may need to rewrite the path stored in the file. Especially when using a docker container. +rewriteBasePath = True +basePathBefore = 'Z:' +basePathAfter = "/data" def query_xml(path, title): tree=ET.parse(path) - # print(tree.find('title').text, file=sys.stderr) - if title == tree.find('title').text: - debug('Exact match found for ' + title) + # print(tree.find("title").text, file=sys.stderr) + if title == tree.find("title").text: + debug("Exact match found for " + title) else: - ebug('No exact match found for ' + title + ". Matching with " + tree.find('title').text + "!") + debug("No exact match found for " + title + ". Matching with " + tree.find("title").text + "!") # Extract matadata from xml - res={'title':title} - if tree.find('title') != None: - res['title'] = tree.find('title').text - if tree.find('plot') != None: - res['details'] = tree.find('plot').text - if tree.find('releasedate') != None: - res['date'] = tree.find('releasedate').text - if tree.find('tag') != None: - res['tags']=[{"name":x.text} for x in tree.findall('tag')] - if tree.find('genre') != None: - if res['tags'] is not None: - res['tags'] += [{"name":x.text} for x in tree.findall('genre')] + res={"title":title} + if tree.find("title") != None: + res["title"] = tree.find("title").text + if tree.find("plot") != None: + res["details"] = tree.find("plot").text + if tree.find("releasedate") != None: + res["date"] = tree.find("releasedate").text + if tree.find("tag") != None: + res["tags"]=[{"name":x.text} for x in tree.findall("tag")] + if tree.find("genre") != None: + if res["tags"] is not None: + res["tags"] += [{"name":x.text} for x in tree.findall("genre")] + else: + res["tags"] = [{"name":x.text} for x in tree.findall("genre")] + if tree.find("actor") != None: + res["performers"] = [] + for actor in tree.findall("actor"): + if actor.find("type").text == "Actor": + res["performers"].append({"name": actor.find("name").text}) + if tree.find("studio") != None: + res["studio"] = {"name":tree.find("studio").text} + + posterElem = tree.find("art").find("poster") + if posterElem.text != None: + if not rewriteBasePath and os.path.isfile(posterElem.text): + res["image"] = make_image_data_url(posterElem.text) + elif rewriteBasePath: + rewrittenPath = posterElem.text.replace(basePathBefore, basePathAfter).replace("\\", "/") + if os.path.isfile(rewrittenPath): + res["image"] = make_image_data_url(rewrittenPath) + else: + debug("Can't find image: " + posterElem.text.replace(basePathBefore, basePathAfter) + ". Is the base path correct?") else: - res['tags'] = [{"name":x.text} for x in tree.findall('genre')] + debug("Can't find image: " + posterElem.text + ". Are you using a docker container? Maybe you need to change the base path in the script file.") return res @@ -47,21 +74,29 @@ def get_file_path(scene_id): filepath = row[1] con.close() return filepath + +def make_image_data_url(image_path): + # type: (str,) -> str + mime, _ = mimetypes.guess_type(image_path) + with open(image_path, 'rb') as img: + encoded = base64.b64encode(img.read()).decode() + return 'data:{0};base64,{1}'.format(mime, encoded) if sys.argv[1] == "query": fragment = json.loads(sys.stdin.read()) # Assume that .nfo is named exactly like the video file and is at the same location # WORKAROUND: Read file name from db until filename is given in the fragment - videoFilePath = get_file_path(fragment['id']) + videoFilePath = get_file_path(fragment["id"]) # Reconstruct file name for .nfo - temp = videoFilePath.split('.') - temp[-1] = 'nfo' - nfoFilePath = '.'.join(temp) + temp = videoFilePath.split(".") + temp[-1] = "nfo" + nfoFilePath = ".".join(temp) if os.path.isfile(nfoFilePath): - res = query_xml(nfoFilePath, fragment['title']) + res = query_xml(nfoFilePath, fragment["title"]) + debug(res) print(json.dumps(res)) else: debug("No file found at" + nfoFilePath) From 820751e7f11399019a61dd3f6ee1dc82cd18ddca Mon Sep 17 00:00:00 2001 From: Phasetime <87231024+Phasetime@users.noreply.github.com> Date: Sun, 15 Aug 2021 16:57:18 +0200 Subject: [PATCH 05/15] updated info about rewriteBasePath --- scrapers/kodi.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/scrapers/kodi.py b/scrapers/kodi.py index a300445f7..77517b21d 100644 --- a/scrapers/kodi.py +++ b/scrapers/kodi.py @@ -9,8 +9,9 @@ This script parses kodi nfo files for metadata. The .nfo file must be in the same directory as the video file and must be named exactly alike. """ -# If you want to ingest image data from the nfo you may need to rewrite the path stored in the file. Especially when using a docker container. -rewriteBasePath = True +# If you want to ingest image data from the .nfo the path to these files may need to be rewritten. Especially when using a docker container. +rewriteBasePath = False +# Example: Z:\Videos\Studio_XXX\example_cover.jpg -> /data/Studio_XXX/example_cover.jpg basePathBefore = 'Z:' basePathAfter = "/data" From 7b608de86191a33252e2bc4a3dd3bec89ed6f24b Mon Sep 17 00:00:00 2001 From: Phasetime <87231024+Phasetime@users.noreply.github.com> Date: Sun, 15 Aug 2021 17:00:34 +0200 Subject: [PATCH 06/15] updated info about rewriteBasePath #2 --- scrapers/kodi.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scrapers/kodi.py b/scrapers/kodi.py index 77517b21d..d453a520e 100644 --- a/scrapers/kodi.py +++ b/scrapers/kodi.py @@ -9,10 +9,10 @@ This script parses kodi nfo files for metadata. The .nfo file must be in the same directory as the video file and must be named exactly alike. """ -# If you want to ingest image data from the .nfo the path to these files may need to be rewritten. Especially when using a docker container. +# If you want to ingest image files from the .nfo the path to these files may need to be rewritten. Especially when using a docker container. rewriteBasePath = False # Example: Z:\Videos\Studio_XXX\example_cover.jpg -> /data/Studio_XXX/example_cover.jpg -basePathBefore = 'Z:' +basePathBefore = 'Z:\Videos' basePathAfter = "/data" def query_xml(path, title): From 31c6d377ca397a05b53e71a16918bba6a0ed8069 Mon Sep 17 00:00:00 2001 From: Phasetime <87231024+Phasetime@users.noreply.github.com> Date: Mon, 16 Aug 2021 01:57:34 +0200 Subject: [PATCH 07/15] fix error when no art is included --- scrapers/kodi.py | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/scrapers/kodi.py b/scrapers/kodi.py index d453a520e..e0968b8b7 100644 --- a/scrapers/kodi.py +++ b/scrapers/kodi.py @@ -46,18 +46,20 @@ def query_xml(path, title): if tree.find("studio") != None: res["studio"] = {"name":tree.find("studio").text} - posterElem = tree.find("art").find("poster") - if posterElem.text != None: - if not rewriteBasePath and os.path.isfile(posterElem.text): - res["image"] = make_image_data_url(posterElem.text) - elif rewriteBasePath: - rewrittenPath = posterElem.text.replace(basePathBefore, basePathAfter).replace("\\", "/") - if os.path.isfile(rewrittenPath): - res["image"] = make_image_data_url(rewrittenPath) - else: - debug("Can't find image: " + posterElem.text.replace(basePathBefore, basePathAfter) + ". Is the base path correct?") - else: - debug("Can't find image: " + posterElem.text + ". Are you using a docker container? Maybe you need to change the base path in the script file.") + if tree.find("art") != None: + if tree.find("art").find("poster") != None: + posterElem = tree.find("art").find("poster") + if posterElem.text != None: + if not rewriteBasePath and os.path.isfile(posterElem.text): + res["image"] = make_image_data_url(posterElem.text) + elif rewriteBasePath: + rewrittenPath = posterElem.text.replace(basePathBefore, basePathAfter).replace("\\", "/") + if os.path.isfile(rewrittenPath): + res["image"] = make_image_data_url(rewrittenPath) + else: + debug("Can't find image: " + posterElem.text.replace(basePathBefore, basePathAfter) + ". Is the base path correct?") + else: + debug("Can't find image: " + posterElem.text + ". Are you using a docker container? Maybe you need to change the base path in the script file.") return res From 015a5fbbc8c5f914b33c51b50f60ee81666706e4 Mon Sep 17 00:00:00 2001 From: Phasetime <87231024+Phasetime@users.noreply.github.com> Date: Mon, 16 Aug 2021 03:46:22 +0200 Subject: [PATCH 08/15] make the scraper always return a result --- scrapers/kodi.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/scrapers/kodi.py b/scrapers/kodi.py index e0968b8b7..7846c3798 100644 --- a/scrapers/kodi.py +++ b/scrapers/kodi.py @@ -87,7 +87,7 @@ def make_image_data_url(image_path): if sys.argv[1] == "query": fragment = json.loads(sys.stdin.read()) - + res = {"title": fragment["title"]} # Assume that .nfo is named exactly like the video file and is at the same location # WORKAROUND: Read file name from db until filename is given in the fragment videoFilePath = get_file_path(fragment["id"]) @@ -99,9 +99,8 @@ def make_image_data_url(image_path): if os.path.isfile(nfoFilePath): res = query_xml(nfoFilePath, fragment["title"]) - debug(res) - print(json.dumps(res)) else: debug("No file found at" + nfoFilePath) - + + print(json.dumps(res)) exit(0) From 2c50697ded749b522994bafb8d360ae1cb306345 Mon Sep 17 00:00:00 2001 From: Phasetime <87231024+Phasetime@users.noreply.github.com> Date: Mon, 16 Aug 2021 03:57:11 +0200 Subject: [PATCH 09/15] omitted debugging statements for production --- scrapers/kodi.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/scrapers/kodi.py b/scrapers/kodi.py index 7846c3798..56f645bf8 100644 --- a/scrapers/kodi.py +++ b/scrapers/kodi.py @@ -8,6 +8,8 @@ """ This script parses kodi nfo files for metadata. The .nfo file must be in the same directory as the video file and must be named exactly alike. """ +debug = False + # If you want to ingest image files from the .nfo the path to these files may need to be rewritten. Especially when using a docker container. rewriteBasePath = False @@ -64,7 +66,7 @@ def query_xml(path, title): return res def debug(s): - print(s, file=sys.stderr) + if debug: print(s, file=sys.stderr) # Would be nicer with Stash API instead of direct SQlite access def get_file_path(scene_id): From d99e45cc07bd0c0de2d97a9bfa62bf0908049b82 Mon Sep 17 00:00:00 2001 From: Phasetime <87231024+Phasetime@users.noreply.github.com> Date: Sun, 26 Sep 2021 00:07:00 +0200 Subject: [PATCH 10/15] fix keyError 'tags' --- scrapers/kodi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scrapers/kodi.py b/scrapers/kodi.py index 56f645bf8..51a69328a 100644 --- a/scrapers/kodi.py +++ b/scrapers/kodi.py @@ -36,7 +36,7 @@ def query_xml(path, title): if tree.find("tag") != None: res["tags"]=[{"name":x.text} for x in tree.findall("tag")] if tree.find("genre") != None: - if res["tags"] is not None: + if "tags" in res: res["tags"] += [{"name":x.text} for x in tree.findall("genre")] else: res["tags"] = [{"name":x.text} for x in tree.findall("genre")] From b9ce84dd8da0901c51985209a63068783258ce77 Mon Sep 17 00:00:00 2001 From: Phasetime <87231024+Phasetime@users.noreply.github.com> Date: Sat, 2 Oct 2021 22:57:05 +0200 Subject: [PATCH 11/15] fix actor type error --- scrapers/kodi.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/scrapers/kodi.py b/scrapers/kodi.py index 51a69328a..728538f06 100644 --- a/scrapers/kodi.py +++ b/scrapers/kodi.py @@ -43,8 +43,13 @@ def query_xml(path, title): if tree.find("actor") != None: res["performers"] = [] for actor in tree.findall("actor"): - if actor.find("type").text == "Actor": + if actor.find("type") != None: + if actor.find("type").text == "Actor": + res["performers"].append({"name": actor.find("name").text}) + else if actor.find("name") != None: res["performers"].append({"name": actor.find("name").text}) + else: + res["performers"].append({"name": actor.text}) if tree.find("studio") != None: res["studio"] = {"name":tree.find("studio").text} From 542a519c5bb7c04946b57fae3282da49d8fc8f4a Mon Sep 17 00:00:00 2001 From: Phasetime <87231024+Phasetime@users.noreply.github.com> Date: Sat, 8 Jan 2022 10:06:49 +0100 Subject: [PATCH 12/15] Control flow error --- scrapers/kodi.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scrapers/kodi.py b/scrapers/kodi.py index 728538f06..fa8b51132 100644 --- a/scrapers/kodi.py +++ b/scrapers/kodi.py @@ -46,7 +46,7 @@ def query_xml(path, title): if actor.find("type") != None: if actor.find("type").text == "Actor": res["performers"].append({"name": actor.find("name").text}) - else if actor.find("name") != None: + elif actor.find("name") != None: res["performers"].append({"name": actor.find("name").text}) else: res["performers"].append({"name": actor.text}) From 544f3ea692d370248156c137930b91650e1ed92b Mon Sep 17 00:00:00 2001 From: Phasetime <87231024+Phasetime@users.noreply.github.com> Date: Sat, 8 Jan 2022 11:23:11 +0100 Subject: [PATCH 13/15] Added graphQL support instead of direct SQLite access + logging --- scrapers/kodi.py | 43 ++++++++++++++++--------------------------- 1 file changed, 16 insertions(+), 27 deletions(-) diff --git a/scrapers/kodi.py b/scrapers/kodi.py index fa8b51132..eae0531bd 100644 --- a/scrapers/kodi.py +++ b/scrapers/kodi.py @@ -1,15 +1,14 @@ import os import sys import json -import sqlite3 import mimetypes import base64 import xml.etree.ElementTree as ET +import py_common.graphql as graphql +import py_common.log as log """ This script parses kodi nfo files for metadata. The .nfo file must be in the same directory as the video file and must be named exactly alike. """ -debug = False - # If you want to ingest image files from the .nfo the path to these files may need to be rewritten. Especially when using a docker container. rewriteBasePath = False @@ -21,9 +20,9 @@ def query_xml(path, title): tree=ET.parse(path) # print(tree.find("title").text, file=sys.stderr) if title == tree.find("title").text: - debug("Exact match found for " + title) + log.info("Exact match found for " + title) else: - debug("No exact match found for " + title + ". Matching with " + tree.find("title").text + "!") + log.info("No exact match found for " + title + ". Matching with " + tree.find("title").text + "!") # Extract matadata from xml res={"title":title} @@ -64,40 +63,30 @@ def query_xml(path, title): if os.path.isfile(rewrittenPath): res["image"] = make_image_data_url(rewrittenPath) else: - debug("Can't find image: " + posterElem.text.replace(basePathBefore, basePathAfter) + ". Is the base path correct?") + log.warning("Can't find image: " + posterElem.text.replace(basePathBefore, basePathAfter) + ". Is the base path correct?") else: - debug("Can't find image: " + posterElem.text + ". Are you using a docker container? Maybe you need to change the base path in the script file.") + log.warning("Can't find image: " + posterElem.text + ". Are you using a docker container? Maybe you need to change the base path in the script file.") return res -def debug(s): - if debug: print(s, file=sys.stderr) - -# Would be nicer with Stash API instead of direct SQlite access -def get_file_path(scene_id): - db_file = "../stash-go.sqlite" - - con = sqlite3.connect(db_file) - cur = con.cursor() - for row in cur.execute("SELECT * FROM scenes where id = " + str(scene_id) + ";"): - #debug_print(row) - filepath = row[1] - con.close() - return filepath - def make_image_data_url(image_path): # type: (str,) -> str mime, _ = mimetypes.guess_type(image_path) with open(image_path, 'rb') as img: encoded = base64.b64encode(img.read()).decode() return 'data:{0};base64,{1}'.format(mime, encoded) - + if sys.argv[1] == "query": fragment = json.loads(sys.stdin.read()) res = {"title": fragment["title"]} - # Assume that .nfo is named exactly like the video file and is at the same location - # WORKAROUND: Read file name from db until filename is given in the fragment - videoFilePath = get_file_path(fragment["id"]) + + # Assume that .nfo/.xml is named exactly alike the video file and is at the same location + # Query graphQL for the file path + graphqlResponse = graphql.getScene(fragment["id"]) + if graphqlResponse: + videoFilePath = graphqlResponse["path"] + else: + exit(0) # Reconstruct file name for .nfo temp = videoFilePath.split(".") @@ -107,7 +96,7 @@ def make_image_data_url(image_path): if os.path.isfile(nfoFilePath): res = query_xml(nfoFilePath, fragment["title"]) else: - debug("No file found at" + nfoFilePath) + log.info("No file found at" + nfoFilePath) print(json.dumps(res)) exit(0) From 26c39895f5245aa1a68dec801882be1394dbbd5f Mon Sep 17 00:00:00 2001 From: Phasetime <87231024+Phasetime@users.noreply.github.com> Date: Sat, 8 Jan 2022 12:00:21 +0100 Subject: [PATCH 14/15] worked in #827 cleanup, pathlib --- scrapers/kodi.py | 63 +++++++++++++++++++++++++++++------------------- 1 file changed, 38 insertions(+), 25 deletions(-) diff --git a/scrapers/kodi.py b/scrapers/kodi.py index eae0531bd..e5541223a 100644 --- a/scrapers/kodi.py +++ b/scrapers/kodi.py @@ -1,9 +1,12 @@ -import os import sys -import json +import pathlib + import mimetypes import base64 + +import json import xml.etree.ElementTree as ET + import py_common.graphql as graphql import py_common.log as log """ @@ -17,21 +20,29 @@ basePathAfter = "/data" def query_xml(path, title): - tree=ET.parse(path) - # print(tree.find("title").text, file=sys.stderr) + res = {"title": title} + try: + tree = ET.parse(path) + except Exception as e: + log.error(f'xml parsing failed:{e}') + print(json.dumps(res)) + exit(1) + if title == tree.find("title").text: log.info("Exact match found for " + title) else: log.info("No exact match found for " + title + ". Matching with " + tree.find("title").text + "!") # Extract matadata from xml - res={"title":title} if tree.find("title") != None: res["title"] = tree.find("title").text + if tree.find("plot") != None: res["details"] = tree.find("plot").text + if tree.find("releasedate") != None: res["date"] = tree.find("releasedate").text + if tree.find("tag") != None: res["tags"]=[{"name":x.text} for x in tree.findall("tag")] if tree.find("genre") != None: @@ -39,6 +50,7 @@ def query_xml(path, title): res["tags"] += [{"name":x.text} for x in tree.findall("genre")] else: res["tags"] = [{"name":x.text} for x in tree.findall("genre")] + if tree.find("actor") != None: res["performers"] = [] for actor in tree.findall("actor"): @@ -49,6 +61,7 @@ def query_xml(path, title): res["performers"].append({"name": actor.find("name").text}) else: res["performers"].append({"name": actor.text}) + if tree.find("studio") != None: res["studio"] = {"name":tree.find("studio").text} @@ -66,7 +79,6 @@ def query_xml(path, title): log.warning("Can't find image: " + posterElem.text.replace(basePathBefore, basePathAfter) + ". Is the base path correct?") else: log.warning("Can't find image: " + posterElem.text + ". Are you using a docker container? Maybe you need to change the base path in the script file.") - return res def make_image_data_url(image_path): @@ -78,25 +90,26 @@ def make_image_data_url(image_path): if sys.argv[1] == "query": fragment = json.loads(sys.stdin.read()) - res = {"title": fragment["title"]} + s_id = fragment.get("id") + if not s_id: + log.error(f"No ID found") + sys.exit(1) # Assume that .nfo/.xml is named exactly alike the video file and is at the same location # Query graphQL for the file path - graphqlResponse = graphql.getScene(fragment["id"]) - if graphqlResponse: - videoFilePath = graphqlResponse["path"] - else: - exit(0) - - # Reconstruct file name for .nfo - temp = videoFilePath.split(".") - temp[-1] = "nfo" - nfoFilePath = ".".join(temp) - - if os.path.isfile(nfoFilePath): - res = query_xml(nfoFilePath, fragment["title"]) - else: - log.info("No file found at" + nfoFilePath) - - print(json.dumps(res)) - exit(0) + scene = graphql.getScene(s_id) + if scene: + scene_path = scene.get("path") + if scene_path: + p = pathlib.Path(scene_path) + + res = {"title": fragment["title"]} + + f = p.with_suffix(".nfo") + if f.is_file(): + res = query_xml(f, fragment["title"]) + else: + log.info(f"No nfo/xml files found for the scene: {p}") + + print(json.dumps(res)) + exit(0) \ No newline at end of file From 7ec69fa200c76ad8d1298459f437791918f5aa12 Mon Sep 17 00:00:00 2001 From: Phasetime <87231024+Phasetime@users.noreply.github.com> Date: Sat, 8 Jan 2022 12:06:59 +0100 Subject: [PATCH 15/15] more pathlib --- scrapers/kodi.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/scrapers/kodi.py b/scrapers/kodi.py index e5541223a..b14a7bffb 100644 --- a/scrapers/kodi.py +++ b/scrapers/kodi.py @@ -69,11 +69,11 @@ def query_xml(path, title): if tree.find("art").find("poster") != None: posterElem = tree.find("art").find("poster") if posterElem.text != None: - if not rewriteBasePath and os.path.isfile(posterElem.text): + if not rewriteBasePath and pathlib.Path(posterElem.text).is_file(): res["image"] = make_image_data_url(posterElem.text) elif rewriteBasePath: rewrittenPath = posterElem.text.replace(basePathBefore, basePathAfter).replace("\\", "/") - if os.path.isfile(rewrittenPath): + if pathlib.Path(rewrittenPath).is_file(): res["image"] = make_image_data_url(rewrittenPath) else: log.warning("Can't find image: " + posterElem.text.replace(basePathBefore, basePathAfter) + ". Is the base path correct?")