Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scraper for .nfo exports from kodi/plex #689

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions scrapers/kodi.py
Original file line number Diff line number Diff line change
@@ -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 + "!")
bnkai marked this conversation as resolved.
Show resolved Hide resolved

# 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:
bnkai marked this conversation as resolved.
Show resolved Hide resolved
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
10 changes: 10 additions & 0 deletions scrapers/kodi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: "Kodi XML"
sceneByFragment:
action: script
script:
- python
# use python3 instead if needed
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to documentation, Stash is supposed to detect which python is available on the system : either python or python3. See: https://docs.stashapp.cc/in-app-manual/scraping/scraperdevelopment/#actions

If the documentation affirmation stands, this comment may be deleted.

- kodi.py
- query

# Last Updated February 04, 2021