Last active
September 12, 2023 14:09
-
-
Save BenMcLean/c6559e6ae5a19553cda2fc4f459231a7 to your computer and use it in GitHub Desktop.
Replace hyphens in Jellyfin movie titles with colons.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
import glob | |
import os | |
import sys | |
import xml.etree.ElementTree | |
parser = argparse.ArgumentParser(description='Replaces hyphens in Jellyfin movie titles with colons instead.') | |
parser.add_argument('-f', '--folder', metavar='folder', type=str, nargs=1, required=True, default=[1], help='Root directory') | |
def error(msg, exitcode=3): | |
print(msg, file=sys.stderr) | |
sys.exit(exitcode) | |
args = parser.parse_args() | |
folder = args.folder[0] | |
if not os.path.exists(folder): | |
error('Path "' + folder + '" not found!', 4) | |
for file in glob.glob(os.path.join(folder, '**\*.nfo'), recursive=True): | |
et = xml.etree.ElementTree.parse(file) | |
if et is None: | |
error('Couldn\'t parse XML of file "' + file + '"!', 5) | |
root = et.getroot() | |
if root is None: | |
error('Couldn\'t parse root of XML file "' + file + '"!', 6) | |
title = root.find('title') | |
if title is None: | |
error('Couldn\'t get title in XML file "' + file + '"!', 7) | |
replacement = title.text.replace(' - ', ': ') | |
change = title.text != replacement | |
if (change): | |
title.text = replacement | |
sorttitle = root.find('sorttitle') | |
if sorttitle is not None: | |
replacement = sorttitle.text.replace(' - ', ': ') | |
if (sorttitle.text != replacement): | |
sorttitle.text = replacement | |
change = True | |
if (change): | |
print("Writing " + file) | |
et.write(file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Theme song for this script: https://youtu.be/cVi477_kDYA