Skip to content

Instantly share code, notes, and snippets.

@BenMcLean
Last active September 12, 2023 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BenMcLean/c6559e6ae5a19553cda2fc4f459231a7 to your computer and use it in GitHub Desktop.
Save BenMcLean/c6559e6ae5a19553cda2fc4f459231a7 to your computer and use it in GitHub Desktop.
Replace hyphens in Jellyfin movie titles with colons.
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)
@BenMcLean
Copy link
Author

Theme song for this script: https://youtu.be/cVi477_kDYA

@BenMcLean
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment